help with modified positional argument
This commit is contained in:
parent
138c12616f
commit
89ec6309b1
2 changed files with 79 additions and 56 deletions
|
@ -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)
|
||||||
|
|
|
@ -182,13 +182,17 @@ 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 _forhelp and not obj.owner.isdefault() and obj.value.get():
|
||||||
|
self.prog += ' {}'.format(obj.value.get())
|
||||||
|
else:
|
||||||
if 'positional' in properties:
|
if 'positional' in properties:
|
||||||
if not 'mandatory' in properties:
|
if not 'mandatory' in properties:
|
||||||
raise ValueError('"positional" argument must be "mandatory" too')
|
raise ValueError('"positional" argument must be "mandatory" too')
|
||||||
args = [option.path()]
|
|
||||||
if _forhelp:
|
if _forhelp:
|
||||||
|
args = [option.path()]
|
||||||
kwargs['default'] = obj.value.default()
|
kwargs['default'] = obj.value.default()
|
||||||
else:
|
else:
|
||||||
|
args = [option.path()]
|
||||||
kwargs['default'] = obj.value.get()
|
kwargs['default'] = obj.value.get()
|
||||||
kwargs['nargs'] = '?'
|
kwargs['nargs'] = '?'
|
||||||
else:
|
else:
|
||||||
|
@ -197,6 +201,7 @@ class TiramisuCmdlineParser(ArgumentParser):
|
||||||
args = [self._gen_argument(name, properties)]
|
args = [self._gen_argument(name, properties)]
|
||||||
if _forhelp and 'mandatory' in properties:
|
if _forhelp and 'mandatory' in properties:
|
||||||
kwargs['required'] = True
|
kwargs['required'] = True
|
||||||
|
|
||||||
if option.type() == 'boolean':
|
if option.type() == 'boolean':
|
||||||
if 'mandatory' in properties:
|
if 'mandatory' in properties:
|
||||||
raise ValueError('"mandatory" property is not allowed for BoolOption')
|
raise ValueError('"mandatory" property is not allowed for BoolOption')
|
||||||
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue