Overview
Commander provides a comprehensive system for managing and configuring CLI features, including custom help, reference documentation, shell completion, and command hooks. Built on top of Cobra, it simplifies the creation of professional command-line tools.
Manager
The Manager is the core component that orchestrates all CLI features.
Type Definition
type Manager struct {
RootCmd * cobra . Command
Help bool // Enable custom help
Reference bool // Enable reference command
Completion bool // Enable shell completion
Config bool // Enable configuration management
Docs bool // Enable markdown documentation
Hooks [] HookBehavior // Hook behaviors to apply
Topics [] HelpTopic // Help topics with details
}
The root Cobra command for the CLI application
Enable custom help formatting with organized command sections
Enable the reference command for comprehensive documentation
Enable shell completion for Bash, Zsh, Fish, and PowerShell
Enable configuration management features
Enable markdown documentation generation
Custom behaviors to apply to commands annotated with client:true
Additional help topics for detailed information
Functions
New
Creates a new CLI Manager with optional configurations.
func New ( rootCmd * cobra . Command , options ... func ( * Manager )) * Manager
The root Cobra command for the CLI
Functional options for configuring the Manager (e.g., WithTopics, WithHooks)
Configured Manager instance with default settings applied
Example:
rootCmd := & cobra . Command { Use : "mycli" }
manager := commander . New ( rootCmd ,
commander . WithTopics ( topics ),
commander . WithHooks ( hooks ),
)
manager . Init ()
Init
Initializes and sets up all CLI features based on the Manager’s configuration.
This method:
Configures custom help formatting
Adds reference command
Adds completion command
Sets up markdown documentation generation
Registers help topics
Applies client hooks
Example:
manager := commander . New ( rootCmd )
manager . Help = true
manager . Reference = true
manager . Init () // Apply all configurations
IsCommandErr
Checks if an error is related to a Cobra command error (e.g., unknown command, unknown flag).
func IsCommandErr ( err error ) bool
True if the error is a command-related error, false otherwise
Example:
if err := rootCmd . Execute (); err != nil {
if commander . IsCommandErr ( err ) {
// User error: show help
fmt . Println ( "See --help for usage" )
} else {
// Program error: log and exit
log . Fatal ( err )
}
}
Configuration Options
WithTopics
Configures help topics for the Manager.
func WithTopics ( topics [] HelpTopic ) func ( * Manager )
Slice of help topics to add to the CLI
Example:
topics := [] commander . HelpTopic {
{
Name : "environment" ,
Short : "Environment variables reference" ,
Long : "Detailed description of environment variables..." ,
Example : "export MY_VAR=value" ,
},
}
manager := commander . New ( rootCmd , commander . WithTopics ( topics ))
WithHooks
Configures hook behaviors for the Manager.
func WithHooks ( hooks [] HookBehavior ) func ( * Manager )
Slice of hook behaviors to apply to commands
Example:
hooks := [] commander . HookBehavior {
{
Name : "auth" ,
Behavior : func ( cmd * cobra . Command ) {
cmd . PreRunE = func ( cmd * cobra . Command , args [] string ) error {
return checkAuthentication ()
}
},
},
}
manager := commander . New ( rootCmd , commander . WithHooks ( hooks ))
Types
HelpTopic
Defines a single help topic with its details.
type HelpTopic struct {
Name string
Short string
Long string
Example string
}
The command name for the help topic (e.g., “environment”)
Brief description shown in help listings
Detailed information about the topic
Example usage or code snippets
HookBehavior
Defines a specific behavior applied to commands.
type HookBehavior struct {
Name string
Behavior func ( cmd * cobra . Command )
}
Descriptive name for the hook (e.g., “setup”, “auth”)
Behavior
func(cmd *cobra.Command)
required
Function that modifies or enhances the command
Features
Custom Help
Provides enhanced help output with:
Organized command sections (Core, Other, Help topics)
Command grouping by annotations
Custom formatting with colors and styles
Detailed error messages for incorrect usage
Example:
// Annotate commands for grouping
cmd := & cobra . Command {
Use : "deploy" ,
Short : "Deploy application" ,
Annotations : map [ string ] string {
"group" : "core" ,
},
}
Reference Command
Automatically generates a comprehensive reference of all commands in markdown format.
$ mycli reference
# mycli reference
## `mycli deploy`
Deploy application
## `mycli config`
Manage configuration
Shell Completion
Generates completion scripts for multiple shells:
# Bash
$ mycli completion bash > /etc/bash_completion.d/mycli
# Zsh
$ mycli completion zsh > "${ fpath [1]}/_mycli"
# Fish
$ mycli completion fish > ~/.config/fish/completions/mycli.fish
# PowerShell
PS > mycli completion powershell > mycli.ps1
Markdown Documentation
Generates markdown documentation tree for all commands (when enabled).
$ mycli markdown
# Generates docs/ directory with markdown files
Best Practices
Use annotations to organize commands into logical groups: cmd . Annotations = map [ string ] string {
"group" : "core" , // or "help", or custom group names
}
Mark commands that need special behaviors: cmd . Annotations = map [ string ] string {
"client" : "true" , // Hooks will be applied to this command
}
Distinguish between user and program errors: if err := rootCmd . Execute (); err != nil {
if commander . IsCommandErr ( err ) {
os . Exit ( 1 ) // User error
} else {
log . Fatal ( err ) // Program error
}
}
Complete Example
package main
import (
" github.com/raystack/salt/cli/commander "
" github.com/spf13/cobra "
)
func main () {
// Create root command
rootCmd := & cobra . Command {
Use : "mycli" ,
Short : "My CLI application" ,
}
// Define help topics
topics := [] commander . HelpTopic {
{
Name : "environment" ,
Short : "Environment variables" ,
Long : "List of environment variables..." ,
Example : "export API_KEY=secret" ,
},
}
// Define hooks
hooks := [] commander . HookBehavior {
{
Name : "auth" ,
Behavior : func ( cmd * cobra . Command ) {
cmd . PreRunE = func ( cmd * cobra . Command , args [] string ) error {
// Authentication logic
return nil
}
},
},
}
// Create and initialize manager
manager := commander . New (
rootCmd ,
commander . WithTopics ( topics ),
commander . WithHooks ( hooks ),
)
manager . Init ()
// Execute
if err := rootCmd . Execute (); err != nil {
if commander . IsCommandErr ( err ) {
os . Exit ( 1 )
} else {
log . Fatal ( err )
}
}
}