4 Commits

Author SHA1 Message Date
c9a911870d new tiramisu_cmdline_parser version 2020-04-08 16:58:39 +02:00
7f95b1bfb0 new tiramisu_cmdline_parser version 2020-04-08 16:48:58 +02:00
c13da02553 add float support 2020-03-20 22:19:10 +01:00
505af25995 support empty choice 2020-03-16 15:34:51 +01:00
3 changed files with 218 additions and 4 deletions

207
tests/test_choice.py Normal file
View File

@ -0,0 +1,207 @@
from io import StringIO
from contextlib import redirect_stdout, redirect_stderr
import pytest
from tiramisu_cmdline_parser import TiramisuCmdlineParser
from tiramisu import ChoiceOption, OptionDescription, Config
try:
from tiramisu_api import Config as JsonConfig
params = ['tiramisu', 'tiramisu-json']
except:
params = ['tiramisu']
def get_config(json):
positional = ChoiceOption('positional',
'choice the sub argument',
('str', 'list', 'int', 'none'),
properties=('positional', 'mandatory'))
positional_int = ChoiceOption('positional_int',
'choice the sub argument',
(1, 2, 3),
properties=('positional', 'mandatory'))
str_ = ChoiceOption('str',
'choice the sub argument',
('str1', 'str2', 'str3'))
int_ = ChoiceOption('int',
'choice the sub argument',
(1, 2, 3))
int_multi = ChoiceOption('int_multi',
'choice the sub argument',
(1, 2, 3),
multi=True)
od = OptionDescription('od',
'od',
[positional, positional_int, str_, int_, int_multi])
config = Config(od)
config.property.read_write()
if json == 'tiramisu':
return config
jconfig = JsonConfig(config.option.dict())
return jconfig
@pytest.fixture(params=params)
def json(request):
return request.param
def test_choice_positional(json):
output1 = '''usage: prog.py "str" "1" [-h] [--str {str1,str2,str3}] [--int {1,2,3}]
[--int_multi [{1,2,3} [{1,2,3} ...]]]
{str,list,int,none} {1,2,3}
prog.py: error: argument positional: invalid choice: 'error' (choose from 'str', 'list', 'int', 'none')
'''
output2 = '''usage: prog.py "str" "1" [-h] [--str {str1,str2,str3}] [--int {1,2,3}]
[--int_multi [{1,2,3} [{1,2,3} ...]]]
{str,list,int,none} {1,2,3}
prog.py: error: argument positional_int: invalid choice: '4' (choose from '1', '2', '3')
'''
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['str', '1'])
assert config.value.dict() == {'positional': 'str',
'positional_int': 1,
'str': None,
'int': None,
'int_multi': []}
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['error', '1'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output1
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['str', '4'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output2
def test_choice_str(json):
output = """usage: prog.py "str" "1" --str "str3" [-h] [--str {str1,str2,str3}]
[--int {1,2,3}]
[--int_multi [{1,2,3} [{1,2,3} ...]]]
{str,list,int,none} {1,2,3}
prog.py: error: argument --str: invalid choice: 'error' (choose from 'str1', 'str2', 'str3')
"""
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['str', '1', '--str', 'str1'])
assert config.value.dict() == {'positional': 'str',
'positional_int': 1,
'str': 'str1',
'int': None,
'int_multi': []}
parser.parse_args(['str', '1', '--str', 'str2'])
assert config.value.dict() == {'positional': 'str',
'positional_int': 1,
'str': 'str2',
'int': None,
'int_multi': []}
parser.parse_args(['str', '1', '--str', 'str3'])
assert config.value.dict() == {'positional': 'str',
'positional_int': 1,
'str': 'str3',
'int': None,
'int_multi': []}
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['str', '1', '--str', 'error'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
assert config.value.dict() == {'positional': 'str',
'positional_int': 1,
'str': 'str3',
'int': None,
'int_multi': []}
def test_choice_int(json):
output = """usage: prog.py "str" "1" --int "1" [-h] [--str {str1,str2,str3}]
[--int {1,2,3}]
[--int_multi [{1,2,3} [{1,2,3} ...]]]
{str,list,int,none} {1,2,3}
prog.py: error: argument --int: invalid choice: '4' (choose from '1', '2', '3')
"""
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['str', '1', '--int', '1'])
assert config.value.dict() == {'positional': 'str',
'positional_int': 1,
'str': None,
'int': 1,
'int_multi': []}
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['str', '1', '--int', '4'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
assert config.value.dict() == {'positional': 'str',
'positional_int': 1,
'str': None,
'int': 1,
'int_multi': []}
def test_choice_int_multi(json):
output = """usage: prog.py "str" "1" --int_multi "1" "2" [-h] [--str {str1,str2,str3}]
[--int {1,2,3}]
[--int_multi [{1,2,3} [{1,2,3} ...]]]
{str,list,int,none} {1,2,3}
prog.py: error: argument --int_multi: invalid choice: '4' (choose from '1', '2', '3')
"""
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['str', '1', '--int_multi', '1', '2'])
assert config.value.dict() == {'positional': 'str',
'positional_int': 1,
'str': None,
'int': None,
'int_multi': [1, 2]}
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['str', '1', '--int_multi', '4'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
assert config.value.dict() == {'positional': 'str',
'positional_int': 1,
'str': None,
'int': None,
'int_multi': [1, 2]}
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['str', '1', '--int_multi', '1', '4'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
assert config.value.dict() == {'positional': 'str',
'positional_int': 1,
'str': None,
'int': None,
'int_multi': [1, 2]}

View File

@ -1,4 +1,9 @@
from .api import TiramisuCmdlineParser
try:
from .api import TiramisuCmdlineParser
except ImportError as err:
import warnings
warnings.warn("cannot not import TiramisuCmdlineParser {err}", ImportWarning)
TiramisuCmdlineParser = None
__version__ = "0.4"
__version__ = "0.5"
__all__ = ('TiramisuCmdlineParser',)

View File

@ -26,7 +26,7 @@ except ImportError:
RequirementError = PropertiesOptionError
LeadershipError = ValueError
try:
from tiramisu__api import Config as ConfigJson
from tiramisu_api import Config as ConfigJson
if Config is None:
Config = ConfigJson
except ImportError:
@ -39,7 +39,7 @@ def get_choice_list(obj, properties, display):
return str(choice)
return choice
choices = [convert(choice) for choice in obj.value.list()]
if choices[0] == '':
if choices and choices[0] == '':
del choices[0]
if display:
choices = '{{{}}}'.format(','.join(choices))
@ -545,6 +545,8 @@ class TiramisuCmdlineParser(ArgumentParser):
elif option.type() == 'choice' and not option.isfollower():
# do not manage choice with argparse there is problem with integer problem
kwargs['choices'] = get_choice_list(obj, properties, False)
elif option.type() == 'float':
kwargs['type'] = float
else:
pass
actions.setdefault(option.name(), []).append(kwargs)