This commit is contained in:
2019-07-29 22:10:40 +02:00
parent 79ddb8bc11
commit 7fe8cf322d
40 changed files with 30 additions and 22 deletions

View File

@ -0,0 +1 @@
parser.add_argument("echo", help="echo the string you use here")

View File

@ -0,0 +1 @@
parser.add_arguments(StrOption('echo', 'echo the string you use here', properties=('mandatory', 'positional')))

View File

@ -0,0 +1 @@
parser.add_argument("echo", help="echo the string you use here", default='blah', nargs='?')

View File

@ -0,0 +1 @@
parser.add_arguments(StrOption('echo', 'echo the string you use here', properties=('mandatory', 'positional'), default='blah'))

View File

@ -0,0 +1,2 @@
parser.add_argument("square", help="display a square of a given number",
type=int)

View File

@ -0,0 +1 @@
parser.add_arguments(IntOption('square', 'display a square of a given number', properties=('mandatory', 'positional')))

View File

@ -0,0 +1 @@
parser.add_argument("echo", help="echo the string you use here", nargs='+')

View File

@ -0,0 +1 @@
parser.add_arguments(StrOption('echo', 'echo the string you use here', properties=('mandatory', 'positional'), multi=True))

View File

@ -0,0 +1 @@
parser.add_argument('--verbosity', help='increase output verbosity', action='store_true')

View File

@ -0,0 +1 @@
parser.add_arguments(BoolOption('verbosity', 'increase output verbosity', default=False))

View File

@ -0,0 +1 @@
parser.add_argument('--verbosity', help='increase output verbosity', action='store_false')

View File

@ -0,0 +1 @@
parser.add_arguments(BoolOption('verbosity', 'increase output verbosity', default=True))

View File

@ -0,0 +1,2 @@
parser.add_argument('--door', help='Door numbers', choices=['1', '2', '3'])

View File

@ -0,0 +1 @@
parser.add_arguments(ChoiceOption('door', 'Door numbers', ('1', '2', '3')))

View File

@ -0,0 +1,2 @@
parser.add_argument('--int', help='integer', type=int)

View File

@ -0,0 +1 @@
parser.add_arguments(IntOption('int', 'integer'))

View File

@ -0,0 +1,2 @@
parser.add_argument('--foo', help='foo help')

View File

@ -0,0 +1,2 @@
parser.add_arguments(StrOption('foo', 'foo help'))

View File

@ -0,0 +1,2 @@
parser.add_argument('--foo', help='foo help', nargs='*')

View File

@ -0,0 +1,2 @@
parser.add_arguments(StrOption('foo', 'foo help', multi=True))

View File

@ -0,0 +1,2 @@
parser.add_argument('--door', help='Door numbers', choices=[1, 2, 3])

View File

@ -0,0 +1 @@
parser.add_arguments(ChoiceOption('door', 'Door numbers', (1, 2, 3)))

View File

@ -0,0 +1,2 @@
parser.add_argument('--foo', help='foo help', default='default', nargs='?')

View File

@ -0,0 +1,2 @@
parser.add_arguments(StrOption('foo', 'foo help', 'default'))

View File

@ -0,0 +1 @@
parser.add_argument('-f', '--foo', help='foo help')

View File

@ -0,0 +1,3 @@
str_long = StrOption('foo', 'foo help')
str_short = SymLinkOption('f', str_long)
parser.add_arguments([str_long, str_short])

View File

@ -0,0 +1,2 @@
parser.add_argument('-v', help='increase output verbosity', action='store_true')
parser.add_argument('-s', help='second argument', action='store_true')

View File

@ -0,0 +1 @@
parser.add_arguments([BoolOption('v', 'increase output verbosity', default=False), BoolOption('s', 'second argument', default=False)])

View File

@ -0,0 +1,3 @@
parser.add_argument("echo", help="echo the string you use here")
parser.add_argument('--verbosity', help='increase output verbosity', action='store_true')

View File

@ -0,0 +1,3 @@
parser.add_arguments([StrOption('echo', 'echo the string you use here', properties=('mandatory', 'positional')),
BoolOption('verbosity', 'increase output verbosity', default=False)])

