Compare commits
3 Commits
release/0.
...
release/0.
Author | SHA1 | Date | |
---|---|---|---|
57e85b49eb | |||
504d5d71a4 | |||
e2c4e3381a |
@ -331,6 +331,53 @@ prog.py: error: the following arguments are required: --leader.follower_submulti
|
|||||||
assert config.value.dict() == output
|
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):
|
def test_leadership_modif_mandatory_unvalidate(json):
|
||||||
output = {'leader.leader': ['192.168.1.1'],
|
output = {'leader.leader': ['192.168.1.1'],
|
||||||
'leader.follower': [None],
|
'leader.follower': [None],
|
||||||
|
@ -154,6 +154,27 @@ optional arguments:
|
|||||||
assert f.getvalue() == output
|
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):
|
def test_readme_help_modif(json):
|
||||||
output = """usage: prog.py "str" --str "toto" [-h] [-v] [-nv] --str STR
|
output = """usage: prog.py "str" --str "toto" [-h] [-v] [-nv] --str STR
|
||||||
{str,list,int,none}
|
{str,list,int,none}
|
||||||
@ -179,7 +200,27 @@ optional arguments:
|
|||||||
assert f.getvalue() == output
|
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}
|
output = """usage: prog.py "str" -v [-h] [-v] [-nv] --str STR {str,list,int,none}
|
||||||
|
|
||||||
positional arguments:
|
positional arguments:
|
||||||
@ -203,7 +244,28 @@ optional arguments:
|
|||||||
assert f.getvalue() == output
|
assert f.getvalue() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_help_modif_short_no(json):
|
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 increase output 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_no1(json):
|
||||||
output = """usage: prog.py "str" -v [-h] [-v] [-nv] --str STR {str,list,int,none}
|
output = """usage: prog.py "str" -v [-h] [-v] [-nv] --str STR {str,list,int,none}
|
||||||
|
|
||||||
positional arguments:
|
positional arguments:
|
||||||
@ -227,6 +289,26 @@ optional arguments:
|
|||||||
assert f.getvalue() == output
|
assert f.getvalue() == output
|
||||||
|
|
||||||
|
|
||||||
|
def test_readme_help_modif_short_no_remove(json):
|
||||||
|
output = """usage: prog.py "str" -v [-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(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):
|
def test_readme_positional_mandatory(json):
|
||||||
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
|
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
|
||||||
prog.py: error: the following arguments are required: cmd
|
prog.py: error: the following arguments are required: cmd
|
||||||
@ -291,6 +373,22 @@ prog.py: error: the following arguments are required: --str
|
|||||||
assert f.getvalue() == output
|
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):
|
def test_readme_mandatory_tree(json):
|
||||||
output = """usage: prog.py "str" [-h] [-v] [-nv] --root.str STR {str,list,int,none}
|
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
|
prog.py: error: the following arguments are required: --root.str
|
||||||
@ -307,6 +405,22 @@ prog.py: error: the following arguments are required: --root.str
|
|||||||
assert f.getvalue() == output
|
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):
|
def test_readme_mandatory_tree_flatten(json):
|
||||||
output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR {str,list,int,none}
|
output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR {str,list,int,none}
|
||||||
prog.py: error: the following arguments are required: --str
|
prog.py: error: the following arguments are required: --str
|
||||||
@ -323,6 +437,22 @@ prog.py: error: the following arguments are required: --str
|
|||||||
assert f.getvalue() == output
|
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):
|
def test_readme_cross(json):
|
||||||
output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none}
|
output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none}
|
||||||
prog.py: error: unrecognized arguments: --int
|
prog.py: error: unrecognized arguments: --int
|
||||||
@ -339,6 +469,22 @@ prog.py: error: unrecognized arguments: --int
|
|||||||
assert f.getvalue() == output
|
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):
|
def test_readme_cross_tree(json):
|
||||||
output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none}
|
output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none}
|
||||||
prog.py: error: unrecognized arguments: --int
|
prog.py: error: unrecognized arguments: --int
|
||||||
@ -355,6 +501,22 @@ prog.py: error: unrecognized arguments: --int
|
|||||||
assert f.getvalue() == output
|
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):
|
def test_readme_cross_tree_flatten(json):
|
||||||
output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none}
|
output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none}
|
||||||
prog.py: error: unrecognized arguments: --int
|
prog.py: error: unrecognized arguments: --int
|
||||||
@ -371,6 +533,22 @@ prog.py: error: unrecognized arguments: --int
|
|||||||
assert f.getvalue() == output
|
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):
|
def test_readme_unknown(json):
|
||||||
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
|
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')
|
prog.py: error: argument root.cmd: invalid choice: 'unknown' (choose from 'str', 'list', 'int', 'none')
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
from .api import TiramisuCmdlineParser
|
from .api import TiramisuCmdlineParser
|
||||||
|
|
||||||
__version__ = "0.1"
|
__version__ = "0.2"
|
||||||
__all__ = ('TiramisuCmdlineParser',)
|
__all__ = ('TiramisuCmdlineParser',)
|
||||||
|
@ -147,13 +147,15 @@ class _BuildKwargs:
|
|||||||
cmdlineparser: 'TiramisuCmdlineParser',
|
cmdlineparser: 'TiramisuCmdlineParser',
|
||||||
properties: List[str],
|
properties: List[str],
|
||||||
force_no: bool,
|
force_no: bool,
|
||||||
force_del: bool) -> None:
|
force_del: bool,
|
||||||
|
display_modified_value: bool,
|
||||||
|
not_display: bool) -> None:
|
||||||
self.kwargs = {}
|
self.kwargs = {}
|
||||||
self.cmdlineparser = cmdlineparser
|
self.cmdlineparser = cmdlineparser
|
||||||
self.properties = properties
|
self.properties = properties
|
||||||
self.force_no = force_no
|
self.force_no = force_no
|
||||||
self.force_del = force_del
|
self.force_del = force_del
|
||||||
if not self.force_no and not self.force_del:
|
if (not self.force_no or (not_display and not display_modified_value)) and not self.force_del:
|
||||||
description = option.doc()
|
description = option.doc()
|
||||||
if not description:
|
if not description:
|
||||||
description = description.replace('%', '%%')
|
description = description.replace('%', '%%')
|
||||||
@ -223,6 +225,7 @@ class TiramisuCmdlineParser(ArgumentParser):
|
|||||||
root: str=None,
|
root: str=None,
|
||||||
fullpath: bool=True,
|
fullpath: bool=True,
|
||||||
remove_empty_od: bool=False,
|
remove_empty_od: bool=False,
|
||||||
|
display_modified_value: bool=True,
|
||||||
formatter_class=HelpFormatter,
|
formatter_class=HelpFormatter,
|
||||||
_forhelp: bool=False,
|
_forhelp: bool=False,
|
||||||
**kwargs):
|
**kwargs):
|
||||||
@ -230,6 +233,7 @@ class TiramisuCmdlineParser(ArgumentParser):
|
|||||||
self.config = config
|
self.config = config
|
||||||
self.root = root
|
self.root = root
|
||||||
self.remove_empty_od = remove_empty_od
|
self.remove_empty_od = remove_empty_od
|
||||||
|
self.display_modified_value = display_modified_value
|
||||||
if TiramisuHelpFormatter not in formatter_class.__mro__:
|
if TiramisuHelpFormatter not in formatter_class.__mro__:
|
||||||
formatter_class = type('TiramisuHelpFormatter', (TiramisuHelpFormatter, formatter_class), {})
|
formatter_class = type('TiramisuHelpFormatter', (TiramisuHelpFormatter, formatter_class), {})
|
||||||
formatter_class.remove_empty_od = self.remove_empty_od
|
formatter_class.remove_empty_od = self.remove_empty_od
|
||||||
@ -287,6 +291,7 @@ class TiramisuCmdlineParser(ArgumentParser):
|
|||||||
self.prog,
|
self.prog,
|
||||||
root=self.root,
|
root=self.root,
|
||||||
remove_empty_od=self.remove_empty_od,
|
remove_empty_od=self.remove_empty_od,
|
||||||
|
display_modified_value=self.display_modified_value,
|
||||||
formatter_class=self.formatter_class,
|
formatter_class=self.formatter_class,
|
||||||
epilog=self.epilog,
|
epilog=self.epilog,
|
||||||
description=self.description,
|
description=self.description,
|
||||||
@ -401,12 +406,15 @@ class TiramisuCmdlineParser(ArgumentParser):
|
|||||||
properties = obj.option.properties()
|
properties = obj.option.properties()
|
||||||
else:
|
else:
|
||||||
properties = obj.property.get()
|
properties = obj.property.get()
|
||||||
kwargs = _BuildKwargs(name, option, self, properties, force_no, force_del)
|
not_display = not option.isfollower() and not obj.owner.isdefault() and value is not None
|
||||||
if not option.isfollower() and _forhelp and not obj.owner.isdefault() and value is not None and not force_no:
|
kwargs = _BuildKwargs(name, option, self, properties, force_no, force_del, self.display_modified_value, not_display)
|
||||||
|
if _forhelp and not_display and ((value is not False and not force_no) or (value is False and force_no)):
|
||||||
options_is_not_default[option.name()] = {'properties': properties,
|
options_is_not_default[option.name()] = {'properties': properties,
|
||||||
'type': option.type(),
|
'type': option.type(),
|
||||||
'name': name,
|
'name': name,
|
||||||
'value': value}
|
'value': value}
|
||||||
|
if not self.display_modified_value:
|
||||||
|
continue
|
||||||
if 'positional' in properties:
|
if 'positional' in properties:
|
||||||
if option.type() == 'boolean':
|
if option.type() == 'boolean':
|
||||||
raise ValueError(_('boolean option must not be positional'))
|
raise ValueError(_('boolean option must not be positional'))
|
||||||
@ -548,6 +556,7 @@ class TiramisuCmdlineParser(ArgumentParser):
|
|||||||
root=self.root,
|
root=self.root,
|
||||||
fullpath=self.fullpath,
|
fullpath=self.fullpath,
|
||||||
remove_empty_od=self.remove_empty_od,
|
remove_empty_od=self.remove_empty_od,
|
||||||
|
display_modified_value=self.display_modified_value,
|
||||||
formatter_class=self.formatter_class,
|
formatter_class=self.formatter_class,
|
||||||
epilog=self.epilog,
|
epilog=self.epilog,
|
||||||
description=self.description,
|
description=self.description,
|
||||||
@ -560,6 +569,7 @@ class TiramisuCmdlineParser(ArgumentParser):
|
|||||||
root=self.root,
|
root=self.root,
|
||||||
fullpath=self.fullpath,
|
fullpath=self.fullpath,
|
||||||
remove_empty_od=self.remove_empty_od,
|
remove_empty_od=self.remove_empty_od,
|
||||||
|
display_modified_value=self.display_modified_value,
|
||||||
formatter_class=self.formatter_class,
|
formatter_class=self.formatter_class,
|
||||||
epilog=self.epilog,
|
epilog=self.epilog,
|
||||||
description=self.description,
|
description=self.description,
|
||||||
|
Reference in New Issue
Block a user