Documentation
¶
Overview ¶
Package cli implements a minimal, opinionated command and flag framework built on spf13/pflag.
An App registers command constructors with App.AddCommand. The selected constructor creates a Command, whose Bind callback defines scoped flags and whose Run callback executes with a signal-aware context.
A minimal application looks like this:
app := cli.NewApp("mig")
app.AddCommand("version", "Print version information", func() *cli.Command {
return &cli.Command{
Run: func(ctx context.Context, args []string) error {
fmt.Println("mig version 1.2.3")
return nil
},
}
})
if err := app.Run(); err != nil {
return err
}
App.DefaultCommand selects a command when no explicit command is present. Flags are defined in Command.Bind. Before argument parsing, matching environment variables are applied to unchanged flags: names are lowercased and underscores become hyphens, so DB_DSN maps to --db-dsn.
The -h and --help flags print help and return nil. Command lookup and flag parsing errors print relevant usage before being returned. Errors produced by Command.Run are returned without printing usage.
Index ¶
- func ParseWithFlagSet(fs *FlagSet, args []string) error
- type App
- func (app *App) AddCommand(name, title string, constructor func() *Command)
- func (app *App) FindCommand(commands []string, fallback string) (*Command, error)
- func (app *App) HasCommand(name string) bool
- func (app *App) Help()
- func (app *App) HelpCommand(fs *FlagSet, command *Command)
- func (app *App) ParseCommands(args []string) []string
- func (app *App) Run() error
- func (app *App) RunWithArgs(args []string) error
- type Command
- type CommandInfo
- type FlagSet
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseWithFlagSet ¶
ParseWithFlagSet applies environment variables and parses args for a scoped FlagSet. Environment names are lowercased and underscores become hyphens; variables without an underscore or a matching flag are ignored. Argument values take precedence over environment values.
Types ¶
type App ¶
type App struct {
// Name is the executable name shown in usage output.
Name string
// DefaultCommand is selected when no explicit command is provided.
DefaultCommand string
// contains filtered or unexported fields
}
App is the cli entrypoint.
Example ¶
package main
import (
"context"
"fmt"
"github.com/titpetric/cli"
)
func main() {
app := cli.NewApp("mig")
app.AddCommand("version", "Print version information", func() *cli.Command {
var verbose bool
return &cli.Command{
Name: "version",
Title: "Print version information",
Bind: func(fs *cli.FlagSet) {
fs.BoolVarP(&verbose, "verbose", "v", false, "enable verbose output")
},
Run: func(ctx context.Context, args []string) error {
if verbose {
fmt.Println("mig version 1.2.3 (commit abcdef)")
} else {
fmt.Println("mig version 1.2.3")
}
return nil
},
}
})
// In a real program you would call:
// _ = app.Run()
//
// For examples/tests, invoke RunWithArgs directly.
_ = app.RunWithArgs([]string{"version"})
}
Output: mig version 1.2.3
func (*App) AddCommand ¶
AddCommand adds a command to the app.
func (*App) FindCommand ¶
FindCommand finds a command for the app.
func (*App) HasCommand ¶ added in v0.2.1
HasCommand checks if a command exists in the app.
func (*App) HelpCommand ¶
HelpCommand prints out help for a specific command.
func (*App) ParseCommands ¶ added in v0.2.2
ParseCommands cleans up args[], returning only commands. If no commands are detected, DefaultCommand is returned.
func (*App) RunWithArgs ¶
RunWithArgs selects and executes a command with a context canceled by SIGINT or SIGTERM. Help requests return nil; lookup and flag parsing errors print usage, while errors returned by Command.Run do not.
type Command ¶
type Command struct {
// Name defaults to the name registered with App.AddCommand.
Name string
// Title defaults to the title registered with App.AddCommand.
Title string
// Default omits the command name from this command's usage line.
Default bool
// Usage returns optional descriptive text printed before flag defaults.
Usage func() string
// Bind defines this command's flags.
Bind func(*FlagSet)
// Run executes the command with its remaining positional arguments.
Run func(context.Context, []string) error
// Flags is populated by App.RunWithArgs with the flags defined by Bind.
// HelpCommand uses it to print command flag defaults.
Flags *FlagSet
}
Command is an individual command.
type CommandInfo ¶
CommandInfo is the constructor info for a command