leader/follower support
This commit is contained in:
parent
dd14b0f752
commit
1cb8a2cc7b
4 changed files with 708 additions and 194 deletions
|
@ -118,7 +118,7 @@ def main():
|
||||||
proposal = ProposalOption('guess{}'.format(idx),
|
proposal = ProposalOption('guess{}'.format(idx),
|
||||||
'Guess {}'.format(idx),
|
'Guess {}'.format(idx),
|
||||||
requires=requires,
|
requires=requires,
|
||||||
properties=('positional',))
|
properties=('positional', 'mandatory'))
|
||||||
#FIXME maximum recursion ...
|
#FIXME maximum recursion ...
|
||||||
#if proposals:
|
#if proposals:
|
||||||
# proposal.impl_add_consistency('not_equal', proposals[0])
|
# proposal.impl_add_consistency('not_equal', proposals[0])
|
||||||
|
@ -150,7 +150,7 @@ def main():
|
||||||
config.property.read_write()
|
config.property.read_write()
|
||||||
try:
|
try:
|
||||||
parser = TiramisuCmdlineParser(config)
|
parser = TiramisuCmdlineParser(config)
|
||||||
parser.parse_args()
|
parser.parse_args(valid_mandatory=False)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# if no more suggestion
|
# if no more suggestion
|
||||||
pass
|
pass
|
||||||
|
|
319
test/test_leadership.py
Normal file
319
test/test_leadership.py
Normal file
|
@ -0,0 +1,319 @@
|
||||||
|
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_json_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', '', 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.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 var
|
||||||
|
--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.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.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.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_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.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
|
|
@ -1,13 +1,15 @@
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
from contextlib import redirect_stdout, redirect_stderr
|
from contextlib import redirect_stdout, redirect_stderr
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
from tiramisu_cmdline_parser import TiramisuCmdlineParser
|
from tiramisu_cmdline_parser import TiramisuCmdlineParser
|
||||||
from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \
|
from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \
|
||||||
SymLinkOption, OptionDescription, Config
|
SymLinkOption, OptionDescription, Config
|
||||||
|
from tiramisu_json_api import Config as JsonConfig
|
||||||
|
|
||||||
|
|
||||||
def get_config(has_tree=False, default_verbosity=False):
|
def get_config(json, has_tree=False, default_verbosity=False, add_long=False, add_store_false=False):
|
||||||
choiceoption = ChoiceOption('cmd',
|
choiceoption = ChoiceOption('cmd',
|
||||||
'choice the sub argument',
|
'choice the sub argument',
|
||||||
('str', 'list', 'int', 'none'),
|
('str', 'list', 'int', 'none'),
|
||||||
|
@ -55,10 +57,22 @@ def get_config(has_tree=False, default_verbosity=False):
|
||||||
[root])
|
[root])
|
||||||
config = Config(root)
|
config = Config(root)
|
||||||
config.property.read_write()
|
config.property.read_write()
|
||||||
return config
|
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
|
||||||
|
|
||||||
|
|
||||||
def test_readme_help():
|
@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}
|
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
|
||||||
|
|
||||||
positional arguments:
|
positional arguments:
|
||||||
|
@ -69,14 +83,14 @@ optional arguments:
|
||||||
-v, --verbosity increase output verbosity
|
-v, --verbosity increase output verbosity
|
||||||
-nv, --no-verbosity
|
-nv, --no-verbosity
|
||||||
"""
|
"""
|
||||||
parser = TiramisuCmdlineParser(get_config(), 'prog.py')
|
parser = TiramisuCmdlineParser(get_config(json), 'prog.py')
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
with redirect_stdout(f):
|
with redirect_stdout(f):
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
assert f.getvalue() == output
|
assert f.getvalue() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_help_tree():
|
def test_readme_help_tree(json):
|
||||||
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
|
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
|
||||||
|
|
||||||
optional arguments:
|
optional arguments:
|
||||||
|
@ -87,14 +101,14 @@ root:
|
||||||
-v, --root.verbosity increase output verbosity
|
-v, --root.verbosity increase output verbosity
|
||||||
-nv, --root.no-verbosity
|
-nv, --root.no-verbosity
|
||||||
"""
|
"""
|
||||||
parser = TiramisuCmdlineParser(get_config(True), 'prog.py')
|
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py')
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
with redirect_stdout(f):
|
with redirect_stdout(f):
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
assert f.getvalue() == output
|
assert f.getvalue() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_help_tree_flatten():
|
def test_readme_help_tree_flatten(json):
|
||||||
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
|
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
|
||||||
|
|
||||||
optional arguments:
|
optional arguments:
|
||||||
|
@ -105,14 +119,14 @@ root:
|
||||||
-v, --verbosity increase output verbosity
|
-v, --verbosity increase output verbosity
|
||||||
-nv, --no-verbosity
|
-nv, --no-verbosity
|
||||||
"""
|
"""
|
||||||
parser = TiramisuCmdlineParser(get_config(True), 'prog.py', fullpath=False)
|
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False)
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
with redirect_stdout(f):
|
with redirect_stdout(f):
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
assert f.getvalue() == output
|
assert f.getvalue() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_help_modif_positional():
|
def test_readme_help_modif_positional(json):
|
||||||
output = """usage: prog.py str [-h] [-v] [-nv] --str STR
|
output = """usage: prog.py str [-h] [-v] [-nv] --str STR
|
||||||
|
|
||||||
optional arguments:
|
optional arguments:
|
||||||
|
@ -121,7 +135,7 @@ optional arguments:
|
||||||
-nv, --no-verbosity
|
-nv, --no-verbosity
|
||||||
--str STR string option
|
--str STR string option
|
||||||
"""
|
"""
|
||||||
parser = TiramisuCmdlineParser(get_config(), 'prog.py')
|
parser = TiramisuCmdlineParser(get_config(json), 'prog.py')
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
with redirect_stdout(f):
|
with redirect_stdout(f):
|
||||||
try:
|
try:
|
||||||
|
@ -133,7 +147,7 @@ optional arguments:
|
||||||
assert f.getvalue() == output
|
assert f.getvalue() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_help_modif():
|
def test_readme_help_modif(json):
|
||||||
output = """usage: prog.py str --str toto [-h] [-v] [-nv]
|
output = """usage: prog.py str --str toto [-h] [-v] [-nv]
|
||||||
|
|
||||||
optional arguments:
|
optional arguments:
|
||||||
|
@ -141,7 +155,7 @@ optional arguments:
|
||||||
-v, --verbosity increase output verbosity
|
-v, --verbosity increase output verbosity
|
||||||
-nv, --no-verbosity
|
-nv, --no-verbosity
|
||||||
"""
|
"""
|
||||||
parser = TiramisuCmdlineParser(get_config(), 'prog.py')
|
parser = TiramisuCmdlineParser(get_config(json), 'prog.py')
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
with redirect_stdout(f):
|
with redirect_stdout(f):
|
||||||
try:
|
try:
|
||||||
|
@ -153,14 +167,14 @@ optional arguments:
|
||||||
assert f.getvalue() == output
|
assert f.getvalue() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_help_modif_short():
|
def test_readme_help_modif_short1(json):
|
||||||
output = """usage: prog.py str --verbosity [-h] --str STR
|
output = """usage: prog.py str --verbosity [-h] --str STR
|
||||||
|
|
||||||
optional arguments:
|
optional arguments:
|
||||||
-h, --help show this help message and exit
|
-h, --help show this help message and exit
|
||||||
--str STR string option
|
--str STR string option
|
||||||
"""
|
"""
|
||||||
parser = TiramisuCmdlineParser(get_config(), 'prog.py')
|
parser = TiramisuCmdlineParser(get_config(json), 'prog.py')
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
with redirect_stdout(f):
|
with redirect_stdout(f):
|
||||||
try:
|
try:
|
||||||
|
@ -172,14 +186,14 @@ optional arguments:
|
||||||
assert f.getvalue() == output
|
assert f.getvalue() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_help_modif_short_no():
|
def test_readme_help_modif_short_no(json):
|
||||||
output = """usage: prog.py str --verbosity [-h] --str STR
|
output = """usage: prog.py str --verbosity [-h] --str STR
|
||||||
|
|
||||||
optional arguments:
|
optional arguments:
|
||||||
-h, --help show this help message and exit
|
-h, --help show this help message and exit
|
||||||
--str STR string option
|
--str STR string option
|
||||||
"""
|
"""
|
||||||
parser = TiramisuCmdlineParser(get_config(), 'prog.py')
|
parser = TiramisuCmdlineParser(get_config(json), 'prog.py')
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
with redirect_stdout(f):
|
with redirect_stdout(f):
|
||||||
try:
|
try:
|
||||||
|
@ -191,11 +205,11 @@ optional arguments:
|
||||||
assert f.getvalue() == output
|
assert f.getvalue() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_positional_mandatory():
|
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
|
||||||
"""
|
"""
|
||||||
parser = TiramisuCmdlineParser(get_config(), 'prog.py')
|
parser = TiramisuCmdlineParser(get_config(json), 'prog.py')
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
with redirect_stderr(f):
|
with redirect_stderr(f):
|
||||||
try:
|
try:
|
||||||
|
@ -207,11 +221,11 @@ prog.py: error: the following arguments are required: cmd
|
||||||
assert f.getvalue() == output
|
assert f.getvalue() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_positional_mandatory_tree():
|
def test_readme_positional_mandatory_tree(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: root.cmd
|
prog.py: error: the following arguments are required: root.cmd
|
||||||
"""
|
"""
|
||||||
parser = TiramisuCmdlineParser(get_config(True), 'prog.py')
|
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py')
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
with redirect_stderr(f):
|
with redirect_stderr(f):
|
||||||
try:
|
try:
|
||||||
|
@ -223,11 +237,11 @@ prog.py: error: the following arguments are required: root.cmd
|
||||||
assert f.getvalue() == output
|
assert f.getvalue() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_positional_mandatory_tree_flatten():
|
def test_readme_positional_mandatory_tree_flatten(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
|
||||||
"""
|
"""
|
||||||
parser = TiramisuCmdlineParser(get_config(True), 'prog.py', fullpath=False)
|
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False)
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
with redirect_stderr(f):
|
with redirect_stderr(f):
|
||||||
try:
|
try:
|
||||||
|
@ -239,11 +253,11 @@ prog.py: error: the following arguments are required: cmd
|
||||||
assert f.getvalue() == output
|
assert f.getvalue() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_mandatory():
|
def test_readme_mandatory(json):
|
||||||
output = """usage: prog.py str [-h] [-v] [-nv] --str STR
|
output = """usage: prog.py str [-h] [-v] [-nv] --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(json), 'prog.py')
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
with redirect_stderr(f):
|
with redirect_stderr(f):
|
||||||
try:
|
try:
|
||||||
|
@ -255,11 +269,11 @@ prog.py: error: the following arguments are required: --str
|
||||||
assert f.getvalue() == output
|
assert f.getvalue() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_mandatory_tree():
|
def test_readme_mandatory_tree(json):
|
||||||
output = """usage: prog.py str [-h] [-v] [-nv] --root.str STR
|
output = """usage: prog.py str [-h] [-v] [-nv] --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(json, True), 'prog.py')
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
with redirect_stderr(f):
|
with redirect_stderr(f):
|
||||||
try:
|
try:
|
||||||
|
@ -271,11 +285,11 @@ prog.py: error: the following arguments are required: --root.str
|
||||||
assert f.getvalue() == output
|
assert f.getvalue() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_mandatory_tree_flatten():
|
def test_readme_mandatory_tree_flatten(json):
|
||||||
output = """usage: prog.py str [-h] [-v] [-nv] --str STR
|
output = """usage: prog.py str [-h] [-v] [-nv] --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(json, True), 'prog.py', fullpath=False)
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
with redirect_stderr(f):
|
with redirect_stderr(f):
|
||||||
try:
|
try:
|
||||||
|
@ -287,11 +301,11 @@ prog.py: error: the following arguments are required: --str
|
||||||
assert f.getvalue() == output
|
assert f.getvalue() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_cross():
|
def test_readme_cross(json):
|
||||||
output = """usage: prog.py none [-h] [-v] [-nv]
|
output = """usage: prog.py none [-h] [-v] [-nv]
|
||||||
prog.py: error: unrecognized arguments: --int
|
prog.py: error: unrecognized arguments: --int
|
||||||
"""
|
"""
|
||||||
parser = TiramisuCmdlineParser(get_config(), 'prog.py')
|
parser = TiramisuCmdlineParser(get_config(json), 'prog.py')
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
with redirect_stderr(f):
|
with redirect_stderr(f):
|
||||||
try:
|
try:
|
||||||
|
@ -303,11 +317,11 @@ prog.py: error: unrecognized arguments: --int
|
||||||
assert f.getvalue() == output
|
assert f.getvalue() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_cross_tree():
|
def test_readme_cross_tree(json):
|
||||||
output = """usage: prog.py none [-h] [-v] [-nv]
|
output = """usage: prog.py none [-h] [-v] [-nv]
|
||||||
prog.py: error: unrecognized arguments: --int
|
prog.py: error: unrecognized arguments: --int
|
||||||
"""
|
"""
|
||||||
parser = TiramisuCmdlineParser(get_config(True), 'prog.py')
|
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py')
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
with redirect_stderr(f):
|
with redirect_stderr(f):
|
||||||
try:
|
try:
|
||||||
|
@ -319,11 +333,11 @@ prog.py: error: unrecognized arguments: --int
|
||||||
assert f.getvalue() == output
|
assert f.getvalue() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_cross_tree_flatten():
|
def test_readme_cross_tree_flatten(json):
|
||||||
output = """usage: prog.py none [-h] [-v] [-nv]
|
output = """usage: prog.py none [-h] [-v] [-nv]
|
||||||
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(json, True), 'prog.py', fullpath=False)
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
with redirect_stderr(f):
|
with redirect_stderr(f):
|
||||||
try:
|
try:
|
||||||
|
@ -335,90 +349,105 @@ prog.py: error: unrecognized arguments: --int
|
||||||
assert f.getvalue() == output
|
assert f.getvalue() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_int():
|
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',
|
output = {'cmd': 'int',
|
||||||
'int': 3,
|
'int': 3,
|
||||||
'verbosity': False,
|
'verbosity': False,
|
||||||
'v': False}
|
'v': False}
|
||||||
config = get_config()
|
config = get_config(json)
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py')
|
parser = TiramisuCmdlineParser(config, 'prog.py')
|
||||||
parser.parse_args(['int', '--int', '3'])
|
parser.parse_args(['int', '--int', '3'])
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_int_tree():
|
def test_readme_int_tree(json):
|
||||||
output = {'root.cmd': 'int',
|
output = {'root.cmd': 'int',
|
||||||
'root.int': 3,
|
'root.int': 3,
|
||||||
'root.verbosity': False,
|
'root.verbosity': False,
|
||||||
'root.v': False}
|
'root.v': False}
|
||||||
config = get_config(True)
|
config = get_config(json, True)
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py')
|
parser = TiramisuCmdlineParser(config, 'prog.py')
|
||||||
parser.parse_args(['int', '--root.int', '3'])
|
parser.parse_args(['int', '--root.int', '3'])
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_int_tree_flatten():
|
def test_readme_int_tree_flatten(json):
|
||||||
output = {'root.cmd': 'int',
|
output = {'root.cmd': 'int',
|
||||||
'root.int': 3,
|
'root.int': 3,
|
||||||
'root.verbosity': False,
|
'root.verbosity': False,
|
||||||
'root.v': False}
|
'root.v': False}
|
||||||
config = get_config(True)
|
config = get_config(json, True)
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
|
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
|
||||||
parser.parse_args(['int', '--int', '3'])
|
parser.parse_args(['int', '--int', '3'])
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_int_verbosity():
|
def test_readme_int_verbosity(json):
|
||||||
output = {'cmd': 'int',
|
output = {'cmd': 'int',
|
||||||
'int': 3,
|
'int': 3,
|
||||||
'verbosity': True,
|
'verbosity': True,
|
||||||
'v': True}
|
'v': True}
|
||||||
config = get_config()
|
config = get_config(json)
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py')
|
parser = TiramisuCmdlineParser(config, 'prog.py')
|
||||||
parser.parse_args(['int', '--int', '3', '--verbosity'])
|
parser.parse_args(['int', '--int', '3', '--verbosity'])
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_int_verbosity_tree():
|
def test_readme_int_verbosity_tree(json):
|
||||||
output = {'root.cmd': 'int',
|
output = {'root.cmd': 'int',
|
||||||
'root.int': 3,
|
'root.int': 3,
|
||||||
'root.verbosity': True,
|
'root.verbosity': True,
|
||||||
'root.v': True}
|
'root.v': True}
|
||||||
config = get_config(True)
|
config = get_config(json, True)
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py')
|
parser = TiramisuCmdlineParser(config, 'prog.py')
|
||||||
parser.parse_args(['int', '--root.int', '3', '--root.verbosity'])
|
parser.parse_args(['int', '--root.int', '3', '--root.verbosity'])
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_int_verbosity_tree_flatten():
|
def test_readme_int_verbosity_tree_flatten(json):
|
||||||
output = {'root.cmd': 'int',
|
output = {'root.cmd': 'int',
|
||||||
'root.int': 3,
|
'root.int': 3,
|
||||||
'root.verbosity': True,
|
'root.verbosity': True,
|
||||||
'root.v': True}
|
'root.v': True}
|
||||||
config = get_config(True)
|
config = get_config(json, True)
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
|
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
|
||||||
parser.parse_args(['int', '--int', '3', '--verbosity'])
|
parser.parse_args(['int', '--int', '3', '--verbosity'])
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_int_verbosity_short():
|
def test_readme_int_verbosity_short(json):
|
||||||
output = {'cmd': 'int',
|
output = {'cmd': 'int',
|
||||||
'int': 3,
|
'int': 3,
|
||||||
'verbosity': True,
|
'verbosity': True,
|
||||||
'v': True}
|
'v': True}
|
||||||
config = get_config()
|
config = get_config(json)
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py')
|
parser = TiramisuCmdlineParser(config, 'prog.py')
|
||||||
parser.parse_args(['int', '--int', '3', '-v'])
|
parser.parse_args(['int', '--int', '3', '-v'])
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_int_verbosity_short_store_false():
|
def test_readme_int_verbosity_short_store_false(json):
|
||||||
output = {'cmd': 'int',
|
output = {'cmd': 'int',
|
||||||
'int': 3,
|
'int': 3,
|
||||||
'verbosity': None,
|
'verbosity': None,
|
||||||
'v': True}
|
'v': True}
|
||||||
config = get_config(default_verbosity=None)
|
config = get_config(json, default_verbosity=None, add_store_false=True)
|
||||||
config.option('verbosity').property.add('storefalse')
|
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py')
|
parser = TiramisuCmdlineParser(config, 'prog.py')
|
||||||
parser.parse_args(['int', '--int', '3', '-v'])
|
parser.parse_args(['int', '--int', '3', '-v'])
|
||||||
output = {'cmd': 'int',
|
output = {'cmd': 'int',
|
||||||
|
@ -434,178 +463,188 @@ def test_readme_int_verbosity_short_store_false():
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_int_verbosity_short_no():
|
def test_readme_int_verbosity_short_no(json):
|
||||||
output = {'cmd': 'int',
|
output = {'cmd': 'int',
|
||||||
'int': 3,
|
'int': 3,
|
||||||
'verbosity': False,
|
'verbosity': False,
|
||||||
'v': False}
|
'v': False}
|
||||||
config = get_config()
|
config = get_config(json)
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py')
|
parser = TiramisuCmdlineParser(config, 'prog.py')
|
||||||
parser.parse_args(['int', '--int', '3', '-nv'])
|
parser.parse_args(['int', '--int', '3', '-nv'])
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_int_verbosity_short_tree():
|
def test_readme_int_verbosity_short_tree(json):
|
||||||
output = {'root.cmd': 'int',
|
output = {'root.cmd': 'int',
|
||||||
'root.int': 3,
|
'root.int': 3,
|
||||||
'root.verbosity': True,
|
'root.verbosity': True,
|
||||||
'root.v': True}
|
'root.v': True}
|
||||||
config = get_config(True)
|
config = get_config(json, True)
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py')
|
parser = TiramisuCmdlineParser(config, 'prog.py')
|
||||||
parser.parse_args(['int', '--root.int', '3', '-v'])
|
parser.parse_args(['int', '--root.int', '3', '-v'])
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_int_verbosity_short_tree_flatten():
|
def test_readme_int_verbosity_short_tree_flatten(json):
|
||||||
output = {'root.cmd': 'int',
|
output = {'root.cmd': 'int',
|
||||||
'root.int': 3,
|
'root.int': 3,
|
||||||
'root.verbosity': True,
|
'root.verbosity': True,
|
||||||
'root.v': True}
|
'root.v': True}
|
||||||
config = get_config(True)
|
config = get_config(json, True)
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
|
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
|
||||||
parser.parse_args(['int', '--int', '3', '-v'])
|
parser.parse_args(['int', '--int', '3', '-v'])
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_str():
|
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',
|
output = {'cmd': 'str',
|
||||||
'str': 'value',
|
'str': 'value',
|
||||||
'verbosity': False,
|
'verbosity': False,
|
||||||
'v': False}
|
'v': False}
|
||||||
config = get_config()
|
config = get_config(json)
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py')
|
parser = TiramisuCmdlineParser(config, 'prog.py')
|
||||||
parser.parse_args(['str', '--str', 'value'])
|
parser.parse_args(['str', '--str', 'value'])
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_str_tree():
|
def test_readme_str_tree(json):
|
||||||
output = {'root.cmd': 'str',
|
output = {'root.cmd': 'str',
|
||||||
'root.str': 'value',
|
'root.str': 'value',
|
||||||
'root.verbosity': False,
|
'root.verbosity': False,
|
||||||
'root.v': False}
|
'root.v': False}
|
||||||
config = get_config(True)
|
config = get_config(json, True)
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py')
|
parser = TiramisuCmdlineParser(config, 'prog.py')
|
||||||
parser.parse_args(['str', '--root.str', 'value'])
|
parser.parse_args(['str', '--root.str', 'value'])
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_str_tree_flatten():
|
def test_readme_str_tree_flatten(json):
|
||||||
output = {'root.cmd': 'str',
|
output = {'root.cmd': 'str',
|
||||||
'root.str': 'value',
|
'root.str': 'value',
|
||||||
'root.verbosity': False,
|
'root.verbosity': False,
|
||||||
'root.v': False}
|
'root.v': False}
|
||||||
config = get_config(True)
|
config = get_config(json, True)
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
|
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
|
||||||
parser.parse_args(['str', '--str', 'value'])
|
parser.parse_args(['str', '--str', 'value'])
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_str_int():
|
def test_readme_str_int(json):
|
||||||
output = {'cmd': 'str',
|
output = {'cmd': 'str',
|
||||||
'str': '3',
|
'str': '3',
|
||||||
'verbosity': False,
|
'verbosity': False,
|
||||||
'v': False}
|
'v': False}
|
||||||
config = get_config()
|
config = get_config(json)
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py')
|
parser = TiramisuCmdlineParser(config, 'prog.py')
|
||||||
parser.parse_args(['str', '--str', '3'])
|
parser.parse_args(['str', '--str', '3'])
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_str_int_tree():
|
def test_readme_str_int_tree(json):
|
||||||
output = {'root.cmd': 'str',
|
output = {'root.cmd': 'str',
|
||||||
'root.str': '3',
|
'root.str': '3',
|
||||||
'root.verbosity': False,
|
'root.verbosity': False,
|
||||||
'root.v': False}
|
'root.v': False}
|
||||||
config = get_config(True)
|
config = get_config(json, True)
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py')
|
parser = TiramisuCmdlineParser(config, 'prog.py')
|
||||||
parser.parse_args(['str', '--root.str', '3'])
|
parser.parse_args(['str', '--root.str', '3'])
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_str_int_tree_flatten():
|
def test_readme_str_int_tree_flatten(json):
|
||||||
output = {'root.cmd': 'str',
|
output = {'root.cmd': 'str',
|
||||||
'root.str': '3',
|
'root.str': '3',
|
||||||
'root.verbosity': False,
|
'root.verbosity': False,
|
||||||
'root.v': False}
|
'root.v': False}
|
||||||
config = get_config(True)
|
config = get_config(json, True)
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
|
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
|
||||||
parser.parse_args(['str', '--str', '3'])
|
parser.parse_args(['str', '--str', '3'])
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_list():
|
def test_readme_list(json):
|
||||||
output = {'cmd': 'list',
|
output = {'cmd': 'list',
|
||||||
'list': ['a', 'b', 'c'],
|
'list': ['a', 'b', 'c'],
|
||||||
'verbosity': False,
|
'verbosity': False,
|
||||||
'v': False}
|
'v': False}
|
||||||
config = get_config()
|
config = get_config(json)
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py')
|
parser = TiramisuCmdlineParser(config, 'prog.py')
|
||||||
parser.parse_args(['list', '--list', 'a', 'b', 'c'])
|
parser.parse_args(['list', '--list', 'a', 'b', 'c'])
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_list_tree():
|
def test_readme_list_tree(json):
|
||||||
output = {'root.cmd': 'list',
|
output = {'root.cmd': 'list',
|
||||||
'root.list': ['a', 'b', 'c'],
|
'root.list': ['a', 'b', 'c'],
|
||||||
'root.verbosity': False,
|
'root.verbosity': False,
|
||||||
'root.v': False}
|
'root.v': False}
|
||||||
config = get_config(True)
|
config = get_config(json, True)
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py')
|
parser = TiramisuCmdlineParser(config, 'prog.py')
|
||||||
parser.parse_args(['list', '--root.list', 'a', 'b', 'c'])
|
parser.parse_args(['list', '--root.list', 'a', 'b', 'c'])
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_list_tree_flatten():
|
def test_readme_list_tree_flatten(json):
|
||||||
output = {'root.cmd': 'list',
|
output = {'root.cmd': 'list',
|
||||||
'root.list': ['a', 'b', 'c'],
|
'root.list': ['a', 'b', 'c'],
|
||||||
'root.verbosity': False,
|
'root.verbosity': False,
|
||||||
'root.v': False}
|
'root.v': False}
|
||||||
config = get_config(True)
|
config = get_config(json, True)
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
|
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
|
||||||
parser.parse_args(['list', '--list', 'a', 'b', 'c'])
|
parser.parse_args(['list', '--list', 'a', 'b', 'c'])
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_list_uniq():
|
def test_readme_list_uniq(json):
|
||||||
output = {'cmd': 'list',
|
output = {'cmd': 'list',
|
||||||
'list': ['a'],
|
'list': ['a'],
|
||||||
'verbosity': False,
|
'verbosity': False,
|
||||||
'v': False}
|
'v': False}
|
||||||
config = get_config()
|
config = get_config(json)
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py')
|
parser = TiramisuCmdlineParser(config, 'prog.py')
|
||||||
parser.parse_args(['list', '--list', 'a'])
|
parser.parse_args(['list', '--list', 'a'])
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_list_uniq_tree():
|
def test_readme_list_uniq_tree(json):
|
||||||
output = {'root.cmd': 'list',
|
output = {'root.cmd': 'list',
|
||||||
'root.list': ['a'],
|
'root.list': ['a'],
|
||||||
'root.verbosity': False,
|
'root.verbosity': False,
|
||||||
'root.v': False}
|
'root.v': False}
|
||||||
config = get_config(True)
|
config = get_config(json, True)
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py')
|
parser = TiramisuCmdlineParser(config, 'prog.py')
|
||||||
parser.parse_args(['list', '--root.list', 'a'])
|
parser.parse_args(['list', '--root.list', 'a'])
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_list_uniq_tree_flatten():
|
def test_readme_list_uniq_tree_flatten(json):
|
||||||
output = {'root.cmd': 'list',
|
output = {'root.cmd': 'list',
|
||||||
'root.list': ['a'],
|
'root.list': ['a'],
|
||||||
'root.verbosity': False,
|
'root.verbosity': False,
|
||||||
'root.v': False}
|
'root.v': False}
|
||||||
config = get_config(True)
|
config = get_config(json, True)
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
|
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
|
||||||
parser.parse_args(['list', '--list', 'a'])
|
parser.parse_args(['list', '--list', 'a'])
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
||||||
|
|
||||||
def test_readme_longargument():
|
def test_readme_longargument(json):
|
||||||
output = {'cmd': 'list',
|
output = {'cmd': 'list',
|
||||||
'list': ['a'],
|
'list': ['a'],
|
||||||
'verbosity': True,
|
'verbosity': True,
|
||||||
'v': True}
|
'v': True}
|
||||||
config = get_config()
|
config = get_config(json, add_long=True)
|
||||||
config.option('verbosity').property.add('longargument')
|
|
||||||
parser = TiramisuCmdlineParser(config, 'prog.py')
|
parser = TiramisuCmdlineParser(config, 'prog.py')
|
||||||
parser.parse_args(['list', '--list', 'a', '--v'])
|
parser.parse_args(['list', '--list', 'a', '--v'])
|
||||||
assert config.value.dict() == output
|
assert config.value.dict() == output
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
#
|
#
|
||||||
# You should have received a copy of the GNU Lesser General Public License
|
# You should have received a copy of the GNU Lesser General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
from typing import Union, List, Optional
|
from typing import Union, List, Dict, Tuple, Optional, Any
|
||||||
from argparse import ArgumentParser, Namespace, SUPPRESS, _HelpAction, HelpFormatter
|
from argparse import ArgumentParser, Namespace, SUPPRESS, _HelpAction, HelpFormatter
|
||||||
from copy import copy
|
from copy import copy
|
||||||
from gettext import gettext as _
|
from gettext import gettext as _
|
||||||
|
@ -20,10 +20,11 @@ from gettext import gettext as _
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from tiramisu import Config
|
from tiramisu import Config
|
||||||
from tiramisu.error import PropertiesOptionError
|
from tiramisu.error import PropertiesOptionError, RequirementError
|
||||||
except ModuleNotFoundError:
|
except ModuleNotFoundError:
|
||||||
Config = None
|
Config = None
|
||||||
from tiramisu_json_api.error import PropertiesOptionError
|
from tiramisu_json_api.error import PropertiesOptionError
|
||||||
|
RequirementError = PropertiesOptionError
|
||||||
try:
|
try:
|
||||||
from tiramisu_json_api import Config as ConfigJson
|
from tiramisu_json_api import Config as ConfigJson
|
||||||
if Config is None:
|
if Config is None:
|
||||||
|
@ -33,34 +34,71 @@ except ModuleNotFoundError:
|
||||||
|
|
||||||
|
|
||||||
class TiramisuNamespace(Namespace):
|
class TiramisuNamespace(Namespace):
|
||||||
def _populate(self):
|
def __init__(self,
|
||||||
#self._config.property.read_only()
|
config: Config) -> None:
|
||||||
|
super().__setattr__('_config', config)
|
||||||
|
super().__setattr__('list_force_no', {})
|
||||||
|
self._populate()
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
def _populate(self) -> None:
|
||||||
for tiramisu_key, tiramisu_value in self._config.value.dict().items():
|
for tiramisu_key, tiramisu_value in self._config.value.dict().items():
|
||||||
option = self._config.option(tiramisu_key)
|
option = self._config.option(tiramisu_key)
|
||||||
if not option.option.issymlinkoption():
|
if not option.option.issymlinkoption():
|
||||||
if tiramisu_value == [] and option.option.ismulti() and option.owner.isdefault():
|
if tiramisu_value == [] and \
|
||||||
|
option.option.ismulti() and \
|
||||||
|
option.owner.isdefault():
|
||||||
tiramisu_value = None
|
tiramisu_value = None
|
||||||
super().__setattr__(tiramisu_key, tiramisu_value)
|
super().__setattr__(tiramisu_key, tiramisu_value)
|
||||||
#self._config.property.read_write()
|
|
||||||
|
|
||||||
def __init__(self, config):
|
def __setattr__(self,
|
||||||
self._config = config
|
key: str,
|
||||||
super().__init__()
|
value: Any) -> None:
|
||||||
|
if key in self.list_force_no:
|
||||||
|
true_key = self.list_force_no[key]
|
||||||
|
else:
|
||||||
|
true_key = key
|
||||||
|
option = self._config.option(true_key)
|
||||||
|
if option.option.isfollower():
|
||||||
|
_setattr = self._setattr_follower
|
||||||
|
true_value = ','.join(value[1:])
|
||||||
|
else:
|
||||||
|
_setattr = self._setattr
|
||||||
|
true_value = value
|
||||||
|
try:
|
||||||
|
_setattr(option, true_key, key, value)
|
||||||
|
except ValueError as err:
|
||||||
|
if option.option.type() == 'choice':
|
||||||
|
raise ValueError("invalid choice: '{}' (choose from {})".format(true_value, ', '.join([f"'{val}'" for val in option.value.list()])))
|
||||||
|
else:
|
||||||
|
raise err
|
||||||
|
|
||||||
def __setattr__(self, key, value):
|
def _setattr(self,
|
||||||
if key == '_config':
|
option: 'Option',
|
||||||
super().__setattr__(key, value)
|
true_key: str,
|
||||||
return
|
key: str,
|
||||||
# self._config.property.read_write()
|
value: Any) -> None:
|
||||||
option = self._config.option(key)
|
if option.option.ismulti() and \
|
||||||
if option.option.ismulti() and value is not None and not isinstance(value, list):
|
value is not None and \
|
||||||
|
not isinstance(value, list):
|
||||||
value = [value]
|
value = [value]
|
||||||
option.value.set(value)
|
option.value.set(value)
|
||||||
|
|
||||||
def __getattribute__(self, key):
|
def _setattr_follower(self,
|
||||||
if key == '__dict__' and hasattr(self, '_config'):
|
option: 'Option',
|
||||||
self._populate()
|
true_key: str,
|
||||||
return super().__getattribute__(key)
|
key: str,
|
||||||
|
value: Any) -> None:
|
||||||
|
if not value[0].isdecimal():
|
||||||
|
raise ValueError('index must be a number, not {}'.format(value[0]))
|
||||||
|
index = int(value[0])
|
||||||
|
if option.option.type() == 'boolean':
|
||||||
|
value = key not in self.list_force_no
|
||||||
|
elif option.option.issubmulti():
|
||||||
|
value = value[1:]
|
||||||
|
else:
|
||||||
|
value = value[1]
|
||||||
|
self._config.option(true_key, index).value.set(value)
|
||||||
|
|
||||||
|
|
||||||
class TiramisuHelpFormatter(HelpFormatter):
|
class TiramisuHelpFormatter(HelpFormatter):
|
||||||
|
@ -81,6 +119,62 @@ class _TiramisuHelpAction(_HelpAction):
|
||||||
_HelpAction.__call__(self, parser, None, None)
|
_HelpAction.__call__(self, parser, None, None)
|
||||||
|
|
||||||
|
|
||||||
|
class _BuildKwargs:
|
||||||
|
def __init__(self,
|
||||||
|
name: str,
|
||||||
|
option: 'Option',
|
||||||
|
cmdlineparser: 'TiramisuCmdlineParser',
|
||||||
|
properties: List[str],
|
||||||
|
force_no: bool) -> None:
|
||||||
|
self.kwargs = {}
|
||||||
|
self.cmdlineparser = cmdlineparser
|
||||||
|
self.properties = properties
|
||||||
|
self.force_no = force_no
|
||||||
|
if not self.force_no:
|
||||||
|
self.kwargs['help'] = option.doc().replace('%', '%%')
|
||||||
|
if 'positional' not in self.properties:
|
||||||
|
is_short_name = self.cmdlineparser._is_short_name(name, 'longargument' in self.properties)
|
||||||
|
if self.force_no:
|
||||||
|
ga_name = self.gen_argument_name(name, is_short_name)
|
||||||
|
self.cmdlineparser.namespace.list_force_no[ga_name] = option.path()
|
||||||
|
else:
|
||||||
|
ga_name = name
|
||||||
|
self.kwargs['dest'] = self.gen_argument_name(option.path(), False)
|
||||||
|
self.args = [self.cmdlineparser._gen_argument(ga_name, is_short_name)]
|
||||||
|
else:
|
||||||
|
self.args = [option.path()]
|
||||||
|
|
||||||
|
def __setitem__(self,
|
||||||
|
key: str,
|
||||||
|
value: Any) -> None:
|
||||||
|
self.kwargs[key] = value
|
||||||
|
|
||||||
|
def add_argument(self,
|
||||||
|
option: 'Option'):
|
||||||
|
is_short_name = self.cmdlineparser._is_short_name(option.name(), 'longargument' in self.properties)
|
||||||
|
if self.force_no:
|
||||||
|
name = self.gen_argument_name(option.name(), is_short_name)
|
||||||
|
else:
|
||||||
|
name = option.name()
|
||||||
|
self.args.insert(0, self.cmdlineparser._gen_argument(name, is_short_name))
|
||||||
|
|
||||||
|
def gen_argument_name(self, name, is_short_name):
|
||||||
|
if self.force_no:
|
||||||
|
if is_short_name:
|
||||||
|
prefix = 'n'
|
||||||
|
else:
|
||||||
|
prefix = 'no-'
|
||||||
|
if '.' in name:
|
||||||
|
sname = name.rsplit('.', 1)
|
||||||
|
name = sname[0] + '.' + prefix + sname[1]
|
||||||
|
else:
|
||||||
|
name = prefix + name
|
||||||
|
return name
|
||||||
|
|
||||||
|
def get(self) -> Tuple[Dict]:
|
||||||
|
return self.args, self.kwargs
|
||||||
|
|
||||||
|
|
||||||
class TiramisuCmdlineParser(ArgumentParser):
|
class TiramisuCmdlineParser(ArgumentParser):
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
config: Union[Config, ConfigJson],
|
config: Union[Config, ConfigJson],
|
||||||
|
@ -91,6 +185,7 @@ class TiramisuCmdlineParser(ArgumentParser):
|
||||||
self.fullpath = fullpath
|
self.fullpath = fullpath
|
||||||
self.config = config
|
self.config = config
|
||||||
kwargs['formatter_class'] = TiramisuHelpFormatter
|
kwargs['formatter_class'] = TiramisuHelpFormatter
|
||||||
|
self.namespace = TiramisuNamespace(self.config)
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.register('action', 'help', _TiramisuHelpAction)
|
self.register('action', 'help', _TiramisuHelpAction)
|
||||||
self._config_to_argparser(_forhelp,
|
self._config_to_argparser(_forhelp,
|
||||||
|
@ -118,9 +213,19 @@ class TiramisuCmdlineParser(ArgumentParser):
|
||||||
actions.pop(0)
|
actions.pop(0)
|
||||||
return super()._match_arguments_partial(actions, arg_string_pattern)
|
return super()._match_arguments_partial(actions, arg_string_pattern)
|
||||||
|
|
||||||
def _parse_known_args(self, args=None, namespace=None):
|
def _is_short_name(self, name, longargument):
|
||||||
|
return len(name) == 1 and not longargument
|
||||||
|
|
||||||
namespace_, args_ = super()._parse_known_args(args, namespace)
|
def _gen_argument(self, name, is_short_name):
|
||||||
|
if is_short_name:
|
||||||
|
return self.prefix_chars + name
|
||||||
|
return self.prefix_chars * 2 + name
|
||||||
|
|
||||||
|
def _parse_known_args(self, args=None, namespace=None):
|
||||||
|
try:
|
||||||
|
namespace_, args_ = super()._parse_known_args(args, namespace)
|
||||||
|
except ValueError as err:
|
||||||
|
self.error(err)
|
||||||
if args != args_ and args_ and args_[0].startswith(self.prefix_chars):
|
if args != args_ and args_ and args_[0].startswith(self.prefix_chars):
|
||||||
# option that was disabled are no more disable
|
# option that was disabled are no more disable
|
||||||
# so create a new parser
|
# so create a new parser
|
||||||
|
@ -148,21 +253,39 @@ class TiramisuCmdlineParser(ArgumentParser):
|
||||||
def add_subparsers(self, *args, **kwargs):
|
def add_subparsers(self, *args, **kwargs):
|
||||||
raise NotImplementedError(_('do not use add_subparsers'))
|
raise NotImplementedError(_('do not use add_subparsers'))
|
||||||
|
|
||||||
def _gen_argument(self, name, longargument, no_prefix=False):
|
def _option_is_not_default(self,
|
||||||
shortarg = len(name) == 1 and not longargument
|
properties,
|
||||||
if no_prefix:
|
type,
|
||||||
if shortarg:
|
name,
|
||||||
prefix = 'n'
|
value):
|
||||||
|
if 'positional' not in properties:
|
||||||
|
is_short_name = self._is_short_name(name, 'longargument' in properties)
|
||||||
|
self.prog += ' {}'.format(self._gen_argument(name, is_short_name))
|
||||||
|
if type != 'boolean':
|
||||||
|
self.prog += f' {value}'
|
||||||
|
|
||||||
|
def _config_list(self,
|
||||||
|
config: Config,
|
||||||
|
prefix: Optional[str],
|
||||||
|
_forhelp: bool,
|
||||||
|
group):
|
||||||
|
for obj in config.list(type='all'):
|
||||||
|
# do not display frozen option
|
||||||
|
if 'frozen' in obj.option.properties():
|
||||||
|
continue
|
||||||
|
if obj.option.isoptiondescription():
|
||||||
|
if _forhelp:
|
||||||
|
group = self.add_argument_group(obj.option.doc())
|
||||||
|
if prefix:
|
||||||
|
prefix_ = prefix + '.' + obj.option.name()
|
||||||
|
else:
|
||||||
|
prefix_ = obj.option.path()
|
||||||
|
self._config_to_argparser(_forhelp, obj, prefix_, group)
|
||||||
|
elif obj.option.type() == 'boolean' and not obj.option.issymlinkoption():
|
||||||
|
yield obj, False
|
||||||
|
yield obj, True
|
||||||
else:
|
else:
|
||||||
prefix = 'no-'
|
yield obj, None
|
||||||
if '.' in name:
|
|
||||||
sname = name.rsplit('.', 1)
|
|
||||||
name = sname[0] + '.' + prefix + sname[1]
|
|
||||||
else:
|
|
||||||
name = prefix + name
|
|
||||||
if shortarg:
|
|
||||||
return self.prefix_chars + name
|
|
||||||
return self.prefix_chars * 2 + name
|
|
||||||
|
|
||||||
def _config_to_argparser(self,
|
def _config_to_argparser(self,
|
||||||
_forhelp: bool,
|
_forhelp: bool,
|
||||||
|
@ -172,107 +295,128 @@ class TiramisuCmdlineParser(ArgumentParser):
|
||||||
if group is None:
|
if group is None:
|
||||||
group = super()
|
group = super()
|
||||||
actions = {}
|
actions = {}
|
||||||
for obj in config.list(type='all'):
|
leadership_len = None
|
||||||
|
for obj, force_no in self._config_list(config, prefix, _forhelp, group):
|
||||||
option = obj.option
|
option = obj.option
|
||||||
if option.isoptiondescription():
|
if option.isleader():
|
||||||
if _forhelp:
|
value = obj.value.get()
|
||||||
group = self.add_argument_group(option.doc())
|
leadership_len = len(value)
|
||||||
if prefix:
|
elif option.isfollower():
|
||||||
prefix_ = prefix + '.' + option.name()
|
value = []
|
||||||
else:
|
for index in range(leadership_len):
|
||||||
prefix_ = option.path()
|
value.append(self.config.option(obj.option.path(), index).value.get())
|
||||||
self._config_to_argparser(_forhelp, obj, prefix_, group)
|
else:
|
||||||
continue
|
value = obj.value.get()
|
||||||
if 'frozen' in option.properties():
|
|
||||||
continue
|
|
||||||
name = option.name()
|
name = option.name()
|
||||||
if name.startswith(self.prefix_chars):
|
if name.startswith(self.prefix_chars):
|
||||||
raise ValueError(_('name cannot startswith "{}"').format(self.prefix_chars))
|
raise ValueError(_('name cannot startswith "{}"').format(self.prefix_chars))
|
||||||
if self.fullpath and prefix:
|
if self.fullpath and prefix:
|
||||||
name = prefix + '.' + name
|
name = prefix + '.' + name
|
||||||
properties = obj.property.get()
|
if option.isfollower():
|
||||||
kwargs = {'help': option.doc().replace('%', '%%')}
|
properties = obj.option.properties()
|
||||||
|
else:
|
||||||
|
properties = obj.property.get()
|
||||||
if option.issymlinkoption():
|
if option.issymlinkoption():
|
||||||
symlink_name = option.name(follow_symlink=True)
|
symlink_name = option.name(follow_symlink=True)
|
||||||
if symlink_name in actions:
|
if symlink_name in actions:
|
||||||
actions[symlink_name][0][0].insert(0, self._gen_argument(option.name(), 'longargument' in properties))
|
for action in actions[symlink_name]:
|
||||||
if len(actions[symlink_name]) == 2:
|
action.add_argument(option)
|
||||||
actions[symlink_name][1][0].insert(0, self._gen_argument(option.name(), False, True))
|
|
||||||
continue
|
continue
|
||||||
if _forhelp and not obj.owner.isdefault() and obj.value.get() is not None:
|
kwargs = _BuildKwargs(name, option, self, properties, force_no)
|
||||||
if 'positional' not in properties:
|
if not option.isfollower() and _forhelp and not obj.owner.isdefault() and value is not None:
|
||||||
self.prog += ' {}'.format(self._gen_argument(name, 'longargument' in properties))
|
if not force_no:
|
||||||
if option.type() != 'boolean':
|
self._option_is_not_default(properties,
|
||||||
self.prog += ' {}'.format(obj.value.get())
|
option.type(),
|
||||||
|
name,
|
||||||
|
value)
|
||||||
else:
|
else:
|
||||||
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'))
|
||||||
# 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:
|
||||||
kwargs['default'] = obj.value.default()
|
kwargs['default'] = obj.value.default()
|
||||||
else:
|
else:
|
||||||
kwargs['default'] = obj.value.get()
|
kwargs['default'] = value
|
||||||
kwargs['nargs'] = '?'
|
kwargs['nargs'] = '?'
|
||||||
else:
|
else:
|
||||||
kwargs['dest'] = option.path()
|
|
||||||
kwargs['default'] = SUPPRESS
|
kwargs['default'] = SUPPRESS
|
||||||
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' and not option.isfollower():
|
||||||
if 'storefalse' in properties:
|
if 'storefalse' in properties:
|
||||||
|
if force_no:
|
||||||
|
action = 'store_true'
|
||||||
|
else:
|
||||||
|
action = 'store_false'
|
||||||
|
elif force_no:
|
||||||
action = 'store_false'
|
action = 'store_false'
|
||||||
no_action = 'store_true'
|
|
||||||
else:
|
else:
|
||||||
action = 'store_true'
|
action = 'store_true'
|
||||||
no_action = 'store_false'
|
|
||||||
kwargs['action'] = action
|
kwargs['action'] = action
|
||||||
args = [self._gen_argument(name, 'longargument' in properties)]
|
|
||||||
#
|
|
||||||
nkwargs = copy(kwargs)
|
|
||||||
nkwargs['action'] = no_action
|
|
||||||
del nkwargs['help']
|
|
||||||
nargs = [self._gen_argument(name, 'longargument' in properties, True)]
|
|
||||||
actions[option.name()] = [(args, kwargs), (nargs, nkwargs)]
|
|
||||||
continue
|
|
||||||
args = [self._gen_argument(name, 'longargument' in properties)]
|
|
||||||
if _forhelp:
|
|
||||||
value = obj.value.default()
|
|
||||||
else:
|
|
||||||
value = obj.value.get()
|
|
||||||
if value not in [None, []]:
|
|
||||||
#kwargs['default'] = kwargs['const'] = option.default()
|
|
||||||
#kwargs['action'] = 'store_const'
|
|
||||||
kwargs['nargs'] = '?'
|
|
||||||
if option.ismulti():
|
|
||||||
if _forhelp and 'mandatory' in properties:
|
|
||||||
kwargs['nargs'] = '+'
|
|
||||||
else:
|
else:
|
||||||
kwargs['nargs'] = '*'
|
if option.type() == 'boolean':
|
||||||
if option.type() == 'string':
|
kwargs['metavar'] = 'INDEX'
|
||||||
pass
|
if option.type() != 'boolean':
|
||||||
elif option.type() == 'integer':
|
if _forhelp:
|
||||||
kwargs['type'] = int
|
value = obj.value.default()
|
||||||
elif option.type() == 'choice':
|
if value not in [None, []]:
|
||||||
kwargs['choices'] = obj.value.list()
|
#kwargs['default'] = kwargs['const'] = option.default()
|
||||||
else:
|
#kwargs['action'] = 'store_const'
|
||||||
pass
|
kwargs['nargs'] = '?'
|
||||||
#raise NotImplementedError('not supported yet')
|
|
||||||
actions[option.name()] = [(args, kwargs)]
|
if not option.isfollower() and option.ismulti():
|
||||||
|
if _forhelp and 'mandatory' in properties:
|
||||||
|
kwargs['nargs'] = '+'
|
||||||
|
else:
|
||||||
|
kwargs['nargs'] = '*'
|
||||||
|
if option.isfollower() and not option.type() == 'boolean':
|
||||||
|
metavar = option.name().upper()
|
||||||
|
if option.issubmulti():
|
||||||
|
kwargs['nargs'] = '+'
|
||||||
|
else:
|
||||||
|
kwargs['nargs'] = 2
|
||||||
|
if _forhelp and 'mandatory' not in properties:
|
||||||
|
metavar = f'[{metavar}]'
|
||||||
|
if option.type() == 'choice':
|
||||||
|
choice_list = obj.value.list()
|
||||||
|
if choice_list[0] == '':
|
||||||
|
del choice_list[0]
|
||||||
|
choices = '{{{}}}'.format(','.join(choice_list))
|
||||||
|
if 'mandatory' not in properties:
|
||||||
|
choices = f'[{choices}]'
|
||||||
|
kwargs['metavar'] = ('INDEX', choices)
|
||||||
|
else:
|
||||||
|
kwargs['metavar'] = ('INDEX', metavar)
|
||||||
|
if option.type() == 'string':
|
||||||
|
pass
|
||||||
|
elif option.type() == 'integer' or option.type() == 'boolean':
|
||||||
|
# when boolean we are here only if follower
|
||||||
|
kwargs['type'] = int
|
||||||
|
if _forhelp and option.type() == 'boolean':
|
||||||
|
kwargs['metavar'] = 'INDEX'
|
||||||
|
kwargs['nargs'] = 1
|
||||||
|
elif option.type() == 'choice' and not option.isfollower():
|
||||||
|
kwargs['choices'] = obj.value.list()
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
#raise NotImplementedError('not supported yet')
|
||||||
|
actions.setdefault(option.name(), []).append(kwargs)
|
||||||
for values in actions.values():
|
for values in actions.values():
|
||||||
for args, kwargs in values:
|
for value in values:
|
||||||
|
args, kwargs = value.get()
|
||||||
group.add_argument(*args, **kwargs)
|
group.add_argument(*args, **kwargs)
|
||||||
|
def _valid_mandatory(self):
|
||||||
|
pass
|
||||||
|
|
||||||
def parse_args(self,
|
def parse_args(self,
|
||||||
*args,
|
*args,
|
||||||
valid_mandatory=True,
|
valid_mandatory=True,
|
||||||
**kwargs):
|
**kwargs):
|
||||||
kwargs['namespace'] = TiramisuNamespace(self.config)
|
kwargs['namespace'] = self.namespace
|
||||||
try:
|
try:
|
||||||
namespaces = super().parse_args(*args, **kwargs)
|
namespaces = super().parse_args(*args, **kwargs)
|
||||||
del namespaces.__dict__['_config']
|
|
||||||
except PropertiesOptionError as err:
|
except PropertiesOptionError as err:
|
||||||
name = err._option_bag.option.impl_getname()
|
name = err._option_bag.option.impl_getname()
|
||||||
properties = self.config.option(name).property.get()
|
properties = self.config.option(name).property.get()
|
||||||
|
@ -286,19 +430,31 @@ class TiramisuCmdlineParser(ArgumentParser):
|
||||||
else:
|
else:
|
||||||
self.error('unrecognized arguments: {}'.format(name))
|
self.error('unrecognized arguments: {}'.format(name))
|
||||||
if valid_mandatory:
|
if valid_mandatory:
|
||||||
|
errors = []
|
||||||
for key in self.config.value.mandatory():
|
for key in self.config.value.mandatory():
|
||||||
properties = self.config.option(key).property.get()
|
properties = self.config.option(key).option.properties()
|
||||||
if 'positional' not in properties:
|
if not self.config.option(key).option.isfollower():
|
||||||
if self.fullpath or '.' not in key:
|
if 'positional' not in properties:
|
||||||
name = key
|
if self.fullpath or '.' not in key:
|
||||||
|
name = key
|
||||||
|
else:
|
||||||
|
name = key.rsplit('.', 1)[1]
|
||||||
|
is_short_name = self._is_short_name(name, 'longargument' in self.config.option(key).property.get())
|
||||||
|
args = self._gen_argument(name, is_short_name)
|
||||||
else:
|
else:
|
||||||
name = key.rsplit('.', 1)[1]
|
args = key
|
||||||
args = self._gen_argument(name, 'longargument' in self.config.option(key).property.get())
|
|
||||||
else:
|
else:
|
||||||
args = key
|
if 'positional' not in properties:
|
||||||
|
args = self._gen_argument(key, False)
|
||||||
|
else:
|
||||||
|
args = key
|
||||||
if not self.fullpath and '.' in args:
|
if not self.fullpath and '.' in args:
|
||||||
args = args.rsplit('.', 1)[1]
|
args = args.rsplit('.', 1)[1]
|
||||||
self.error('the following arguments are required: {}'.format(args))
|
if 'positional' not in properties:
|
||||||
|
args = self._gen_argument(args, False)
|
||||||
|
errors.append(args)
|
||||||
|
if errors:
|
||||||
|
self.error('the following arguments are required: {}'.format(', '.join(errors)))
|
||||||
return namespaces
|
return namespaces
|
||||||
|
|
||||||
def format_usage(self,
|
def format_usage(self,
|
||||||
|
|
Loading…
Reference in a new issue