-
Notifications
You must be signed in to change notification settings - Fork 28
Description
Describe the bug
Command groups defined in cobra are not being accounted for or displayed in the same manner when using the fang library. The hierarchical organization and visual grouping of commands that works correctly in pure cobra implementations is lost when using fang as a wrapper.
Setup
Please complete the following information along with version numbers, if applicable.
- OS: macOS
- Shell: zsh
- Terminal Emulator: ghostty
- Terminal Multiplexer: tmux
- Go Version: v1.24.3
- Cobra Version: v1.9.1
Note: you might encounter rendering issues if your locale does not use
UTF-8
encoding. Please check your locale (locale
on POSIX systems) to
see what encoding is being used by your system.
To Reproduce
Steps to reproduce the behavior:
- Create a cobra CLI application with command groups using
cobra.Group
- Define multiple commands and assign them to different groups
- Wrap the cobra application with fang
- Run the help command to view command organization
- Compare output with pure cobra implementation
Source Code
package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
// Assuming fang import here
)
func main() {
// Define command groups
dataGroup := cobra.Group{
ID: "data",
Title: "Data Commands:",
}
utilGroup := cobra.Group{
ID: "util",
Title: "Utility Commands:",
}
rootCmd := &cobra.Command{
Use: "myapp",
Short: "My application CLI",
}
// Add groups to root command
rootCmd.AddGroup(&dataGroup, &utilGroup)
// Data commands
fetchCmd := &cobra.Command{
Use: "fetch",
Short: "Fetch data from source",
GroupID: dataGroup.ID,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Fetch command executed")
},
}
processCmd := &cobra.Command{
Use: "process",
Short: "Process data files",
GroupID: dataGroup.ID,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Process command executed")
},
}
// Utility commands
configCmd := &cobra.Command{
Use: "config",
Short: "Manage configuration",
GroupID: utilGroup.ID,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Config command executed")
},
}
statusCmd := &cobra.Command{
Use: "status",
Short: "Show application status",
GroupID: utilGroup.ID,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Status command executed")
},
}
rootCmd.AddCommand(fetchCmd, processCmd, configCmd, statusCmd)
// Pure cobra execution works correctly
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
// When wrapped with fang, groups are not displayed properly
// fang.Execute(rootCmd) // Groups lost here
}
Expected behavior
When using pure cobra, the help output should display commands organized by their groups:
Available Commands:
help Help about any command
Data Commands:
fetch Fetch data from source
process Process data files
Utility Commands:
config Manage configuration
status Show application status
Actual behavior
When using fang, the command groups are not preserved and commands appear in a flat list without the group organization:
Available Commands:
help Help about any command
fetch Fetch data from source
process Process data files
config Manage configuration
status Show application status
Screenshots
Additional context
This issue affects the user experience significantly as command groups provide important visual organization for CLI applications with many commands. The fang library appears to be processing cobra commands in a way that strips or ignores the GroupID assignments and group definitions.
The bug likely stems from fang's command processing logic not accounting for:
cobra.Group
definitions added to the root commandGroupID
assignments on individual commands- The help template rendering that displays grouped commands
This makes fang incompatible with modern cobra applications that rely on command grouping for better UX.