View File

@ -0,0 +1,2 @@
parser.add_argument('-v', help='increase output verbosity', action='store_true')
parser.add_argument('-i', '--int', help='integer', type=int)

View File

@ -0,0 +1,4 @@
int_long = IntOption('int', 'integer')
parser.add_arguments([BoolOption('v', 'increase output verbosity', default=False),
int_long,
SymLinkOption('i', int_long)])

95
tests/test_help.py Normal file
View File

@ -0,0 +1,95 @@
from io import StringIO
from contextlib import redirect_stdout, redirect_stderr
import pytest
from argparse import RawDescriptionHelpFormatter
from tiramisu_cmdline_parser import TiramisuCmdlineParser
from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \
SymLinkOption, OptionDescription, Config
from tiramisu_api import Config as JsonConfig
def get_config(json):
choiceoption = ChoiceOption('cmd',
'choice the sub argument',
('str', 'list', 'int', 'none'),
properties=('mandatory',
'positional'))
od = OptionDescription('od',
'od',
[choiceoption])
root = OptionDescription('root',
'root',
[od])
config = Config(root)
config.property.read_write()
if json == 'tiramisu':
return config
jconfig = JsonConfig(config.option.dict())
return jconfig
@pytest.fixture(params=['tiramisu', 'tiramisu-json'])
def json(request):
return request.param
def test_help(json):
output = """usage: prog.py [-h] {str,list,int,none}
optional arguments:
-h, --help show this help message and exit
od:
od
{str,list,int,none} choice the sub argument
"""
parser = TiramisuCmdlineParser(get_config(json), 'prog.py')
f = StringIO()
with redirect_stdout(f):
parser.print_help()
assert f.getvalue() == output
def test_help_epilog(json):
output = """usage: prog.py [-h] {str,list,int,none}
optional arguments:
-h, --help show this help message and exit
od:
od
{str,list,int,none} choice the sub argument
two line
"""
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', epilog="\ntwo\nline")
f = StringIO()
with redirect_stdout(f):
parser.print_help()
assert f.getvalue() == output
def test_help_epilog_raw(json):
output = """usage: prog.py [-h] {str,list,int,none}
optional arguments:
-h, --help show this help message and exit
od:
od
{str,list,int,none} choice the sub argument
two
line
"""
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', epilog="\ntwo\nline", formatter_class=RawDescriptionHelpFormatter)
f = StringIO()
with redirect_stdout(f):
parser.print_help()
assert f.getvalue() == output

345
tests/test_leadership.py Normal file
View File

