-
Notifications
You must be signed in to change notification settings - Fork 0
Fix group help display and refactor command resolution #33
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fixed group bare invocation showing "Unknown command" instead of group help, and help output now lists subcommands by name instead of raw argparse internals. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -181,6 +181,38 @@ def to_def(self) -> GroupDef: | |
| hidden=self.hidden, | ||
| ) | ||
|
|
||
| def format_help(self, prog_prefix: str = "") -> str: | ||
| """Render help from this group's command/group registries. | ||
|
|
||
| Writes rendered help to stdout and returns the output string. | ||
| """ | ||
| import sys | ||
|
|
||
| from milo.help import HelpState | ||
| from milo.templates import get_env | ||
|
|
||
| prog = f"{prog_prefix} {self.name}".strip() if prog_prefix else self.name | ||
| commands = tuple( | ||
| [ | ||
| {"name": cmd.name, "help": getattr(cmd, "description", "")} | ||
| for cmd in self._commands.values() | ||
| if not getattr(cmd, "hidden", False) | ||
| ] | ||
| + [ | ||
| {"name": g.name, "help": g.description} | ||
| for g in self._groups.values() | ||
| if not g.hidden | ||
| ] | ||
| ) | ||
|
|
||
| state = HelpState(prog=prog, description=self.description, commands=commands) | ||
| env = get_env() | ||
| template = env.get_template("help.kida") | ||
| output = template.render(state=state) | ||
| sys.stdout.write(output + "\n") | ||
| sys.stdout.flush() | ||
| return output | ||
|
Comment on lines
+184
to
+214
|
||
|
|
||
| def walk_commands(self, prefix: str = ""): | ||
| """Yield (dotted_path, CommandDef) for all commands in this tree.""" | ||
| path_prefix = f"{prefix}{self.name}." if prefix else f"{self.name}." | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,30 @@ | ||
| {{ state.prog | bold }}{% if state.description %} {{ "-" | dim }} {{ state.description }}{% endif %} | ||
| {%- if state.commands %} | ||
|
|
||
| {{ "commands" | yellow | bold }}: | ||
| {%- for cmd in state.commands %} | ||
| {{ cmd.name | cyan }} {{ cmd.help }} | ||
| {%- endfor %} | ||
| {%- endif %} | ||
| {%- for group in state.groups %} | ||
|
|
||
| {% for group in state.groups %} | ||
| {{ group.title | yellow | bold }}: | ||
| {% for action in group.actions %} {% if action.option_strings %}{{ action.option_strings | join(", ") | green }}{% else %}{{ action.dest | cyan }}{% endif %}{% if action.metavar %} {{ action.metavar | dim }}{% endif %} {{ action.help }}{% if action.default is not none and action.default != "==SUPPRESS==" %} {{ ("(default: " ~ action.default ~ ")") | dim }}{% endif %} | ||
| {% endfor %} | ||
| {% endfor %} | ||
| {% if state.examples %} | ||
| {%- for action in group.actions %} | ||
| {% if action.option_strings %}{{ action.option_strings | join(", ") | green }}{% if action.metavar %} {{ action.metavar | dim }}{% endif %} {{ action.help }}{% if action.default is not none and action.default != "==SUPPRESS==" and action.default is not false and action.default != 0 and action.default != "" %} {{ ("(default: " ~ action.default ~ ")") | dim }}{% endif %}{% else %}{{ action.dest | cyan }}{% if action.metavar %} {{ action.metavar | dim }}{% endif %} {{ action.help }}{% if action.default is not none and action.default != "==SUPPRESS==" and action.default is not false and action.default != 0 and action.default != "" %} {{ ("(default: " ~ action.default ~ ")") | dim }}{% endif %}{% endif %} | ||
| {%- endfor %} | ||
| {%- endfor %} | ||
| {%- if state.options %} | ||
|
|
||
| {{ "options" | yellow | bold }}: | ||
| {%- for opt in state.options %} | ||
| {{ opt.flags | green }}{% if opt.metavar %} {{ opt.metavar | dim }}{% endif %} {{ opt.help }}{% if opt.default %} {{ ("(default: " ~ opt.default ~ ")") | dim }}{% endif %} | ||
| {%- endfor %} | ||
| {%- endif %} | ||
| {%- if state.examples %} | ||
|
|
||
| {{ "examples" | yellow | bold }}: | ||
| {% for ex in state.examples %} {{ ("$ " ~ ex.command) | green }}{% if ex.description %} | ||
| {%- for ex in state.examples %} | ||
| {{ ("$ " ~ ex.command) | green }}{% if ex.description %} | ||
| {{ ex.description | dim }}{% endif %} | ||
| {% endfor %} | ||
| {% endif %} | ||
| {%- endfor %} | ||
| {%- endif %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For nested groups,
group.format_help(self.name)only prefixes the CLI name, sobengal site configwill render asbengal configin the help header. To show the correct invocation path, carry the resolved group path/prefix through_resolve_group_command(e.g., accumulateprog_prefixduring recursion or store a fullprogstring onResolvedGroup) and pass that intoformat_help.