Skip to content

Commit

Permalink
fix: Add help strings to top level commands
Browse files Browse the repository at this point in the history
  • Loading branch information
rumpelsepp committed Dec 18, 2024
1 parent 5d1d222 commit de5750e
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions src/gallia/cli/gallia.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def parse_and_run(
commands: type[BaseCommand] | MutableMapping[str, CommandTree | type[BaseCommand]],
auto_complete: bool = True,
setup_log: bool = True,
top_level_options: Mapping[str, Callable[[], None]] | None = None,
top_level_options: Mapping[str, tuple[Callable[[], None], str]] | None = None,
show_help_on_zero_args: bool = True,
) -> Never:
"""
Expand All @@ -145,17 +145,11 @@ def parse_and_run(

parser = create_parser(commands)

def make_f(c: Callable[[], None]) -> Callable[[Any], None]:
def f(_: Any) -> None:
c()

return f

if top_level_options is not None:
for name, func in top_level_options.items():
for name, (func, help_) in top_level_options.items():

class Action(argparse.Action):
f = make_f(func)
cmd = staticmethod(func)

def __call__(
self,
Expand All @@ -164,11 +158,14 @@ def __call__(
values: str | Sequence[Any] | None,
option_string: str | None = None,
) -> None:
self.f()
self.cmd()
sys.exit(exitcodes.OK)

parser.add_argument(
name if name.startswith("-") else f"--{name}", nargs=0, action=Action
name,
nargs=0,
action=Action,
help=help_,
)

if show_help_on_zero_args and len(sys.argv) == 1:
Expand All @@ -191,21 +188,6 @@ def __call__(
sys.exit(get_command(config).entry_point())


def main() -> None:
gallia_commands = load_commands()
parse_and_run(
gallia_commands,
top_level_options={
"version": version,
"-v": version,
"show-plugins": show_plugins,
"show-config": show_config,
"template": template,
},
show_help_on_zero_args=True,
)


def version() -> None:
"""
Prints the currently installed version of gallia.
Expand Down Expand Up @@ -328,5 +310,19 @@ def template() -> None:
print(output.strip())


def main() -> None:
gallia_commands = load_commands()
parse_and_run(
gallia_commands,
top_level_options={
"--version": (version, "show version and exit"),
"--show-plugins": (show_plugins, "show registered plugins"),
"--show-config": (show_config, "show loaded config"),
"--template": (template, "generate a annotated config template"),
},
show_help_on_zero_args=True,
)


if __name__ == "__main__":
main()

0 comments on commit de5750e

Please sign in to comment.