help with modified positional argument

This commit is contained in:
Emmanuel Garette 2019-04-02 08:49:36 +02:00 committed by Emmanuel Garette
parent 138c12616f
commit 89ec6309b1
2 changed files with 79 additions and 56 deletions

View file

@ -109,6 +109,26 @@ root:
assert f.getvalue() == output assert f.getvalue() == output
def test_readme_help_modif():
output = """usage: prog.py str [-h] [-v] --str STR
optional arguments:
-h, --help show this help message and exit
-v, --verbosity increase output verbosity
--str STR string option
"""
parser = TiramisuCmdlineParser(get_config(), 'prog.py')
f = StringIO()
with redirect_stdout(f):
try:
parser.parse_args(['str', '--help'])
except SystemExit as err:
assert str(err) == "0"
else:
raise Exception('must raises')
assert f.getvalue() == output
def test_readme_positional_mandatory(): def test_readme_positional_mandatory():
output = """usage: prog.py [-h] [-v] {str,list,int,none} output = """usage: prog.py [-h] [-v] {str,list,int,none}
prog.py: error: the following arguments are required: cmd prog.py: error: the following arguments are required: cmd
@ -158,7 +178,7 @@ prog.py: error: the following arguments are required: cmd
def test_readme_mandatory(): def test_readme_mandatory():
output = """usage: prog.py [-h] [-v] --str STR {str,list,int,none} output = """usage: prog.py str [-h] [-v] --str STR
prog.py: error: the following arguments are required: --str prog.py: error: the following arguments are required: --str
""" """
parser = TiramisuCmdlineParser(get_config(), 'prog.py') parser = TiramisuCmdlineParser(get_config(), 'prog.py')
@ -174,7 +194,7 @@ prog.py: error: the following arguments are required: --str
def test_readme_mandatory_tree(): def test_readme_mandatory_tree():
output = """usage: prog.py [-h] [-v] --root.str STR {str,list,int,none} output = """usage: prog.py str [-h] [-v] --root.str STR
prog.py: error: the following arguments are required: --root.str prog.py: error: the following arguments are required: --root.str
""" """
parser = TiramisuCmdlineParser(get_config(True), 'prog.py') parser = TiramisuCmdlineParser(get_config(True), 'prog.py')
@ -190,7 +210,7 @@ prog.py: error: the following arguments are required: --root.str
def test_readme_mandatory_tree_flatten(): def test_readme_mandatory_tree_flatten():
output = """usage: prog.py [-h] [-v] --str STR {str,list,int,none} output = """usage: prog.py str [-h] [-v] --str STR
prog.py: error: the following arguments are required: --str prog.py: error: the following arguments are required: --str
""" """
parser = TiramisuCmdlineParser(get_config(True), 'prog.py', fullpath=False) parser = TiramisuCmdlineParser(get_config(True), 'prog.py', fullpath=False)
@ -206,7 +226,7 @@ prog.py: error: the following arguments are required: --str
def test_readme_cross(): def test_readme_cross():
output = """usage: prog.py [-h] [-v] {str,list,int,none} output = """usage: prog.py none [-h] [-v]
prog.py: error: unrecognized arguments: --int prog.py: error: unrecognized arguments: --int
""" """
parser = TiramisuCmdlineParser(get_config(), 'prog.py') parser = TiramisuCmdlineParser(get_config(), 'prog.py')
@ -222,7 +242,7 @@ prog.py: error: unrecognized arguments: --int
def test_readme_cross_tree(): def test_readme_cross_tree():
output = """usage: prog.py [-h] [-v] {str,list,int,none} output = """usage: prog.py none [-h] [-v]
prog.py: error: unrecognized arguments: --int prog.py: error: unrecognized arguments: --int
""" """
parser = TiramisuCmdlineParser(get_config(True), 'prog.py') parser = TiramisuCmdlineParser(get_config(True), 'prog.py')
@ -238,7 +258,7 @@ prog.py: error: unrecognized arguments: --int
def test_readme_cross_tree_flatten(): def test_readme_cross_tree_flatten():
output = """usage: prog.py [-h] [-v] {str,list,int,none} output = """usage: prog.py none [-h] [-v]
prog.py: error: unrecognized arguments: --int prog.py: error: unrecognized arguments: --int
""" """
parser = TiramisuCmdlineParser(get_config(True), 'prog.py', fullpath=False) parser = TiramisuCmdlineParser(get_config(True), 'prog.py', fullpath=False)

View file

