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

@@ -73,7 +73,7 @@ func handleLeafOrRun(name string, args []string, run func()) {
}
func handleConfig(args []string) {
if isHelpFlagOnly(args) {
if isLeafHelpFlagOnly(args) || isHelpFlagOnly(args) {
if text, ok := help.RenderLeafHelp("config", "show"); ok {
fmt.Print(text)
return
@@ -131,6 +131,17 @@ func handleGroup(group help.Group, args []string) {
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 !sub.Permitted {
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")
}
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) {
for _, group := range help.CommandSurface() {
if group.Name == name {
@@ -742,12 +763,32 @@ func handleMonitorCommand(subCmd string, args []string) {
}
func handleMonitorServerCommand(args []string, tokenFlag string) {
if len(args) == 0 {
output.Error("usage: hf monitor server <list|get|create|delete> ...")
serverCommands := []help.Command{
{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]
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 {
case "list":
@@ -770,12 +811,30 @@ func handleMonitorServerCommand(args []string, tokenFlag string) {
}
func handleMonitorAPIKeyCommand(args []string, tokenFlag string) {
if len(args) == 0 {
output.Error("usage: hf monitor api-key <generate|revoke> <identifier>")
apiKeyCommands := []help.Command{
{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]
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 {
case "generate":