@ -0,0 +1,345 @@
from io import StringIO
from contextlib import redirect_stdout, redirect_stderr
import pytest
from tiramisu_cmdline_parser import TiramisuCmdlineParser
from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \
SymLinkOption, OptionDescription, Leadership, Config, submulti
from tiramisu_api import Config as JsonConfig
def get_config(json, with_mandatory=False):
leader = StrOption('leader', "Leader var", ['192.168.0.1'], multi=True)
follower = StrOption('follower', "Follower", multi=True)
if with_mandatory:
properties = ('mandatory',)
else:
properties = None
follower_submulti = StrOption('follower_submulti', "Follower submulti", multi=submulti, properties=properties)
follower_integer = IntOption('follower_integer', "Follower integer", multi=True)
follower_boolean = BoolOption('follower_boolean', "Follower boolean", multi=True)
follower_choice = ChoiceOption('follower_choice', "Follower choice", ('opt1', 'opt2'), multi=True)
opt_list = [leader, follower, follower_submulti, follower_integer, follower_boolean, follower_choice]
if with_mandatory:
opt_list.append(StrOption('follower_mandatory', "Follower mandatory", multi=True, properties=('mandatory',)))
leadership = Leadership('leader', 'leader', opt_list)
config = Config(OptionDescription('root', 'root', [leadership]))
if json == 'tiramisu':
return config
jconfig = JsonConfig(config.option.dict())
return jconfig
@pytest.fixture(params=['tiramisu', 'tiramisu-api'])
def json(request):
return request.param
def test_leadership_help(json):
output = """usage: prog.py [-h] [--leader.leader [LEADER [LEADER ...]]]
[--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
optional arguments:
-h, --help show this help message and exit
leader:
leader
--leader.leader [LEADER [LEADER ...]]
Leader var
--leader.pop-leader INDEX
--leader.follower INDEX [FOLLOWER]
Follower
--leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...]
Follower submulti
--leader.follower_integer INDEX [FOLLOWER_INTEGER]
Follower integer
--leader.follower_boolean INDEX
Follower boolean
--leader.no-follower_boolean INDEX
--leader.follower_choice INDEX [{opt1,opt2}]
Follower choice
--leader.follower_mandatory INDEX FOLLOWER_MANDATORY
Follower mandatory
"""
parser = TiramisuCmdlineParser(get_config(json, with_mandatory=True), 'prog.py')
f = StringIO()
with redirect_stdout(f):
parser.print_help()
assert f.getvalue() == output
def test_leadership_modif_leader(json):
output = {'leader.leader': ['192.168.1.1'],
'leader.follower': [None],
'leader.follower_boolean': [None],
'leader.follower_choice': [None],
'leader.follower_integer': [None],
'leader.follower_submulti': [[]]}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--leader.leader', '192.168.1.1'])
assert config.value.dict() == output
def test_leadership_modif_follower(json):
output = {'leader.leader': ['192.168.0.1'],
'leader.follower': ['255.255.255.0'],
'leader.follower_boolean': [None],
'leader.follower_choice': [None],
'leader.follower_integer': [None],
'leader.follower_submulti': [[]]}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--leader.follower', '0', '255.255.255.0'])
assert config.value.dict() == output
def test_leadership_modif_follower_not_submulti(json):
output = """usage: prog.py [-h] [--leader.leader [LEADER [LEADER ...]]]
[--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}]]
prog.py: error: unrecognized arguments: 255.255.255.0
"""
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['--leader.follower', '0', '255.255.255.0', '255.255.255.0'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
def test_leadership_modif_follower_submulti(json):
output = {'leader.leader': ['192.168.0.1'],
'leader.follower': [None],
'leader.follower_boolean': [None],
'leader.follower_choice': [None],
'leader.follower_integer': [None],
'leader.follower_submulti': [['255.255.255.0']]}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--leader.follower_submulti', '0', '255.255.255.0'])
assert config.value.dict() == output
def test_leadership_modif_follower_submulti_multi(json):
output = {'leader.leader': ['192.168.0.1'],
'leader.follower': [None],
'leader.follower_boolean': [None],
'leader.follower_choice': [None],
'leader.follower_integer': [None],
'leader.follower_submulti': [['255.255.255.0', '255.255.255.128']]}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--leader.follower_submulti', '0', '255.255.255.0', '255.255.255.128'])
assert config.value.dict() == output
def test_leadership_modif_follower_bool_true(json):
output = {'leader.leader': ['192.168.0.1'],
'leader.follower': [None],
'leader.follower_boolean': [True],
'leader.follower_choice': [None],
'leader.follower_integer': [None],
'leader.follower_submulti': [[]]}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--leader.follower_boolean', '0'])
assert config.value.dict() == output
def test_leadership_modif_follower_bool_false(json):
output = {'leader.leader': ['192.168.0.1'],
'leader.follower': [None],
'leader.follower_boolean': [False],
'leader.follower_choice': [None],
'leader.follower_integer': [None],
'leader.follower_submulti': [[]]}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--leader.no-follower_boolean', '0'])
assert config.value.dict() == output
def test_leadership_modif_follower_choice(json):
output = {'leader.leader': ['192.168.0.1'],
'leader.follower': [None],
'leader.follower_boolean': [None],
'leader.follower_choice': ['opt1'],
'leader.follower_integer': [None],
'leader.follower_submulti': [[]]}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--leader.follower_choice', '0', 'opt1'])
assert config.value.dict() == output
def test_leadership_modif_follower_choice_unknown(json):
output = """usage: prog.py [-h] [--leader.leader [LEADER [LEADER ...]]]
[--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}]]
prog.py: error: invalid choice: 'opt_unknown' (choose from 'opt1', 'opt2')
"""
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['--leader.follower_choice', '0', 'opt_unknown'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
def test_leadership_modif_follower_not_number(json):
output = """usage: prog.py [-h] [--leader.leader [LEADER [LEADER ...]]]
[--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}]]
prog.py: error: index must be a number, not a
"""
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['--leader.follower', 'a', '255.255.255.0'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
def test_leadership_modif_multi(json):
output = {'leader.leader': ['192.168.1.1', '10.253.10.1', '192.168.253.1'],
'leader.follower': ['255.255.255.128', None, '255.255.255.0'],
'leader.follower_boolean': [None, None, None],
'leader.follower_choice': [None, None, None],
'leader.follower_integer': [None, None, None],
'leader.follower_submulti': [[], [], []]}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--leader.leader', '192.168.1.1', '10.253.10.1', '192.168.253.1',
'--leader.follower', '0', '255.255.255.128',
'--leader.follower', '2', '255.255.255.0'])
assert config.value.dict() == output
def test_leadership_modif_multi_reduce(json):
output = {'leader.leader': ['192.168.1.1', '192.168.253.1'],
'leader.follower': ['255.255.255.128', '255.255.255.0'],
'leader.follower_boolean': [None, None],
'leader.follower_choice': [None, None],
'leader.follower_integer': [None, None],
'leader.follower_submulti': [[], []]}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--leader.leader', '192.168.1.1', '10.253.10.1', '192.168.253.1',
'--leader.follower', '0', '255.255.255.128',
'--leader.follower', '2', '255.255.255.0',
'--leader.pop-leader', '1'])
assert config.value.dict() == output
def test_leadership_modif_mandatory(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.leader [LEADER [LEADER ...]]]
[--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')
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],
'leader.follower_mandatory': [None],
'leader.follower_boolean': [None],
'leader.follower_choice': [None],
'leader.follower_integer': [None],
'leader.follower_submulti': [[]]}
config = get_config(json, with_mandatory=True)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--leader.leader', '192.168.1.1'], valid_mandatory=False)
assert config.value.dict() == output