@ -182,55 +182,60 @@ class TiramisuCmdlineParser(ArgumentParser):
if option.issymlinkoption(): if option.issymlinkoption():
actions[option.name(follow_symlink=True)][0].insert(0, self._gen_argument(option.name(), properties)) actions[option.name(follow_symlink=True)][0].insert(0, self._gen_argument(option.name(), properties))
continue continue
if 'positional' in properties: if _forhelp and not obj.owner.isdefault() and obj.value.get():
if not 'mandatory' in properties: self.prog += ' {}'.format(obj.value.get())
raise ValueError('"positional" argument must be "mandatory" too')
args = [option.path()]
if _forhelp:
kwargs['default'] = obj.value.default()
else:
kwargs['default'] = obj.value.get()
kwargs['nargs'] = '?'
else: else:
kwargs['dest'] = option.path() if 'positional' in properties:
kwargs['default'] = SUPPRESS if not 'mandatory' in properties:
args = [self._gen_argument(name, properties)] raise ValueError('"positional" argument must be "mandatory" too')
if _forhelp and 'mandatory' in properties: if _forhelp:
kwargs['required'] = True args = [option.path()]
if option.type() == 'boolean': kwargs['default'] = obj.value.default()
if 'mandatory' in properties:
raise ValueError('"mandatory" property is not allowed for BoolOption')
#if not isinstance(option.default(), bool):
# raise ValueError('default value is mandatory for BoolOption')
if obj.value.get() is False:
action = 'store_true'
else:
action = 'store_false'
kwargs['action'] = action
else:
if _forhelp:
value = obj.value.default()
else:
value = obj.value.get()
if value not in [None, []]:
#kwargs['default'] = kwargs['const'] = option.default()
#kwargs['action'] = 'store_const'
kwargs['nargs'] = '?'
if option.ismulti():
if _forhelp and 'mandatory' in properties:
kwargs['nargs'] = '+'
else: else:
kwargs['nargs'] = '*' args = [option.path()]
if option.type() == 'string': kwargs['default'] = obj.value.get()
pass kwargs['nargs'] = '?'
elif option.type() == 'integer':
kwargs['type'] = int
elif option.type() == 'choice':
kwargs['choices'] = obj.value.list()
else: else:
pass kwargs['dest'] = option.path()
#raise NotImplementedError('not supported yet') kwargs['default'] = SUPPRESS
actions[option.name()] = (args, kwargs) args = [self._gen_argument(name, properties)]
if _forhelp and 'mandatory' in properties:
kwargs['required'] = True
if option.type() == 'boolean':
if 'mandatory' in properties:
raise ValueError('"mandatory" property is not allowed for BoolOption')
#if not isinstance(option.default(), bool):
# raise ValueError('default value is mandatory for BoolOption')
if obj.value.get() is False:
action = 'store_true'
else:
action = 'store_false'
kwargs['action'] = action
else:
if _forhelp:
value = obj.value.default()
else:
value = obj.value.get()
if value not in [None, []]:
#kwargs['default'] = kwargs['const'] = option.default()
#kwargs['action'] = 'store_const'
kwargs['nargs'] = '?'
if option.ismulti():
if _forhelp and 'mandatory' in properties:
kwargs['nargs'] = '+'
else:
kwargs['nargs'] = '*'
if option.type() == 'string':
pass
elif option.type() == 'integer':
kwargs['type'] = int
elif option.type() == 'choice':
kwargs['choices'] = obj.value.list()
else:
pass
#raise NotImplementedError('not supported yet')
actions[option.name()] = (args, kwargs)
for args, kwargs in actions.values(): for args, kwargs in actions.values():
group.add_argument(*args, **kwargs) group.add_argument(*args, **kwargs)
@ -279,14 +284,12 @@ class TiramisuCmdlineParser(ArgumentParser):
_forhelp=True) _forhelp=True)
return super(TiramisuCmdlineParser, help_formatter).format_usage(*args, **kwargs) return super(TiramisuCmdlineParser, help_formatter).format_usage(*args, **kwargs)
def format_help(self, def format_help(self):
*args,
**kwargs):
help_formatter = TiramisuCmdlineParser(self.config, help_formatter = TiramisuCmdlineParser(self.config,
self.prog, self.prog,
fullpath=self.fullpath, fullpath=self.fullpath,
_forhelp=True) _forhelp=True)
return super(TiramisuCmdlineParser, help_formatter).format_help(*args, **kwargs) return super(TiramisuCmdlineParser, help_formatter).format_help()
def get_config(self): def get_config(self):
return self.config return self.config