add --pop-leader to remove a value in a leader option
This commit is contained in:
@ -39,6 +39,7 @@ class TiramisuNamespace(Namespace):
|
||||
super().__setattr__('_config', config)
|
||||
super().__setattr__('_root', root)
|
||||
super().__setattr__('list_force_no', {})
|
||||
super().__setattr__('list_force_del', {})
|
||||
self._populate()
|
||||
super().__init__()
|
||||
|
||||
@ -61,6 +62,8 @@ class TiramisuNamespace(Namespace):
|
||||
value: Any) -> None:
|
||||
if key in self.list_force_no:
|
||||
true_key = self.list_force_no[key]
|
||||
elif key in self.list_force_del:
|
||||
true_key = self.list_force_del[key]
|
||||
else:
|
||||
true_key = key
|
||||
option = self._config.option(true_key)
|
||||
@ -71,7 +74,10 @@ class TiramisuNamespace(Namespace):
|
||||
_setattr = self._setattr
|
||||
true_value = value
|
||||
try:
|
||||
_setattr(option, true_key, key, value)
|
||||
if key in self.list_force_del:
|
||||
option.value.pop(value)
|
||||
else:
|
||||
_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()])))
|
||||
@ -122,17 +128,12 @@ class TiramisuHelpFormatter:
|
||||
self.items[0][0].__name__ == '_format_text':
|
||||
return ''
|
||||
# Remove OD if name == description
|
||||
if self.items and \
|
||||
self.formatter.remove_empty_description_od and \
|
||||
self.items[0][0].__name__ == '_format_text':
|
||||
name = self.items[0][1][0]
|
||||
path = self.heading
|
||||
if '.' in path:
|
||||
compare = path.rsplit('.', 1)[1]
|
||||
else:
|
||||
compare = path
|
||||
if name == compare:
|
||||
return ''
|
||||
if self.formatter.remove_empty_description_od and \
|
||||
self.items is not None and \
|
||||
self.heading is not None and \
|
||||
len(self.items) > 1 and \
|
||||
self.items[0][0].__name__ != '_format_text':
|
||||
return ''
|
||||
return super().format_help()
|
||||
|
||||
|
||||
@ -151,18 +152,23 @@ class _BuildKwargs:
|
||||
option: 'Option',
|
||||
cmdlineparser: 'TiramisuCmdlineParser',
|
||||
properties: List[str],
|
||||
force_no: bool) -> None:
|
||||
force_no: bool,
|
||||
force_del: bool) -> None:
|
||||
self.kwargs = {}
|
||||
self.cmdlineparser = cmdlineparser
|
||||
self.properties = properties
|
||||
self.force_no = force_no
|
||||
if not self.force_no:
|
||||
self.force_del = force_del
|
||||
if not self.force_no and not self.force_del:
|
||||
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()
|
||||
elif self.force_del:
|
||||
ga_name = self.gen_argument_name(name, is_short_name)
|
||||
self.cmdlineparser.namespace.list_force_del[ga_name] = option.path()
|
||||
else:
|
||||
ga_name = name
|
||||
self.kwargs['dest'] = self.gen_argument_name(option.path(), False)
|
||||
@ -180,6 +186,8 @@ class _BuildKwargs:
|
||||
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)
|
||||
elif self.force_del:
|
||||
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))
|
||||
@ -195,6 +203,16 @@ class _BuildKwargs:
|
||||
name = sname[0] + '.' + prefix + sname[1]
|
||||
else:
|
||||
name = prefix + name
|
||||
if self.force_del:
|
||||
if is_short_name:
|
||||
prefix = 'p'
|
||||
else:
|
||||
prefix = 'pop-'
|
||||
if '.' in name:
|
||||
sname = name.rsplit('.', 1)
|
||||
name = sname[0] + '.' + prefix + sname[1]
|
||||
else:
|
||||
name = prefix + name
|
||||
return name
|
||||
|
||||
def get(self) -> Tuple[Dict]:
|
||||
@ -315,7 +333,7 @@ class TiramisuCmdlineParser(ArgumentParser):
|
||||
config: Config,
|
||||
prefix: Optional[str],
|
||||
_forhelp: bool,
|
||||
group):
|
||||
group, level):
|
||||
for obj in config.list(type='all'):
|
||||
# do not display frozen option
|
||||
if 'frozen' in obj.option.properties():
|
||||
@ -329,23 +347,32 @@ class TiramisuCmdlineParser(ArgumentParser):
|
||||
prefix_ = prefix + '.' + obj.option.name()
|
||||
else:
|
||||
prefix_ = obj.option.path()
|
||||
self._config_to_argparser(_forhelp, obj, prefix_, newgroup)
|
||||
self._config_to_argparser(_forhelp, obj, prefix_, newgroup, level + 1)
|
||||
elif obj.option.type() == 'boolean' and not obj.option.issymlinkoption():
|
||||
yield obj, False
|
||||
yield obj, True
|
||||
if not obj.option.isleader():
|
||||
yield obj, False, None
|
||||
yield obj, True, None
|
||||
else:
|
||||
yield obj, False, False
|
||||
yield obj, False, True
|
||||
yield obj, True, None
|
||||
elif obj.option.isleader():
|
||||
yield obj, None, False
|
||||
yield obj, None, True
|
||||
else:
|
||||
yield obj, None
|
||||
yield obj, None, None
|
||||
|
||||
def _config_to_argparser(self,
|
||||
_forhelp: bool,
|
||||
config,
|
||||
prefix: Optional[str],
|
||||
group=None) -> None:
|
||||
group=None,
|
||||
level=0) -> None:
|
||||
if group is None:
|
||||
group = super()
|
||||
actions = {}
|
||||
leadership_len = None
|
||||
for obj, force_no in self._config_list(config, prefix, _forhelp, group):
|
||||
for obj, force_no, force_del in self._config_list(config, prefix, _forhelp, group, level):
|
||||
option = obj.option
|
||||
name = option.name()
|
||||
if name.startswith(self.prefix_chars):
|
||||
@ -356,7 +383,9 @@ class TiramisuCmdlineParser(ArgumentParser):
|
||||
for action in actions[symlink_name]:
|
||||
action.add_argument(option)
|
||||
continue
|
||||
if option.isleader():
|
||||
if force_del:
|
||||
value = None
|
||||
elif option.isleader():
|
||||
value = obj.value.get()
|
||||
leadership_len = len(value)
|
||||
elif option.isfollower():
|
||||
@ -371,7 +400,7 @@ class TiramisuCmdlineParser(ArgumentParser):
|
||||
properties = obj.option.properties()
|
||||
else:
|
||||
properties = obj.property.get()
|
||||
kwargs = _BuildKwargs(name, option, self, properties, force_no)
|
||||
kwargs = _BuildKwargs(name, option, self, properties, force_no, force_del)
|
||||
if not option.isfollower() and _forhelp and not obj.owner.isdefault() and value is not None:
|
||||
if not force_no:
|
||||
self._option_is_not_default(properties,
|
||||
@ -393,33 +422,34 @@ class TiramisuCmdlineParser(ArgumentParser):
|
||||
kwargs['default'] = SUPPRESS
|
||||
if _forhelp and 'mandatory' in properties:
|
||||
kwargs['required'] = True
|
||||
if option.type() == 'boolean' and not option.isfollower():
|
||||
if 'storefalse' in properties:
|
||||
if force_no:
|
||||
action = 'store_true'
|
||||
else:
|
||||
if not force_del and option.type() == 'boolean':
|
||||
if not option.isfollower():
|
||||
if 'storefalse' in properties:
|
||||
if force_no:
|
||||
action = 'store_true'
|
||||
else:
|
||||
action = 'store_false'
|
||||
elif force_no:
|
||||
action = 'store_false'
|
||||
elif force_no:
|
||||
action = 'store_false'
|
||||
else:
|
||||
action = 'store_true'
|
||||
kwargs['action'] = action
|
||||
else:
|
||||
action = 'store_true'
|
||||
kwargs['action'] = action
|
||||
else:
|
||||
if option.type() == 'boolean':
|
||||
kwargs['metavar'] = 'INDEX'
|
||||
if option.type() != 'boolean':
|
||||
if _forhelp:
|
||||
value = obj.value.default()
|
||||
if value not in [None, []]:
|
||||
#kwargs['default'] = kwargs['const'] = option.default()
|
||||
#kwargs['action'] = 'store_const'
|
||||
kwargs['nargs'] = '?'
|
||||
if option.type() != 'boolean' or force_del:
|
||||
if not force_del:
|
||||
if _forhelp:
|
||||
value = obj.value.default()
|
||||
if value not in [None, []]:
|
||||
#kwargs['default'] = kwargs['const'] = option.default()
|
||||
#kwargs['action'] = 'store_const'
|
||||
kwargs['nargs'] = '?'
|
||||
|
||||
if not option.isfollower() and option.ismulti():
|
||||
if _forhelp and 'mandatory' in properties:
|
||||
kwargs['nargs'] = '+'
|
||||
else:
|
||||
kwargs['nargs'] = '*'
|
||||
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():
|
||||
@ -438,7 +468,10 @@ class TiramisuCmdlineParser(ArgumentParser):
|
||||
kwargs['metavar'] = ('INDEX', choices)
|
||||
else:
|
||||
kwargs['metavar'] = ('INDEX', metavar)
|
||||
if option.type() == 'string':
|
||||
if force_del:
|
||||
kwargs['metavar'] = 'INDEX'
|
||||
kwargs['type'] = int
|
||||
elif option.type() == 'string':
|
||||
pass
|
||||
elif option.type() == 'integer' or option.type() == 'boolean':
|
||||
# when boolean we are here only if follower
|
||||
|
Reference in New Issue
Block a user