View File

@ -0,0 +1,189 @@
from io import StringIO
from contextlib import redirect_stdout, redirect_stderr
import pytest
from tiramisu_cmdline_parser import TiramisuCmdlineParser
from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \
SymLinkOption, OptionDescription, Config
from tiramisu_api import Config as JsonConfig
def get_config(json, has_tree=False, default_verbosity=False, add_long=False, add_store_false=False, empty_optiondescription=False):
choiceoption = ChoiceOption('cmd',
'choice the sub argument',
('str', 'list', 'int', 'none'),
properties=('mandatory',
'positional'))
booloption = BoolOption('verbosity',
'increase output verbosity',
default=default_verbosity)
short_booloption = SymLinkOption('v', booloption)
od0 = OptionDescription('od0',
'Sub-Tree 1',
[choiceoption,
booloption,
short_booloption,
])
if empty_optiondescription:
descr = None
else:
descr = 'First OptionDescription'
od1 = OptionDescription('od1',
descr,
[od0])
before = StrOption('before',
'Before',
properties=('mandatory',))
after = StrOption('after',
'After',
properties=('mandatory',))
str_ = StrOption('str',
'string option 2',
properties=('mandatory',))
subtree = OptionDescription('subtree',
'Sub-Tree 2',
[str_])
od2 = OptionDescription('od2',
None,
[before, subtree, after])
root = OptionDescription('root',
'root',
[od1, od2])
config = Config(root)
config.property.read_write()
if json == 'tiramisu':
return config
jconfig = JsonConfig(config.option.dict())
return jconfig
@pytest.fixture(params=['tiramisu', 'tiramisu-json'])
def json(request):
return request.param
def test_optiondescription_help(json):
output = """usage: prog.py [-h] [-v] [-nv] --od2.subtree.str STR --od2.before BEFORE
--od2.after AFTER
{str,list,int,none}
optional arguments:
-h, --help show this help message and exit
od1:
First OptionDescription
od1.od0:
Sub-Tree 1
{str,list,int,none} choice the sub argument
-v, --od1.od0.verbosity
increase output verbosity
-nv, --od1.od0.no-verbosity
od2:
--od2.before BEFORE Before
--od2.after AFTER After
od2.subtree:
Sub-Tree 2
--od2.subtree.str STR
string option 2
"""
parser = TiramisuCmdlineParser(get_config(json), 'prog.py')
f = StringIO()
with redirect_stdout(f):
parser.print_help()
assert f.getvalue() == output
def test_optiondescription_help_remove_empty_od(json):
output = """usage: prog.py [-h] [-v] [-nv] --od2.subtree.str STR --od2.before BEFORE
--od2.after AFTER
{str,list,int,none}
optional arguments:
-h, --help show this help message and exit
od1.od0:
Sub-Tree 1
{str,list,int,none} choice the sub argument
-v, --od1.od0.verbosity
increase output verbosity
-nv, --od1.od0.no-verbosity
od2:
--od2.before BEFORE Before
--od2.after AFTER After
od2.subtree:
Sub-Tree 2
--od2.subtree.str STR
string option 2
"""
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', remove_empty_od=True)
f = StringIO()
with redirect_stdout(f):
parser.print_help()
assert f.getvalue() == output
def test_optiondescription_help_remove_empty_description_od(json):
output = """usage: prog.py [-h] [-v] [-nv] --od2.subtree.str STR --od2.before BEFORE
--od2.after AFTER
{str,list,int,none}
optional arguments:
-h, --help show this help message and exit
od1.od0:
Sub-Tree 1
{str,list,int,none} choice the sub argument
-v, --od1.od0.verbosity
increase output verbosity
-nv, --od1.od0.no-verbosity
od2:
--od2.before BEFORE Before
--od2.after AFTER After
od2.subtree:
Sub-Tree 2
--od2.subtree.str STR
string option 2
"""
parser = TiramisuCmdlineParser(get_config(json, empty_optiondescription=True), 'prog.py')
f = StringIO()
with redirect_stdout(f):
parser.print_help()
assert f.getvalue() == output
def test_optiondescription_help_subtree(json):
output = """usage: prog.py [-h] --od2.subtree.str STR --od2.before BEFORE --od2.after
AFTER
optional arguments:
-h, --help show this help message and exit
--od2.before BEFORE Before
--od2.after AFTER After
od2.subtree:
Sub-Tree 2
--od2.subtree.str STR
string option 2
"""
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py', root='od2')
f = StringIO()
with redirect_stdout(f):
parser.print_help()
assert f.getvalue() == output

