cli: also register common flags in subcommands

When a user runs --help on a subcommand they don't see some options such
as --options or --flake. To fix this we now register all common flags
also in subcommands.
This commit is contained in:
Jörg Thalheim
2024-05-29 09:00:51 +02:00
parent b9788a5dba
commit 5b926f57cc

View File

@@ -51,19 +51,14 @@ class AppendOptionAction(argparse.Action):
lst.append(values[1]) lst.append(values[1])
def create_parser(prog: str | None = None) -> argparse.ArgumentParser: def flake_path(arg: str) -> str | Path:
parser = argparse.ArgumentParser( flake_dir = Path(arg).resolve()
prog=prog, if flake_dir.exists() and flake_dir.is_dir():
description="The clan cli tool.", return flake_dir
epilog=( return arg
"""
Online reference for the clan cli tool: https://docs.clan.lol/reference/cli/
For more detailed information, visit: https://docs.clan.lol
"""
),
formatter_class=argparse.RawTextHelpFormatter,
)
def add_common_flags(parser: argparse.ArgumentParser) -> None:
parser.add_argument( parser.add_argument(
"--debug", "--debug",
help="Enable debug logging", help="Enable debug logging",
@@ -94,6 +89,32 @@ For more detailed information, visit: https://docs.clan.lol
type=flake_path, type=flake_path,
) )
def register_common_flags(parser: argparse.ArgumentParser) -> None:
has_subparsers = False
for action in parser._actions:
if isinstance(action, argparse._SubParsersAction):
for choice, child_parser in action.choices.items():
has_subparsers = True
register_common_flags(child_parser)
if not has_subparsers:
add_common_flags(parser)
def create_parser(prog: str | None = None) -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog=prog,
description="The clan cli tool.",
epilog=(
"""
Online reference for the clan cli tool: https://docs.clan.lol/reference/cli/
For more detailed information, visit: https://docs.clan.lol
"""
),
formatter_class=argparse.RawTextHelpFormatter,
)
add_common_flags(parser)
subparsers = parser.add_subparsers() subparsers = parser.add_subparsers()
parser_backups = subparsers.add_parser( parser_backups = subparsers.add_parser(
@@ -208,7 +229,7 @@ For more detailed information, visit: https://docs.clan.lol/getting-started/secr
This subcommand provides an interface to facts of clan machines. This subcommand provides an interface to facts of clan machines.
Facts are artifacts that a service can generate. Facts are artifacts that a service can generate.
There are public and secret facts. There are public and secret facts.
Public facts can be referenced by other machines directly. Public facts can be referenced by other machines directly.
Public facts can include: ip addresses, public keys. Public facts can include: ip addresses, public keys.
Secret facts can include: passwords, private keys. Secret facts can include: passwords, private keys.
@@ -223,7 +244,7 @@ Examples:
$ clan facts generate $ clan facts generate
Will generate facts for all machines. Will generate facts for all machines.
$ clan facts generate --service [SERVICE] --regenerate $ clan facts generate --service [SERVICE] --regenerate
Will regenerate facts, if they are already generated for a specific service. Will regenerate facts, if they are already generated for a specific service.
This is especially useful for resetting certain passwords while leaving the rest This is especially useful for resetting certain passwords while leaving the rest
@@ -250,7 +271,7 @@ Examples:
List all the machines managed by clan. List all the machines managed by clan.
$ clan machines update [MACHINES] $ clan machines update [MACHINES]
Will update the specified machine [MACHINE], if [MACHINE] is omitted, the command Will update the specified machine [MACHINE], if [MACHINE] is omitted, the command
will attempt to update every configured machine. will attempt to update every configured machine.
$ clan machines install [MACHINES] [TARGET_HOST] $ clan machines install [MACHINES] [TARGET_HOST]
@@ -285,6 +306,8 @@ For more detailed information, visit: https://docs.clan.lol/getting-started/depl
if argcomplete: if argcomplete:
argcomplete.autocomplete(parser) argcomplete.autocomplete(parser)
register_common_flags(parser)
return parser return parser