Documentation
¶
Overview ¶
CLI package ¶
This package contains the implementation for a minimal opinionated flags framework similar to spf13/cobra. It all centers around the `cli.Command` type but provides less functionality.
To create a new CLI application:
```go app := cli.NewApp("mig") app.AddCommand("version", version.Name, version.New)
if err := app.Run(); err != nil {
return err
}
```
The `version.New` is a `func() *cli.Command`.
The Command type defines Name and Title as strings, equivallent to cobra `Command.Use` (Name) and `Command.Long` (Title). There is no equivalent of `Command.Short`.
The API choices are different, cobra's `AddCommand` took a command, and the command type was passed into Run().
The cli package creates a `CommandInfo` with AddCommand, and then calls the constructor of the `*Command` type. The type must have Run filled, and can implement Bind(*FlagSet) to read in CLI flags.
The Run function is context aware, supporting observability.
Index ¶
- Variables
- 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
Constants ¶
This section is empty.
Variables ¶
var ( BoolVar = pflag.BoolVar DurationVar = pflag.DurationVar Int64Var = pflag.Int64Var IntVar = pflag.IntVar StringVar = pflag.StringVar Uint64Var = pflag.Uint64Var UintVar = pflag.UintVar StringSliceVar = pflag.StringSliceVar BoolVarP = pflag.BoolVarP DurationVarP = pflag.DurationVarP Int64VarP = pflag.Int64VarP IntVarP = pflag.IntVarP StringVarP = pflag.StringVarP Uint64VarP = pflag.Uint64VarP UintVarP = pflag.UintVarP StringSliceVarP = pflag.StringSliceVarP PrintDefaults = pflag.PrintDefaults )
Flag variable binding functions from spf13/pflag.
Functions ¶
func ParseWithFlagSet ¶
ParseWithFlagSet parses flags and environment variables for a scoped FlagSet.
Types ¶
type App ¶
App is the cli entrypoint.
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 is a cli entrypoint which sets up a cancellable context for the command.
type Command ¶
type Command struct {
Name string
Title string
Default bool
Bind func(*FlagSet)
Run func(context.Context, []string) error
}
Command is an individual command.
type CommandInfo ¶
CommandInfo is the constructor info for a command