Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add help strings to top level commands #653

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading