add storefalse properties for boolean option

This commit is contained in:
Emmanuel Garette 2019-04-03 07:51:26 +02:00
parent 266cef224e
commit eeca074331
2 changed files with 28 additions and 6 deletions

View File

@ -7,7 +7,7 @@ from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \
SymLinkOption, OptionDescription, Config SymLinkOption, OptionDescription, Config
def get_config(has_tree=False): def get_config(has_tree=False, default_verbosity=False):
choiceoption = ChoiceOption('cmd', choiceoption = ChoiceOption('cmd',
'choice the sub argument', 'choice the sub argument',
('str', 'list', 'int', 'none'), ('str', 'list', 'int', 'none'),
@ -15,7 +15,7 @@ def get_config(has_tree=False):
'positional')) 'positional'))
booloption = BoolOption('verbosity', booloption = BoolOption('verbosity',
'increase output verbosity', 'increase output verbosity',
default=False) default=default_verbosity)
short_booloption = SymLinkOption('v', booloption) short_booloption = SymLinkOption('v', booloption)
str_ = StrOption('str', str_ = StrOption('str',
'string option', 'string option',
@ -412,6 +412,28 @@ def test_readme_int_verbosity_short():
assert config.value.dict() == output assert config.value.dict() == output
def test_readme_int_verbosity_short_store_false():
output = {'cmd': 'int',
'int': 3,
'verbosity': None,
'v': True}
config = get_config(default_verbosity=None)
config.option('verbosity').property.add('storefalse')
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(): def test_readme_int_verbosity_short_no():
output = {'cmd': 'int', output = {'cmd': 'int',
'int': 3, 'int': 3,

View File

@ -221,12 +221,12 @@ class TiramisuCmdlineParser(ArgumentParser):
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':
if obj.value.get() is False: if 'storefalse' in properties:
action = 'store_true'
no_action = 'store_false'
else:
action = 'store_false' action = 'store_false'
no_action = 'store_true' no_action = 'store_true'
else:
action = 'store_true'
no_action = 'store_false'
kwargs['action'] = action kwargs['action'] = action
args = [self._gen_argument(name, 'longargument' in properties)] args = [self._gen_argument(name, 'longargument' in properties)]
# #