683
tests/test_readme.py Normal file
View File

@ -0,0 +1,683 @@
from io import StringIO
from contextlib import redirect_stdout, redirect_stderr
import pytest
from tiramisu_cmdline_parser import TiramisuCmdlineParser
from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \
SymLinkOption, OptionDescription, Config
from tiramisu_api import Config as JsonConfig
def get_config(json, has_tree=False, default_verbosity=False, add_long=False, add_store_false=False):
choiceoption = ChoiceOption('cmd',
'choice the sub argument',
('str', 'list', 'int', 'none'),
properties=('mandatory',
'positional'))
booloption = BoolOption('verbosity',
'increase output verbosity',
default=default_verbosity)
short_booloption = SymLinkOption('v', booloption)
str_ = StrOption('str',
'string option',
properties=('mandatory',),
requires=[{'option': choiceoption,
'expected': 'str',
'action': 'disabled',
'inverse': True}])
list_ = StrOption('list',
'list string option',
multi=True,
properties=('mandatory',),
requires=[{'option': choiceoption,
'expected': 'list',
'action': 'disabled',
'inverse': True}])
int_ = IntOption('int',
'int option',
properties=('mandatory',),
requires=[{'option': choiceoption,
'expected': 'int',
'action': 'disabled',
'inverse': True}])
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()
if add_store_false:
config.option('verbosity').property.add('storefalse')
if add_long:
config.option('verbosity').property.add('longargument')
if json == 'tiramisu':
return config
jconfig = JsonConfig(config.option.dict())
return jconfig
@pytest.fixture(params=['tiramisu', 'tiramisu-json'])
def json(request):
return request.param
def test_readme_help(json):
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
positional arguments:
{str,list,int,none} choice the sub argument
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')
f = StringIO()
with redirect_stdout(f):
parser.print_help()
assert f.getvalue() == output
def test_readme_help_tree(json):
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
optional arguments:
-h, --help show this help message and exit
root:
root
{str,list,int,none} choice the sub argument
-v, --root.verbosity increase output verbosity
-nv, --root.no-verbosity
"""
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py')
f = StringIO()
with redirect_stdout(f):
parser.print_help()
assert f.getvalue() == output
def test_readme_help_tree_flatten(json):
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
optional arguments:
-h, --help show this help message and exit
root:
root
{str,list,int,none} choice the sub argument
-v, --verbosity increase output verbosity
-nv, --no-verbosity
"""
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False)
f = StringIO()
with redirect_stdout(f):
parser.print_help()
assert f.getvalue() == output
def test_readme_help_modif_positional(json):
output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR {str,list,int,none}
positional arguments:
{str,list,int,none} choice the sub argument
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')
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}
positional arguments:
{str,list,int,none} choice the sub argument
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')
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_short1(json):
output = """usage: prog.py "str" -v [-h] [-v] [-nv] --str STR {str,list,int,none}
positional arguments:
{str,list,int,none} choice the sub argument
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')
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}
positional arguments:
{str,list,int,none} choice the sub argument
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')
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
"""
parser = TiramisuCmdlineParser(get_config(json), '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(json):
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: the following arguments are required: root.cmd
"""
parser = TiramisuCmdlineParser(get_config(json, 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(json):
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: the following arguments are required: cmd
"""
parser = TiramisuCmdlineParser(get_config(json, 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(json):
output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR {str,list,int,none}
prog.py: error: the following arguments are required: --str
"""
parser = TiramisuCmdlineParser(get_config(json), '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(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
"""
parser = TiramisuCmdlineParser(get_config(json, 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(json):
output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR {str,list,int,none}
prog.py: error: the following arguments are required: --str
"""
parser = TiramisuCmdlineParser(get_config(json, 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(json):
output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: unrecognized arguments: --int
"""
parser = TiramisuCmdlineParser(get_config(json), '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(json):
output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: unrecognized arguments: --int
"""
parser = TiramisuCmdlineParser(get_config(json, 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(json):
output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: unrecognized arguments: --int
"""
parser = TiramisuCmdlineParser(get_config(json, 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_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')
"""
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False)
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['unknown'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
def test_readme_int(json):
output = {'cmd': 'int',
'int': 3,
'verbosity': False,
'v': False}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['int', '--int', '3'])
assert config.value.dict() == output
def test_readme_int_tree(json):
output = {'root.cmd': 'int',
'root.int': 3,
'root.verbosity': False,
'root.v': False}
config = get_config(json, True)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['int', '--root.int', '3'])
assert config.value.dict() == output
def test_readme_int_tree_flatten(json):
output = {'root.cmd': 'int',
'root.int': 3,
'root.verbosity': False,
'root.v': False}
config = get_config(json, True)
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
parser.parse_args(['int', '--int', '3'])
assert config.value.dict() == output
def test_readme_int_verbosity(json):
output = {'cmd': 'int',
'int': 3,
'verbosity': True,
'v': True}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['int', '--int', '3', '--verbosity'])
assert config.value.dict() == output
def test_readme_int_verbosity_tree(json):
output = {'root.cmd': 'int',
'root.int': 3,
'root.verbosity': True,
'root.v': True}
config = get_config(json, 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(json):
output = {'root.cmd': 'int',
'root.int': 3,
'root.verbosity': True,
'root.v': True}
config = get_config(json, 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(json):
output = {'cmd': 'int',
'int': 3,
'verbosity': True,
'v': True}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['int', '--int', '3', '-v'])
assert config.value.dict() == output
def test_readme_int_verbosity_short_store_false(json):
output = {'cmd': 'int',
'int': 3,
'verbosity': None,
'v': True}
config = get_config(json, default_verbosity=None, add_store_false=True)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['int', '--int', '3', '-v'])
output = {'cmd': 'int',
'int': 3,
'verbosity': False,
'v': False}
assert config.value.dict() == output
parser.parse_args(['int', '--int', '3', '-nv'])
output = {'cmd': 'int',
'int': 3,
'verbosity': True,
'v': True}
assert config.value.dict() == output
def test_readme_int_verbosity_short_no(json):
output = {'cmd': 'int',
'int': 3,
'verbosity': False,
'v': False}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['int', '--int', '3', '-nv'])
assert config.value.dict() == output
def test_readme_int_verbosity_short_tree(json):
output = {'root.cmd': 'int',
'root.int': 3,
'root.verbosity': True,
'root.v': True}
config = get_config(json, 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(json):
output = {'root.cmd': 'int',
'root.int': 3,
'root.verbosity': True,
'root.v': True}
config = get_config(json, True)
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
parser.parse_args(['int', '--int', '3', '-v'])
assert config.value.dict() == output
def test_readme_int_verbosity_short_and_not(json):
output = {'cmd': 'int',
'int': 3,
'verbosity': False,
'v': False}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['int', '--int', '3', '-v', '-nv'])
assert config.value.dict() == output
def test_readme_str(json):
output = {'cmd': 'str',
'str': 'value',
'verbosity': False,
'v': False}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['str', '--str', 'value'])
assert config.value.dict() == output
def test_readme_str_tree(json):
output = {'root.cmd': 'str',
'root.str': 'value',
'root.verbosity': False,
'root.v': False}
config = get_config(json, True)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['str', '--root.str', 'value'])
assert config.value.dict() == output
def test_readme_str_tree_flatten(json):
output = {'root.cmd': 'str',
'root.str': 'value',
'root.verbosity': False,
'root.v': False}
config = get_config(json, True)
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
parser.parse_args(['str', '--str', 'value'])
assert config.value.dict() == output
def test_readme_str_int(json):
output = {'cmd': 'str',
'str': '3',
'verbosity': False,
'v': False}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['str', '--str', '3'])
assert config.value.dict() == output
def test_readme_str_int_tree(json):
output = {'root.cmd': 'str',
'root.str': '3',
'root.verbosity': False,
'root.v': False}
config = get_config(json, 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(json):
output = {'root.cmd': 'str',
'root.str': '3',
'root.verbosity': False,
'root.v': False}
config = get_config(json, True)
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
parser.parse_args(['str', '--str', '3'])
assert config.value.dict() == output
def test_readme_list_single(json):
output = {'cmd': 'list',
'list': ['a'],
'verbosity': False,
'v': False}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['list', '--list', 'a'])
assert config.value.dict() == output
def test_readme_list(json):
output = {'cmd': 'list',
'list': ['a', 'b', 'c'],
'verbosity': False,
'v': False}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['list', '--list', 'a', 'b', 'c'])
assert config.value.dict() == output
def test_readme_list_tree(json):
output = {'root.cmd': 'list',
'root.list': ['a', 'b', 'c'],
'root.verbosity': False,
'root.v': False}
config = get_config(json, 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(json):
output = {'root.cmd': 'list',
'root.list': ['a', 'b', 'c'],
'root.verbosity': False,
'root.v': False}
config = get_config(json, 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(json):
output = {'cmd': 'list',
'list': ['a'],
'verbosity': False,
'v': False}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['list', '--list', 'a'])
assert config.value.dict() == output
def test_readme_list_uniq_tree(json):
output = {'root.cmd': 'list',
'root.list': ['a'],
'root.verbosity': False,
'root.v': False}
config = get_config(json, 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(json):
output = {'root.cmd': 'list',
'root.list': ['a'],
'root.verbosity': False,
'root.v': False}
config = get_config(json, True)
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
parser.parse_args(['list', '--list', 'a'])
assert config.value.dict() == output
def test_readme_longargument(json):
output = {'cmd': 'list',
'list': ['a'],
'verbosity': True,
'v': True}
config = get_config(json, add_long=True)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['list', '--list', 'a', '--v'])
assert config.value.dict() == output

204
tests/test_shortarg.py Normal file
View File

@ -0,0 +1,204 @@
from io import StringIO
import pytest
from contextlib import redirect_stderr
from tiramisu_cmdline_parser import TiramisuCmdlineParser
from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \
SymLinkOption, OptionDescription, Config
from tiramisu_api import Config as JsonConfig
@pytest.fixture(params=['tiramisu', 'tiramisu-json'])
def json(request):
return request.param
def test_short(json):
def get_config():
list_ = StrOption('list',
'list string option')
slist_ = SymLinkOption('l', list_)
root = OptionDescription('root',
'root',
[list_,
slist_,
])
config = Config(root)
config.property.read_write()
if json != 'tiramisu':
config = JsonConfig(config.option.dict())
return config
#
output = {'list': None, 'l': None}
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args([])
assert config.value.dict() == output
#
output = {'list': 'a', 'l': 'a'}
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--list', 'a'])
assert config.value.dict() == output
#
output = {'list': 'a', 'l': 'a'}
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['-l', 'a'])
assert config.value.dict() == output
#
assert config.option('list').value.get() == config.option('l').value.get()
assert config.option('list').owner.get() == config.option('l').owner.get()
assert config.option('list').owner.isdefault() == config.option('l').owner.isdefault()
def test_short_mandatory(json):
def get_config():
list_ = StrOption('list',
'list string option',
properties=('mandatory',))
slist_ = SymLinkOption('l', list_)
root = OptionDescription('root',
'root',
[list_,
slist_,
])
config = Config(root)
config.property.read_write()
if json != 'tiramisu':
config = JsonConfig(config.option.dict())
return config
#
output = """usage: prog.py [-h] -l LIST
prog.py: error: the following arguments are required: --list
"""
config = get_config()
parser = TiramisuCmdlineParser(config, '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
#
output = {'list': 'a', 'l': 'a'}
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--list', 'a'])
assert config.value.dict() == output
#
output = {'list': 'a', 'l': 'a'}
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['-l', 'a'])
assert config.value.dict() == output
def test_short_multi(json):
def get_config():
list_ = StrOption('list',
'list string option',
multi=True)
slist_ = SymLinkOption('l', list_)
root = OptionDescription('root',
'root',
[list_,
slist_,
])
config = Config(root)
config.property.read_write()
if json != 'tiramisu':
config = JsonConfig(config.option.dict())
return config
#
output = {'list': [], 'l': []}
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args([])
assert config.value.dict() == output
#
output = {'list': ['a'], 'l': ['a']}
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--list', 'a'])
assert config.value.dict() == output
#
output = {'list': ['a', 'b'], 'l': ['a', 'b']}
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--list', 'a', 'b'])
assert config.value.dict() == output
#
output = {'list': ['a'], 'l': ['a']}
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['-l', 'a'])
assert config.value.dict() == output
#
output = {'list': ['a', 'b'], 'l': ['a', 'b']}
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['-l', 'a', 'b'])
assert config.value.dict() == output
def test_short_multi_mandatory(json):
def get_config():
list_ = StrOption('list',
'list string option',
multi=True,
properties=('mandatory',))
slist_ = SymLinkOption('l', list_)
root = OptionDescription('root',
'root',
[list_,
slist_,
])
config = Config(root)
config.property.read_write()
if json != 'tiramisu':
config = JsonConfig(config.option.dict())
return config
#
output = """usage: prog.py [-h] -l LIST [LIST ...]
prog.py: error: the following arguments are required: --list
"""
config = get_config()
parser = TiramisuCmdlineParser(config, '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
#
output = {'list': ['a'], 'l': ['a']}
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--list', 'a'])
assert config.value.dict() == output
#
output = {'list': ['a', 'b'], 'l': ['a', 'b']}
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--list', 'a', 'b'])
assert config.value.dict() == output
#
output = {'list': ['a'], 'l': ['a']}
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['-l', 'a'])
assert config.value.dict() == output
#
output = {'list': ['a', 'b'], 'l': ['a', 'b']}
config = get_config()
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['-l', 'a', 'b'])
assert config.value.dict() == output