A simple, powerful CLI framework for Go with automatic flag inheritance and type-safe arguments.
Go Version License Test Coverage
- Automatic Flag Inheritance - Parent flags available to all child commands
- Type-Safe Arguments - Automatic type conversion for function parameters
- Lifecycle Hooks - PreRun, PostRun, PersistentPreRun, PersistentPostRun
- Context Support - Built-in context.Context for cancellation and timeouts
- Shell Completion - Bash, Zsh, Fish, and PowerShell
- Fluent API - Chainable interface for building CLI apps
- Zero Dependencies - Only depends on
github.com/nyxstack/color
go get github.com/nyxstack/cli
π Full Documentation - Comprehensive guides and API reference
- Quick Start - Get started in 5 minutes
- Commands - Creating and organizing commands
- Flags & Arguments - Working with flags and arguments
- Lifecycle Hooks - PreRun, PostRun, and execution flow
- API Reference - Complete API documentation
package main import ( "context" "fmt" "github.com/nyxstack/cli" ) func main() { var verbose bool app := cli.Root("myapp"). Description("My awesome CLI application"). Flag(&verbose, "verbose", "v", false, "Enable verbose output"). Action(func(ctx context.Context, cmd *cli.Command) error { if verbose { fmt.Println("Verbose mode enabled") } fmt.Println("Hello from myapp!") return nil }) if err := app.Execute(); err != nil { fmt.Fprintln(os.Stderr, "Error:", err) os.Exit(1) } }
Usage:
myapp # Hello from myapp! myapp --verbose=true # Verbose mode enabled + Hello from myapp! myapp -v # Same as above myapp --help # Shows automatic help
See the Quick Start Guide for more examples.
var verbose bool root.Flag(&verbose, "verbose", "v", false, "Verbose output") deploy := cli.Cmd("deploy") root.AddCommand(deploy) // --verbose automatically available on deploy command! // Works: myapp --verbose=true deploy // Works: myapp deploy --verbose=true
cli.Cmd("deploy"). Arg("environment", "Target environment", true). Arg("replicas", "Number of replicas", true). Action(func(ctx context.Context, cmd *cli.Command, env string, replicas int) error { // env is string, replicas is int - automatic conversion! fmt.Printf("Deploying to %s with %d replicas\n", env, replicas) return nil })
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() app.ExecuteContext(ctx) // Automatic timeout handling
Comprehensive examples in examples/ directory:
- basic - Simple commands and flags
- subcommands - Command hierarchies
- flags-struct - Struct-based flags
- lifecycle - Hooks and execution flow
- validation - Input validation
- inheritance - Flag inheritance
- And 8 more...
Contributions welcome! Please ensure:
- All tests pass:
go test -v - Coverage maintained:
go test -cover - Code formatted:
go fmt ./...
MIT License - see LICENSE file.