-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUniParse.py
142 lines (136 loc) · 5.74 KB
/
UniParse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
# -*- coding: us-ascii -*-
#--------------------------------------------------------------------------------
# Copyright (C) 2015-2016 BigDFT group
# This file is distributed under the terms of the
# GNU General Public License, see
# or http://www.gnu.org/copyleft/gpl.txt .
#--------------------------------------------------------------------------------
#Redefine ArgumentParser to have the help message if no arguments
class UniParser():
def __init__(self,*args,**kwargs):
argss,whichone=self.kw_pop('method','argparse',**kwargs)
if whichone=='argparse':
try:
import argparse
self.argparse=True
except:
import optparse
self.argparse=False
elif whichone=='optparse':
import optparse
self.argparse=False
else:
raise ValueError('method not recognized')
if not self.argparse: self.positional=[]
if self.argparse:
arg = self.kw_update('formatter_class',argparse.RawDescriptionHelpFormatter,**argss)
self.parser=argparse.ArgumentParser(*args,**arg)
else:
self.parser=optparse.OptionParser(*args,**argss)
def option(self,*args,**kwargs):
arg,remainder=self.kw_pop('remainder',False,**kwargs)
if self.argparse:
import argparse
if remainder: arg.update({'nargs':argparse.REMAINDER})
self.parser.add_argument(*args,**arg)
else:
name=args[0]
if '-' not in name or remainder:
if remainder:
name=args[1].lstrip('--').replace('-','_')
self.parser.add_option(*args,**arg)
positional=self.kw_update('name',name,**kwargs)
self.positional.append(positional)
if remainder: positional.update({'remainder':True})
else:
self.parser.add_option(*args,**arg)
def add_group(self,*args,**kwargs):
arg,mutually_exclusive=self.kw_pop('mutually_exclusive',False,**kwargs)
if self.argparse:
if mutually_exclusive:
grp=self.parser.add_mutually_exclusive_group(*args,**arg)
else:
grp=self.parser.add_argument_group(*args,**arg)
else:
import optparse
if len(args) == 0: args += ('group',)
grp=optparse.OptionGroup(self.parser,*args,**arg)
if hasattr(self,'groups'):
self.groups.append(grp)
else:
self.groups=[grp]
def group_option(self,*args,**kwargs):
if self.argparse:
self.groups[-1].add_argument(*args,**kwargs)
else:
self.groups[-1].add_option(*args,**kwargs)
def kw_update(self,key,val,**kwargs):
arg=kwargs.copy()
arg.update({key: val})
return arg
def kw_pop(self,*args,**kwargs):
arg=kwargs.copy()
key,default=args
if key in arg:
return arg,arg.pop(key)
else:
return arg,default
def error(self, message):
import sys
sys.stderr.write('error: %s\n' % message)
self.print_help()
self.exit()
def args(self):
meth='argparse' if self.argparse else 'optparse'
print 'Parsing arguments with method '+meth+'...'
if self.argparse:
return self.parser.parse_args()
else:
(argstmp, argtmp) = self.parser.parse_args()
for i,pos in enumerate(self.positional):
remainder=pos.get('remainder',False)
if i < len(argtmp):
if remainder:
val=[argtmp[i]]
for j in range(i+1,len(argtmp)):
val.append(argtmp[j])
else:
val=argtmp[i]
else:
val=pos.get('default',[])
name=pos['name']
if hasattr(argstmp,name):
val=[getattr(argstmp,name)]+val
setattr(argstmp,name,val)
return argstmp
def test(method):
parser= UniParser(description='BigDFT suite Installer',
epilog='''
If you want more help type "%(prog)s help"
------------------------------------------------
For more information, visit www.bigdft.org''',method=method)
parser.option('action',nargs='?',default='help',
help='Action to be performed by the Installer.'
' (default: %(default)s)',choices=['help'])
parser.option('package',nargs='?',default='spred',
help='Package to be built by the installer. (default: %(default)s)')
parser.option('-f','--file',
help='Use an alternative configuration file instead of the default configuration '
+ 'given by the environment variable <foo>')
parser.add_group(mutually_exclusive=True)
parser.group_option("-v", "--verbose", action="store_true",help='Verbose output, default from a development branch')
parser.group_option("-q", "--quiet", action="store_true",help='Verbosity disabled output, default from a development branch')
parser.option('-d','--debug',action='store_true',
help='Verbose output, default from a development branch')
parser.option('-c','--configure-line',remainder=True, #nargs=argparse.REMAINDER,
help='Specify the configure line to be passed (set BIGDFT_CONFIGURE_FLAGS variable)')
args = parser.args()
return args
if __name__ == "__main__":
args=test('argparse')
print 'Arguments argparse'
print args.__dict__
args=test('optparse')
print 'Arguments optparse'
print args.__dict__