Improve nested CLI help coverage

This commit is contained in:
zhi
2026-03-21 16:37:47 +00:00
parent ebad3cd0d3
commit 1e8437d0b1
3 changed files with 132 additions and 65 deletions

View File

@@ -74,6 +74,7 @@ internal/
- Top-level and group/leaf help rendering (`--help` / `--help-brief`) - Top-level and group/leaf help rendering (`--help` / `--help-brief`)
- Permission-aware command visibility via `/auth/me/permissions` - Permission-aware command visibility via `/auth/me/permissions`
- Detailed leaf help text for all commands, with padded-cell/manual auth flag differences - Detailed leaf help text for all commands, with padded-cell/manual auth flag differences
- Nested help coverage for `config`, `monitor server`, and `monitor api-key` subtrees
- `(not permitted)` rendering for unauthorized commands - `(not permitted)` rendering for unauthorized commands
**Core commands:** **Core commands:**

View File

@@ -73,7 +73,7 @@ func handleLeafOrRun(name string, args []string, run func()) {
} }
func handleConfig(args []string) { func handleConfig(args []string) {
if isHelpFlagOnly(args) { if isLeafHelpFlagOnly(args) || isHelpFlagOnly(args) {
if text, ok := help.RenderLeafHelp("config", "show"); ok { if text, ok := help.RenderLeafHelp("config", "show"); ok {
fmt.Print(text) fmt.Print(text)
return return
@@ -131,6 +131,17 @@ func handleGroup(group help.Group, args []string) {
output.Errorf("unknown %s subcommand: %s", group.Name, args[0]) output.Errorf("unknown %s subcommand: %s", group.Name, args[0])
} }
if group.Name == "monitor" && (sub.Name == "server" || sub.Name == "api-key") {
if len(args) == 1 {
handleMonitorCommand(sub.Name, nil)
return
}
if isHelpLikePath(args[1:]) || sub.Permitted {
handleMonitorCommand(sub.Name, args[1:])
return
}
}
if len(args) > 1 && isLeafHelpFlagOnly(args[1:]) { if len(args) > 1 && isLeafHelpFlagOnly(args[1:]) {
if !sub.Permitted { if !sub.Permitted {
fmt.Println(help.RenderNotPermitted(group.Name, sub.Name)) fmt.Println(help.RenderNotPermitted(group.Name, sub.Name))
@@ -282,6 +293,16 @@ func isLeafHelpFlagOnly(args []string) bool {
return len(args) == 1 && (args[0] == "--help" || args[0] == "-h" || args[0] == "--help-brief") return len(args) == 1 && (args[0] == "--help" || args[0] == "-h" || args[0] == "--help-brief")
} }
func isHelpLikePath(args []string) bool {
if len(args) == 0 {
return true
}
if len(args) == 1 {
return args[0] == "--help" || args[0] == "-h" || args[0] == "--help-brief"
}
return isLeafHelpFlagOnly(args[len(args)-1:])
}
func findGroup(name string) (help.Group, bool) { func findGroup(name string) (help.Group, bool) {
for _, group := range help.CommandSurface() { for _, group := range help.CommandSurface() {
if group.Name == name { if group.Name == name {
@@ -742,12 +763,32 @@ func handleMonitorCommand(subCmd string, args []string) {
} }
func handleMonitorServerCommand(args []string, tokenFlag string) { func handleMonitorServerCommand(args []string, tokenFlag string) {
if len(args) == 0 { serverCommands := []help.Command{
output.Error("usage: hf monitor server <list|get|create|delete> ...") {Name: "list", Description: "List monitor servers", Permitted: true},
{Name: "get", Description: "Show a monitor server by identifier", Permitted: true},
{Name: "create", Description: "Create a monitor server", Permitted: true},
{Name: "delete", Description: "Delete a monitor server", Permitted: true},
}
if len(args) == 0 || isHelpFlagOnly(args) {
fmt.Print(help.RenderGroupHelp("monitor server", serverCommands))
return
}
if len(args) == 1 && args[0] == "--help-brief" {
fmt.Print(help.RenderGroupHelpBrief("monitor server", serverCommands))
return
} }
subCmd := args[0] subCmd := args[0]
remaining := args[1:] remaining := args[1:]
if isLeafHelpFlagOnly(remaining) {
if text, ok := help.RenderLeafHelp("monitor/server", subCmd); ok {
fmt.Print(text)
return
}
fmt.Printf("hf monitor server %s\n", subCmd)
return
}
switch subCmd { switch subCmd {
case "list": case "list":
@@ -770,12 +811,30 @@ func handleMonitorServerCommand(args []string, tokenFlag string) {
} }
func handleMonitorAPIKeyCommand(args []string, tokenFlag string) { func handleMonitorAPIKeyCommand(args []string, tokenFlag string) {
if len(args) == 0 { apiKeyCommands := []help.Command{
output.Error("usage: hf monitor api-key <generate|revoke> <identifier>") {Name: "generate", Description: "Generate a monitor API key", Permitted: true},
{Name: "revoke", Description: "Revoke a monitor API key", Permitted: true},
}
if len(args) == 0 || isHelpFlagOnly(args) {
fmt.Print(help.RenderGroupHelp("monitor api-key", apiKeyCommands))
return
}
if len(args) == 1 && args[0] == "--help-brief" {
fmt.Print(help.RenderGroupHelpBrief("monitor api-key", apiKeyCommands))
return
} }
subCmd := args[0] subCmd := args[0]
remaining := args[1:] remaining := args[1:]
if isLeafHelpFlagOnly(remaining) {
if text, ok := help.RenderLeafHelp("monitor/api-key", subCmd); ok {
fmt.Print(text)
return
}
fmt.Printf("hf monitor api-key %s\n", subCmd)
return
}
switch subCmd { switch subCmd {
case "generate": case "generate":

View File

@@ -22,6 +22,7 @@ func RenderLeafHelp(group, cmd string) (string, bool) {
var b strings.Builder var b strings.Builder
name := strings.TrimSpace(strings.TrimSpace(group + " " + cmd)) name := strings.TrimSpace(strings.TrimSpace(group + " " + cmd))
name = strings.ReplaceAll(name, "/", " ")
b.WriteString(fmt.Sprintf("hf %s - %s\n", name, spec.Summary)) b.WriteString(fmt.Sprintf("hf %s - %s\n", name, spec.Summary))
b.WriteString("\nUsage:\n") b.WriteString("\nUsage:\n")
for _, line := range spec.Usage { for _, line := range spec.Usage {
@@ -166,7 +167,13 @@ func leafHelpSpec(group, cmd string) (leafHelp, bool) {
"propose/reopen": {Summary: "Reopen a proposal", Usage: []string{"hf propose reopen <propose-code>"}, Flags: authFlagHelp()}, "propose/reopen": {Summary: "Reopen a proposal", Usage: []string{"hf propose reopen <propose-code>"}, Flags: authFlagHelp()},
"monitor/overview": {Summary: "Show monitor overview", Usage: []string{"hf monitor overview"}, Flags: authFlagHelp()}, "monitor/overview": {Summary: "Show monitor overview", Usage: []string{"hf monitor overview"}, Flags: authFlagHelp()},
"monitor/server": {Summary: "Manage monitor servers", Usage: []string{"hf monitor server list", "hf monitor server get <identifier>", "hf monitor server create --identifier <identifier> [--name <display-name>]", "hf monitor server delete <identifier>"}, Flags: authFlagHelp()}, "monitor/server": {Summary: "Manage monitor servers", Usage: []string{"hf monitor server list", "hf monitor server get <identifier>", "hf monitor server create --identifier <identifier> [--name <display-name>]", "hf monitor server delete <identifier>"}, Flags: authFlagHelp()},
"monitor/server/list": {Summary: "List monitor servers", Usage: []string{"hf monitor server list"}, Flags: authFlagHelp()},
"monitor/server/get": {Summary: "Show a monitor server by identifier", Usage: []string{"hf monitor server get <identifier>"}, Flags: authFlagHelp()},
"monitor/server/create": {Summary: "Create a monitor server", Usage: []string{"hf monitor server create --identifier <identifier> [--name <display-name>]"}, Flags: authFlagHelp()},
"monitor/server/delete": {Summary: "Delete a monitor server", Usage: []string{"hf monitor server delete <identifier>"}, Flags: authFlagHelp()},
"monitor/api-key": {Summary: "Manage monitor API keys", Usage: []string{"hf monitor api-key generate <identifier>", "hf monitor api-key revoke <identifier>"}, Flags: authFlagHelp()}, "monitor/api-key": {Summary: "Manage monitor API keys", Usage: []string{"hf monitor api-key generate <identifier>", "hf monitor api-key revoke <identifier>"}, Flags: authFlagHelp()},
"monitor/api-key/generate": {Summary: "Generate a monitor API key", Usage: []string{"hf monitor api-key generate <identifier>"}, Flags: authFlagHelp()},
"monitor/api-key/revoke": {Summary: "Revoke a monitor API key", Usage: []string{"hf monitor api-key revoke <identifier>"}, Flags: authFlagHelp()},
} }
if group == "" { if group == "" {