Compare commits
9 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 03f84ef27e | |||
| ce59a2544b | |||
| 0e86a7ef43 | |||
| 17ce3b77d5 | |||
| 8d2536543b | |||
| d480d5bf5c | |||
| fb0d9c9d22 | |||
| 093819f051 | |||
| b5347fb6cc |
6 changed files with 93 additions and 59 deletions
30
CHANGELOG.md
30
CHANGELOG.md
|
|
@ -1,3 +1,33 @@
|
|||
## 0.7.0a0 (2025-04-30)
|
||||
|
||||
### Feat
|
||||
|
||||
- for boolean always add --xxx and --no-xxx
|
||||
|
||||
### Fix
|
||||
|
||||
- formatter.short_name_max_len for symlink
|
||||
|
||||
## 0.6.2rc2 (2025-04-09)
|
||||
|
||||
### Fix
|
||||
|
||||
- version
|
||||
|
||||
## 0.6.2rc1 (2025-03-19)
|
||||
|
||||
### Fix
|
||||
|
||||
- better leader support
|
||||
|
||||
## 0.6.2rc0 (2025-01-03)
|
||||
|
||||
### Fix
|
||||
|
||||
- python 2.12 support
|
||||
|
||||
## 0.6.1 (2024-11-06)
|
||||
|
||||
## 0.6.1rc0 (2024-11-06)
|
||||
|
||||
### Fix
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ requires = ["flit_core >=3.8.0,<4"]
|
|||
|
||||
[project]
|
||||
name = "tiramisu_cmdline_parser"
|
||||
version = "0.6.1"
|
||||
version = "0.7.0a0"
|
||||
authors = [{name = "Emmanuel Garette", email = "gnunux@gnunux.info"}]
|
||||
readme = "README.md"
|
||||
description = "command-line parser using Tiramisu"
|
||||
|
|
@ -18,6 +18,8 @@ classifiers = [
|
|||
"Programming Language :: Python :: 3.10",
|
||||
"Programming Language :: Python :: 3.11",
|
||||
"Programming Language :: Python :: 3.12",
|
||||
"Programming Language :: Python :: 3.13",
|
||||
"Programming Language :: Python :: 3.14",
|
||||
"Programming Language :: Python :: 3",
|
||||
"Operating System :: OS Independent",
|
||||
"Natural Language :: English",
|
||||
|
|
@ -37,5 +39,9 @@ name = "cz_conventional_commits"
|
|||
tag_format = "$version"
|
||||
version_scheme = "pep440"
|
||||
version_provider = "pep621"
|
||||
#update_changelog_on_bump = true
|
||||
version_files = [
|
||||
"tiramisu_cmdline_parser/__version__.py",
|
||||
"pyproject.toml:version"
|
||||
]
|
||||
update_changelog_on_bump = true
|
||||
changelog_merge_prerelease = true
|
||||
|
|
|
|||
|
|
@ -121,7 +121,7 @@ options:
|
|||
-h, --help show this help message and exit
|
||||
|
||||
leader:
|
||||
-l [LEADER ...], --leader.leader [LEADER ...]
|
||||
-l, --leader.leader [LEADER ...]
|
||||
Leader var
|
||||
"""
|
||||
parser = TiramisuCmdlineParser(get_config(json, with_mandatory=True, with_symlink=True, with_default_value=False), 'prog.py', add_extra_options=False, formatter_class=TestHelpFormatter)
|
||||
|
|
@ -138,13 +138,13 @@ options:
|
|||
-h, --help show this help message and exit
|
||||
|
||||
leader:
|
||||
-l [LEADER ...], --leader.leader [LEADER ...]
|
||||
-l, --leader.leader [LEADER ...]
|
||||
Leader var
|
||||
--leader.follower INDEX [FOLLOWER]
|
||||
Follower
|
||||
--leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...]
|
||||
Follower submulti
|
||||
-i INDEX [FOLLOWER_INTEGER], --leader.follower_integer INDEX [FOLLOWER_INTEGER]
|
||||
-i, --leader.follower_integer INDEX [FOLLOWER_INTEGER]
|
||||
Follower integer
|
||||
--leader.follower_boolean INDEX
|
||||
Follower boolean
|
||||
|
|
|
|||
|
|
@ -22,5 +22,5 @@ except ImportError as err:
|
|||
warnings.warn("cannot not import TiramisuCmdlineParser {err}", ImportWarning)
|
||||
TiramisuCmdlineParser = None
|
||||
|
||||
__version__ = "0.5"
|
||||
from .__version__ import __version__
|
||||
__all__ = ("TiramisuCmdlineParser",)
|
||||
|
|
|
|||
2
tiramisu_cmdline_parser/__version__.py
Normal file
2
tiramisu_cmdline_parser/__version__.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
__version__ = "0.7.0a0"
|
||||
|
||||
|
|
@ -59,6 +59,30 @@ def get_choice_list(config, properties, display):
|
|||
return choices
|
||||
|
||||
|
||||
def gen_argument_name(name, is_short_name, force_no, force_del):
|
||||
if 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
|
||||
if 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
|
||||
|
||||
|
||||
class TiramisuNamespace(Namespace):
|
||||
def __init__(self, config: Config, root: Optional[str]) -> None:
|
||||
super().__setattr__("_config", config)
|
||||
|
|
@ -185,7 +209,7 @@ class TiramisuHelpFormatter:
|
|||
# Remove empty OD
|
||||
if (
|
||||
self.formatter.remove_empty_od
|
||||
and len(self.items) == 1
|
||||
and 0 < len(self.items) <= self.formatter.short_name_max_len
|
||||
and self.items[0][0].__name__ == "_format_text"
|
||||
):
|
||||
return ""
|
||||
|
|
@ -224,11 +248,6 @@ class _BuildKwargs:
|
|||
(not self.force_no or not add_extra_options)
|
||||
or (not_display and not display_modified_value)
|
||||
) and not self.force_del:
|
||||
if self.force_no:
|
||||
description = option.information.get("negative_description", None)
|
||||
else:
|
||||
description = None
|
||||
if description is None:
|
||||
description = option.description()
|
||||
self.kwargs["help"] = description
|
||||
if "positional" not in self.properties:
|
||||
|
|
@ -236,22 +255,24 @@ class _BuildKwargs:
|
|||
name, "longargument" in self.properties
|
||||
)
|
||||
if self.force_no:
|
||||
ga_name = self.gen_argument_name(name, is_short_name)
|
||||
ga_path = self.gen_argument_name(option.path(), is_short_name)
|
||||
ga_name = gen_argument_name(name, is_short_name, self.force_no, self.force_del)
|
||||
ga_path = gen_argument_name(option.path(), is_short_name, self.force_no, self.force_del)
|
||||
self.cmdlineparser.namespace.list_force_no[ga_path] = option.path()
|
||||
elif self.force_del:
|
||||
ga_name = self.gen_argument_name(name, is_short_name)
|
||||
ga_path = self.gen_argument_name(option.path(), is_short_name)
|
||||
ga_name = gen_argument_name(name, is_short_name, self.force_no, self.force_del)
|
||||
ga_path = gen_argument_name(option.path(), is_short_name, self.force_no, self.force_del)
|
||||
self.cmdlineparser.namespace.list_force_del[ga_path] = option.path()
|
||||
else:
|
||||
ga_name = name
|
||||
self.kwargs["dest"] = self.gen_argument_name(option.path(), False)
|
||||
self.kwargs["dest"] = gen_argument_name(option.path(), False, self.force_no, self.force_del)
|
||||
argument = self.cmdlineparser._gen_argument(ga_name, is_short_name)
|
||||
self.cmdlineparser.namespace.arguments[option.path()] = argument
|
||||
self.args = [argument]
|
||||
self.ga_name = ga_name
|
||||
else:
|
||||
self.cmdlineparser.namespace.arguments[option.path()] = option.path()
|
||||
self.args = [option.path()]
|
||||
self.ga_name = name
|
||||
|
||||
def __setitem__(self, key: str, value: Any) -> None:
|
||||
self.kwargs[key] = value
|
||||
|
|
@ -261,38 +282,15 @@ class _BuildKwargs:
|
|||
option.name(), "longargument" in self.properties
|
||||
)
|
||||
if self.force_no:
|
||||
name = self.gen_argument_name(option.name(), is_short_name)
|
||||
name = gen_argument_name(option.name(), is_short_name, self.force_no, self.force_del)
|
||||
elif self.force_del:
|
||||
name = self.gen_argument_name(option.name(), is_short_name)
|
||||
name = gen_argument_name(option.name(), is_short_name, self.force_no, self.force_del)
|
||||
else:
|
||||
name = option.name()
|
||||
argument = self.cmdlineparser._gen_argument(name, is_short_name)
|
||||
self.cmdlineparser.namespace.arguments[option.path()] = argument
|
||||
self.args.insert(0, argument)
|
||||
|
||||
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
|
||||
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]:
|
||||
return self.args, self.kwargs
|
||||
|
||||
|
|
@ -328,6 +326,7 @@ class TiramisuCmdlineParser(ArgumentParser):
|
|||
"TiramisuHelpFormatter", (TiramisuHelpFormatter, formatter_class), {}
|
||||
)
|
||||
formatter_class.remove_empty_od = self.remove_empty_od
|
||||
formatter_class.short_name_max_len = self.short_name_max_len
|
||||
kwargs["formatter_class"] = formatter_class
|
||||
if not _forhelp and self.unrestraint:
|
||||
subconfig = self.config.unrestraint
|
||||
|
|
@ -374,9 +373,9 @@ class TiramisuCmdlineParser(ArgumentParser):
|
|||
return self.prefix_chars + name
|
||||
return self.prefix_chars * 2 + name
|
||||
|
||||
def _parse_known_args(self, args=None, namespace=None):
|
||||
def _parse_known_args(self, args, namespace, *others):
|
||||
try:
|
||||
namespace_, args_ = super()._parse_known_args(args, namespace)
|
||||
namespace_, args_ = super()._parse_known_args(args, namespace, *others)
|
||||
except (ValueError, LeadershipError, AttributeError) as err:
|
||||
self.error(err)
|
||||
if args != args_ and args_ and args_[0].startswith(self.prefix_chars):
|
||||
|
|
@ -397,7 +396,7 @@ class TiramisuCmdlineParser(ArgumentParser):
|
|||
fullpath=self.fullpath,
|
||||
)
|
||||
namespace_, args_ = new_parser._parse_known_args(
|
||||
args_, new_parser.namespace
|
||||
args_, new_parser.namespace, *others
|
||||
)
|
||||
else:
|
||||
if self._registries["action"]["help"].needs:
|
||||
|
|
@ -454,7 +453,7 @@ class TiramisuCmdlineParser(ArgumentParser):
|
|||
elif (
|
||||
self.add_extra_options
|
||||
and obj.type() == "boolean"
|
||||
and not obj.issymlinkoption()
|
||||
# and not obj.issymlinkoption()
|
||||
):
|
||||
if not obj.isleader():
|
||||
yield obj, False, None
|
||||
|
|
@ -472,15 +471,6 @@ class TiramisuCmdlineParser(ArgumentParser):
|
|||
and obj.type() == "boolean"
|
||||
and obj.value.get() is True
|
||||
):
|
||||
negative_description = obj.information.get(
|
||||
"negative_description", None
|
||||
)
|
||||
if _forhelp and not negative_description:
|
||||
raise ValueError(
|
||||
_(
|
||||
f'the boolean "{obj.path()}" cannot have a default value to "True" with option add_extra_options if there is no negative_description'
|
||||
)
|
||||
)
|
||||
yield obj, True, None
|
||||
else:
|
||||
yield obj, None, None
|
||||
|
|
@ -488,7 +478,7 @@ class TiramisuCmdlineParser(ArgumentParser):
|
|||
# no follower found, search if there is a symlink
|
||||
for sobj in config.list(uncalculated=True):
|
||||
try:
|
||||
if sobj.issymlinkoption() and sobj.option().isleader():
|
||||
if sobj.issymlinkoption() and sobj.index() is None and sobj.option().isleader():
|
||||
yield sobj, None, None
|
||||
except ConfigError:
|
||||
pass
|
||||
|
|
@ -510,11 +500,17 @@ class TiramisuCmdlineParser(ArgumentParser):
|
|||
_('name cannot startswith "{}"').format(self.prefix_chars)
|
||||
)
|
||||
if option.issymlinkoption():
|
||||
symlink_name = option.option().name()
|
||||
if self.fullpath:
|
||||
argument_name = option.option().path()
|
||||
else:
|
||||
argument_name = option.option().name()
|
||||
is_short_name = len(option.option().name()) == 1
|
||||
symlink_name = gen_argument_name(argument_name, is_short_name, force_no, force_del)
|
||||
if symlink_name in options_is_not_default:
|
||||
options_is_not_default[symlink_name]["name"] = name
|
||||
if symlink_name in actions:
|
||||
for action in actions[symlink_name]:
|
||||
action.force_no = force_no
|
||||
action.add_argument(option)
|
||||
continue
|
||||
if force_del:
|
||||
|
|
@ -663,7 +659,7 @@ class TiramisuCmdlineParser(ArgumentParser):
|
|||
kwargs["type"] = float
|
||||
else:
|
||||
pass
|
||||
actions.setdefault(option.name(), []).append(kwargs)
|
||||
actions.setdefault(kwargs.ga_name, []).append(kwargs)
|
||||
|
||||
for option_is_not_default in options_is_not_default.values():
|
||||
self._option_is_not_default(**option_is_not_default)
|
||||
|
|
|
|||
Loading…
Reference in a new issue