pkgs/cli: Fix adding common flags for aliases

This commit is contained in:
a-kenji
2024-11-14 14:57:41 +01:00
committed by Mic92
parent 8773abf849
commit 25b90983af

View File

@@ -55,6 +55,16 @@ def default_flake() -> FlakeId | None:
def add_common_flags(parser: argparse.ArgumentParser) -> None:
def argument_exists(parser: argparse.ArgumentParser, arg: str) -> bool:
"""
Check if an argparse argument already exists.
This is needed because the aliases subcommand doesn't *really*
create an alias - it duplicates the actual parser in the tree
making duplication inevitable while naively traversing.
"""
return any(arg in action.option_strings for action in parser._actions) # noqa: SLF001 -> private_member accessed
if not argument_exists(parser, "--debug"):
parser.add_argument(
"--debug",
help="Enable debug logging",
@@ -62,6 +72,7 @@ def add_common_flags(parser: argparse.ArgumentParser) -> None:
default=False,
)
if not argument_exists(parser, "--option"):
parser.add_argument(
"--option",
help="Nix option to set",
@@ -71,6 +82,7 @@ def add_common_flags(parser: argparse.ArgumentParser) -> None:
default=[],
)
if not argument_exists(parser, "--flake"):
parser.add_argument(
"--flake",
help="path to the flake where the clan resides in, can be a remote flake or local, can be set through the [CLAN_DIR] environment variable",