help with modified positional argument

This commit is contained in:
Emmanuel Garette 2019-04-02 08:49:36 +02:00
parent f90c02282a
commit 3ac9031411
2 changed files with 79 additions and 56 deletions

View File

@ -109,6 +109,26 @@ root:
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():
output = """usage: prog.py [-h] [-v] {str,list,int,none}
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():
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
"""
parser = TiramisuCmdlineParser(get_config(), 'prog.py')
@ -174,7 +194,7 @@ prog.py: error: the following arguments are required: --str
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
"""
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():
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
"""
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():
output = """usage: prog.py [-h] [-v] {str,list,int,none}
output = """usage: prog.py none [-h] [-v]
prog.py: error: unrecognized arguments: --int
"""
parser = TiramisuCmdlineParser(get_config(), 'prog.py')
@ -222,7 +242,7 @@ prog.py: error: unrecognized arguments: --int
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
"""
parser = TiramisuCmdlineParser(get_config(True), 'prog.py')
@ -238,7 +258,7 @@ prog.py: error: unrecognized arguments: --int
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
"""
parser = TiramisuCmdlineParser(get_config(True), 'prog.py', fullpath=False)

View File

@ -182,13 +182,17 @@ class TiramisuCmdlineParser(ArgumentParser):
if option.issymlinkoption():
actions[option.name(follow_symlink=True)][0].insert(0, self._gen_argument(option.name(), properties))
continue
if _forhelp and not obj.owner.isdefault() and obj.value.get():
self.prog += ' {}'.format(obj.value.get())
else:
if 'positional' in properties:
if not 'mandatory' in properties:
raise ValueError('"positional" argument must be "mandatory" too')
args = [option.path()]
if _forhelp:
args = [option.path()]
kwargs['default'] = obj.value.default()
else:
args = [option.path()]
kwargs['default'] = obj.value.get()
kwargs['nargs'] = '?'
else:
@ -197,6 +201,7 @@ class TiramisuCmdlineParser(ArgumentParser):
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')
@ -279,14 +284,12 @@ class TiramisuCmdlineParser(ArgumentParser):
_forhelp=True)
return super(TiramisuCmdlineParser, help_formatter).format_usage(*args, **kwargs)
def format_help(self,
*args,
**kwargs):
def format_help(self):
help_formatter = TiramisuCmdlineParser(self.config,
self.prog,
fullpath=self.fullpath,
_forhelp=True)
return super(TiramisuCmdlineParser, help_formatter).format_help(*args, **kwargs)
return super(TiramisuCmdlineParser, help_formatter).format_help()
def get_config(self):
return self.config