--root.v => -v, --int ROOT.INT => --int INT

This commit is contained in:
2019-04-01 21:08:41 +02:00
parent 708e623107
commit f90c02282a
2 changed files with 317 additions and 15 deletions

View File

@ -7,7 +7,7 @@ from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \
SymLinkOption, OptionDescription, Config
def get_config():
def get_config(has_tree=False):
choiceoption = ChoiceOption('cmd',
'choice the sub argument',
('str', 'list', 'int', 'none'),
@ -39,18 +39,25 @@ def get_config():
'expected': 'int',
'action': 'disabled',
'inverse': True}])
config = Config(OptionDescription('root',
'root',
[choiceoption,
booloption,
short_booloption,
str_,
list_,
int_
]))
root = OptionDescription('root',
'root',
[choiceoption,
booloption,
short_booloption,
str_,
list_,
int_
])
if has_tree:
root = OptionDescription('root',
'root',
[root])
config = Config(root)
config.property.read_write()
return config
def test_readme_help():
output = """usage: prog.py [-h] [-v] {str,list,int,none}
@ -68,6 +75,40 @@ optional arguments:
assert f.getvalue() == output
def test_readme_help_tree():
output = """usage: prog.py [-h] [-v] {str,list,int,none}
optional arguments:
-h, --help show this help message and exit
root:
{str,list,int,none} choice the sub argument
-v, --root.verbosity increase output verbosity
"""
parser = TiramisuCmdlineParser(get_config(True), 'prog.py')
f = StringIO()
with redirect_stdout(f):
parser.print_help()
assert f.getvalue() == output
def test_readme_help_tree_flatten():
output = """usage: prog.py [-h] [-v] {str,list,int,none}
optional arguments:
-h, --help show this help message and exit
root:
{str,list,int,none} choice the sub argument
-v, --verbosity increase output verbosity
"""
parser = TiramisuCmdlineParser(get_config(True), 'prog.py', fullpath=False)
f = StringIO()
with redirect_stdout(f):
parser.print_help()
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
@ -84,6 +125,38 @@ prog.py: error: the following arguments are required: cmd
assert f.getvalue() == output
def test_readme_positional_mandatory_tree():
output = """usage: prog.py [-h] [-v] {str,list,int,none}
prog.py: error: the following arguments are required: root.cmd
"""
parser = TiramisuCmdlineParser(get_config(True), 'prog.py')
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args([])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
def test_readme_positional_mandatory_tree_flatten():
output = """usage: prog.py [-h] [-v] {str,list,int,none}
prog.py: error: the following arguments are required: cmd
"""
parser = TiramisuCmdlineParser(get_config(True), 'prog.py', fullpath=False)
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args([])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
def test_readme_mandatory():
output = """usage: prog.py [-h] [-v] --str STR {str,list,int,none}
prog.py: error: the following arguments are required: --str
@ -100,6 +173,38 @@ prog.py: error: the following arguments are required: --str
assert f.getvalue() == output
def test_readme_mandatory_tree():
output = """usage: prog.py [-h] [-v] --root.str STR {str,list,int,none}
prog.py: error: the following arguments are required: --root.str
"""
parser = TiramisuCmdlineParser(get_config(True), 'prog.py')
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():
output = """usage: prog.py [-h] [-v] --str STR {str,list,int,none}
prog.py: error: the following arguments are required: --str
"""
parser = TiramisuCmdlineParser(get_config(True), 'prog.py', fullpath=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():
output = """usage: prog.py [-h] [-v] {str,list,int,none}
prog.py: error: unrecognized arguments: --int
@ -116,6 +221,38 @@ prog.py: error: unrecognized arguments: --int
assert f.getvalue() == output
def test_readme_cross_tree():
output = """usage: prog.py [-h] [-v] {str,list,int,none}
prog.py: error: unrecognized arguments: --int
"""
parser = TiramisuCmdlineParser(get_config(True), 'prog.py')
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():
output = """usage: prog.py [-h] [-v] {str,list,int,none}
prog.py: error: unrecognized arguments: --int
"""
parser = TiramisuCmdlineParser(get_config(True), 'prog.py', fullpath=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_int():
output = {'cmd': 'int',
'int': 3,
@ -124,7 +261,29 @@ def test_readme_int():
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['int', '--int', '3'])
# assert config.value.dict() == output
assert config.value.dict() == output
def test_readme_int_tree():
output = {'root.cmd': 'int',
'root.int': 3,
'root.verbosity': False,
'root.v': False}
config = get_config(True)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['int', '--root.int', '3'])
assert config.value.dict() == output
def test_readme_int_tree_flatten():
output = {'root.cmd': 'int',
'root.int': 3,
'root.verbosity': False,
'root.v': False}
config = get_config(True)
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
parser.parse_args(['int', '--int', '3'])
assert config.value.dict() == output
def test_readme_int_verbosity():
@ -138,6 +297,28 @@ def test_readme_int_verbosity():
assert config.value.dict() == output
def test_readme_int_verbosity_tree():
output = {'root.cmd': 'int',
'root.int': 3,
'root.verbosity': True,
'root.v': True}
config = get_config(True)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['int', '--root.int', '3', '--root.verbosity'])
assert config.value.dict() == output
def test_readme_int_verbosity_tree_flatten():
output = {'root.cmd': 'int',
'root.int': 3,
'root.verbosity': True,
'root.v': True}
config = get_config(True)
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
parser.parse_args(['int', '--int', '3', '--verbosity'])
assert config.value.dict() == output
def test_readme_int_verbosity_short():
output = {'cmd': 'int',
'int': 3,
@ -149,6 +330,28 @@ def test_readme_int_verbosity_short():
assert config.value.dict() == output
def test_readme_int_verbosity_short_tree():
output = {'root.cmd': 'int',
'root.int': 3,
'root.verbosity': True,
'root.v': True}
config = get_config(True)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['int', '--root.int', '3', '-v'])
assert config.value.dict() == output
def test_readme_int_verbosity_short_tree_flatten():
output = {'root.cmd': 'int',
'root.int': 3,
'root.verbosity': True,
'root.v': True}
config = get_config(True)
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
parser.parse_args(['int', '--int', '3', '-v'])
assert config.value.dict() == output
def test_readme_str():
output = {'cmd': 'str',
'str': 'value',
@ -160,6 +363,28 @@ def test_readme_str():
assert config.value.dict() == output
def test_readme_str_tree():
output = {'root.cmd': 'str',
'root.str': 'value',
'root.verbosity': False,
'root.v': False}
config = get_config(True)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['str', '--root.str', 'value'])
assert config.value.dict() == output
def test_readme_str_tree_flatten():
output = {'root.cmd': 'str',
'root.str': 'value',
'root.verbosity': False,
'root.v': False}
config = get_config(True)
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
parser.parse_args(['str', '--str', 'value'])
assert config.value.dict() == output
def test_readme_str_int():
output = {'cmd': 'str',
'str': '3',
@ -171,6 +396,28 @@ def test_readme_str_int():
assert config.value.dict() == output
def test_readme_str_int_tree():
output = {'root.cmd': 'str',
'root.str': '3',
'root.verbosity': False,
'root.v': False}
config = get_config(True)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['str', '--root.str', '3'])
assert config.value.dict() == output
def test_readme_str_int_tree_flatten():
output = {'root.cmd': 'str',
'root.str': '3',
'root.verbosity': False,
'root.v': False}
config = get_config(True)
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
parser.parse_args(['str', '--str', '3'])
assert config.value.dict() == output
def test_readme_list():
output = {'cmd': 'list',
'list': ['a', 'b', 'c'],
@ -182,6 +429,28 @@ def test_readme_list():
assert config.value.dict() == output
def test_readme_list_tree():
output = {'root.cmd': 'list',
'root.list': ['a', 'b', 'c'],
'root.verbosity': False,
'root.v': False}
config = get_config(True)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['list', '--root.list', 'a', 'b', 'c'])
assert config.value.dict() == output
def test_readme_list_tree_flatten():
output = {'root.cmd': 'list',
'root.list': ['a', 'b', 'c'],
'root.verbosity': False,
'root.v': False}
config = get_config(True)
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
parser.parse_args(['list', '--list', 'a', 'b', 'c'])
assert config.value.dict() == output
def test_readme_list_uniq():
output = {'cmd': 'list',
'list': ['a'],
@ -191,3 +460,25 @@ def test_readme_list_uniq():
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['list', '--list', 'a'])
assert config.value.dict() == output
def test_readme_list_uniq_tree():
output = {'root.cmd': 'list',
'root.list': ['a'],
'root.verbosity': False,
'root.v': False}
config = get_config(True)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['list', '--root.list', 'a'])
assert config.value.dict() == output
def test_readme_list_uniq_tree_flatten():
output = {'root.cmd': 'list',
'root.list': ['a'],
'root.verbosity': False,
'root.v': False}
config = get_config(True)
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
parser.parse_args(['list', '--list', 'a'])
assert config.value.dict() == output