add display_modified_value parameter

This commit is contained in:
Emmanuel Garette 2019-07-30 06:58:16 +02:00
parent 7fe8cf322d
commit e2c4e3381a
3 changed files with 234 additions and 1 deletions

View file

@ -331,6 +331,53 @@ prog.py: error: the following arguments are required: --leader.follower_submulti
assert config.value.dict() == output
def test_leadership_modif_mandatory_remove(json):
output = {'leader.leader': ['192.168.1.1'],
'leader.follower': [None],
'leader.follower_mandatory': ['255.255.255.128'],
'leader.follower_boolean': [None],
'leader.follower_choice': [None],
'leader.follower_integer': [None],
'leader.follower_submulti': [['255.255.255.128']]}
output2 = """usage: prog.py --leader.leader "192.168.1.1" [-h] [--leader.pop-leader INDEX]
[--leader.follower INDEX [FOLLOWER]]
--leader.follower_submulti INDEX
[FOLLOWER_SUBMULTI ...]
[--leader.follower_integer INDEX [FOLLOWER_INTEGER]]
[--leader.follower_boolean INDEX]
[--leader.no-follower_boolean INDEX]
[--leader.follower_choice INDEX [{opt1,opt2}]]
--leader.follower_mandatory INDEX
FOLLOWER_MANDATORY
prog.py: error: the following arguments are required: --leader.follower_submulti"""
config = get_config(json, with_mandatory=True)
parser = TiramisuCmdlineParser(config, 'prog.py', display_modified_value=False)
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['--leader.leader', '192.168.1.1'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output2 + ', --leader.follower_mandatory\n'
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['--leader.leader', '192.168.1.1',
'--leader.follower_mandatory', '0', '255.255.255.128'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output2 + '\n'
parser.parse_args(['--leader.leader', '192.168.1.1',
'--leader.follower_submulti', '0', '255.255.255.128',
'--leader.follower_mandatory', '0', '255.255.255.128'])
assert config.value.dict() == output
def test_leadership_modif_mandatory_unvalidate(json):
output = {'leader.leader': ['192.168.1.1'],
'leader.follower': [None],

View file

@ -154,6 +154,27 @@ optional arguments:
assert f.getvalue() == output
def test_readme_help_modif_positional_remove(json):
output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR
optional arguments:
-h, --help show this help message and exit
-v, --verbosity increase output verbosity
-nv, --no-verbosity
--str STR string option
"""
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False)
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_help_modif(json):
output = """usage: prog.py "str" --str "toto" [-h] [-v] [-nv] --str STR
{str,list,int,none}
@ -179,7 +200,27 @@ optional arguments:
assert f.getvalue() == output
def test_readme_help_modif_short1(json):
def test_readme_help_modif_remove(json):
output = """usage: prog.py "str" --str "toto" [-h] [-v] [-nv]
optional arguments:
-h, --help show this help message and exit
-v, --verbosity increase output verbosity
-nv, --no-verbosity
"""
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False)
f = StringIO()
with redirect_stdout(f):
try:
parser.parse_args(['str', '--str', 'toto', '--help'])
except SystemExit as err:
assert str(err) == "0"
else:
raise Exception('must raises')
assert f.getvalue() == output
def test_readme_help_modif_short(json):
output = """usage: prog.py "str" -v [-h] [-v] [-nv] --str STR {str,list,int,none}
positional arguments:
@ -203,6 +244,27 @@ optional arguments:
assert f.getvalue() == output
def test_readme_help_modif_short_remove(json):
# FIXME -v -nv ?? pas de description
output = """usage: prog.py "str" -v [-h] [-nv] --str STR
optional arguments:
-h, --help show this help message and exit
-nv, --no-verbosity
--str STR string option
"""
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False)
f = StringIO()
with redirect_stdout(f):
try:
parser.parse_args(['str', '-v', '--help'])
except SystemExit as err:
assert str(err) == "0"
else:
raise Exception('must raises')
assert f.getvalue() == output
def test_readme_help_modif_short_no(json):
output = """usage: prog.py "str" -v [-h] [-v] [-nv] --str STR {str,list,int,none}
@ -227,6 +289,27 @@ optional arguments:
assert f.getvalue() == output
def test_readme_help_modif_short_no_remove(json):
# FIXME -v -nv ?? c'est -nv qui est set
output = """usage: prog.py "str" -v [-h] [-nv] --str STR
optional arguments:
-h, --help show this help message and exit
-nv, --no-verbosity
--str STR string option
"""
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False)
f = StringIO()
with redirect_stdout(f):
try:
parser.parse_args(['str', '-nv', '--help'])
except SystemExit as err:
assert str(err) == "0"
else:
raise Exception('must raises')
assert f.getvalue() == output
def test_readme_positional_mandatory(json):
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: the following arguments are required: cmd
@ -291,6 +374,22 @@ prog.py: error: the following arguments are required: --str
assert f.getvalue() == output
def test_readme_mandatory_remove(json):
output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR
prog.py: error: the following arguments are required: --str
"""
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False)
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['str'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
def test_readme_mandatory_tree(json):
output = """usage: prog.py "str" [-h] [-v] [-nv] --root.str STR {str,list,int,none}
prog.py: error: the following arguments are required: --root.str
@ -307,6 +406,22 @@ prog.py: error: the following arguments are required: --root.str
assert f.getvalue() == output
def test_readme_mandatory_tree_remove(json):
output = """usage: prog.py "str" [-h] [-v] [-nv] --root.str STR
prog.py: error: the following arguments are required: --root.str
"""
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', display_modified_value=False)
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['str'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
def test_readme_mandatory_tree_flatten(json):
output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR {str,list,int,none}
prog.py: error: the following arguments are required: --str
@ -323,6 +438,22 @@ prog.py: error: the following arguments are required: --str
assert f.getvalue() == output
def test_readme_mandatory_tree_flatten_remove(json):
output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR
prog.py: error: the following arguments are required: --str
"""
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, display_modified_value=False)
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['str'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
def test_readme_cross(json):
output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: unrecognized arguments: --int
@ -339,6 +470,22 @@ prog.py: error: unrecognized arguments: --int
assert f.getvalue() == output
def test_readme_cross_remove(json):
output = """usage: prog.py "none" [-h] [-v] [-nv]
prog.py: error: unrecognized arguments: --int
"""
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False)
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['none', '--int'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
def test_readme_cross_tree(json):
output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: unrecognized arguments: --int
@ -355,6 +502,22 @@ prog.py: error: unrecognized arguments: --int
assert f.getvalue() == output
def test_readme_cross_tree_remove(json):
output = """usage: prog.py "none" [-h] [-v] [-nv]
prog.py: error: unrecognized arguments: --int
"""
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', display_modified_value=False)
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['none', '--int'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
def test_readme_cross_tree_flatten(json):
output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: unrecognized arguments: --int
@ -371,6 +534,22 @@ prog.py: error: unrecognized arguments: --int
assert f.getvalue() == output
def test_readme_cross_tree_flatten_remove(json):
output = """usage: prog.py "none" [-h] [-v] [-nv]
prog.py: error: unrecognized arguments: --int
"""
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, display_modified_value=False)
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['none', '--int'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
def test_readme_unknown(json):
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: argument root.cmd: invalid choice: 'unknown' (choose from 'str', 'list', 'int', 'none')

View file

@ -223,6 +223,7 @@ class TiramisuCmdlineParser(ArgumentParser):
root: str=None,
fullpath: bool=True,
remove_empty_od: bool=False,
display_modified_value: bool=True,
formatter_class=HelpFormatter,
_forhelp: bool=False,
**kwargs):
@ -230,6 +231,7 @@ class TiramisuCmdlineParser(ArgumentParser):
self.config = config
self.root = root
self.remove_empty_od = remove_empty_od
self.display_modified_value = display_modified_value
if TiramisuHelpFormatter not in formatter_class.__mro__:
formatter_class = type('TiramisuHelpFormatter', (TiramisuHelpFormatter, formatter_class), {})
formatter_class.remove_empty_od = self.remove_empty_od
@ -287,6 +289,7 @@ class TiramisuCmdlineParser(ArgumentParser):
self.prog,
root=self.root,
remove_empty_od=self.remove_empty_od,
display_modified_value=self.display_modified_value,
formatter_class=self.formatter_class,
epilog=self.epilog,
description=self.description,
@ -407,6 +410,8 @@ class TiramisuCmdlineParser(ArgumentParser):
'type': option.type(),
'name': name,
'value': value}
if not self.display_modified_value:
continue
if 'positional' in properties:
if option.type() == 'boolean':
raise ValueError(_('boolean option must not be positional'))
@ -548,6 +553,7 @@ class TiramisuCmdlineParser(ArgumentParser):
root=self.root,
fullpath=self.fullpath,
remove_empty_od=self.remove_empty_od,
display_modified_value=self.display_modified_value,
formatter_class=self.formatter_class,
epilog=self.epilog,
description=self.description,
@ -560,6 +566,7 @@ class TiramisuCmdlineParser(ArgumentParser):
root=self.root,
fullpath=self.fullpath,
remove_empty_od=self.remove_empty_od,
display_modified_value=self.display_modified_value,
formatter_class=self.formatter_class,
epilog=self.epilog,
description=self.description,