Kong
Kong is a command-line parser for Go
Install / Use
/learn @alecthomas/KongREADME
Kong is a command-line parser for Go
- Version 1.0.0 Release
- Introduction
- Help
- Command handling
- Hooks: BeforeReset(), BeforeResolve(), BeforeApply(), AfterApply()
- The Bind() option
- Flags
- Commands and sub-commands
- Branching positional arguments
- Positional arguments
- Slices
- Maps
- Pointers
- Nested data structure
- Custom named decoders
- Supported field types
- Custom decoders (mappers)
- Supported tags
- Plugins
- Dynamic Commands
- Variable interpolation
- Validation
- Modifying Kong's behaviour
Name(help)andDescription(help)- set the application name descriptionConfiguration(loader, paths...)- load defaults from configuration filesResolver(...)- support for default values from external sources*Mapper(...)- customising how the command-line is mapped to Go valuesConfigureHelp(HelpOptions)andHelp(HelpFunc)- customising help- Injecting values into
Run()methods - Other options
Version 1.0.0 Release
Kong has been stable for a long time, so it seemed appropriate to cut a 1.0 release.
There is one breaking change, #436, which should effect relatively few users.
Introduction
Kong aims to support arbitrarily complex command-line structures with as little developer effort as possible.
To achieve that, command-lines are expressed as Go types, with the structure and tags directing how the command line is mapped onto the struct.
For example, the following command-line:
shell rm [-f] [-r] <paths> ...
shell ls [<paths> ...]
Can be represented by the following command-line structure:
package main
import "github.com/alecthomas/kong"
var CLI struct {
Rm struct {
Force bool `help:"Force removal."`
Recursive bool `help:"Recursively remove files."`
Paths []string `arg:"" name:"path" help:"Paths to remove." type:"path"`
} `cmd:"" help:"Remove files."`
Ls struct {
Paths []string `arg:"" optional:"" name:"path" help:"Paths to list." type:"path"`
} `cmd:"" help:"List paths."`
}
func main() {
ctx := kong.Parse(&CLI)
switch ctx.Command() {
case "rm <path>":
case "ls":
default:
panic(ctx.Command())
}
}
Help
Help as a user of a Kong application
Every Kong application includes a --help flag that will display auto-generated help.
eg.
$ shell --help
usage: shell <command>
A shell-like example app.
Flags:
--help Show context-sensitive help.
--debug Debug mode.
Commands:
rm <path> ...
Remove files.
ls [<path> ...]
List paths.
If a command is provided, the help will show full detail on the command including all available flags.
eg.
$ shell --help rm
usage: shell rm <paths> ...
Remove files.
Arguments:
<paths> ... Paths to remove.
Flags:
--debug Debug mode.
-f, --force Force removal.
-r, --recursive Recursively remove files.
Defining help in Kong
Help is automatically generated from the command-line structure itself,
including help:"" and other tags. Variables will
also be interpolated into the help string.
Finally, any command, or argument type implementing the interface
Help() string will have this function called to retrieve more detail to
augment the help tag. This allows for much more descriptive text than can
fit in Go tags. See _examples/shell/help
Showing the command's detailed help
A command's additional help text is not shown from top-level help, but can be displayed within contextual help:
Top level help
$ go run ./_examples/shell/help --help
Usage: help <command>
An app demonstrating HelpProviders
Flags:
-h, --help Show context-sensitive help.
--flag Regular flag help
Commands:
echo Regular command help
Contextual
$ go run ./_examples/shell/help echo --help
Usage: help echo <msg>
Regular command help
🚀 additional command help
Arguments:
<msg> Regular argument help
Flags:
-h, --help Show context-sensitive help.
--flag Regular flag help
Showing an argument's detailed help
Custom help will only be shown for positional arguments with named fields (see the README section on positional arguments for more details on what that means)
Contextual argument help
$ go run ./_examples/shell/help msg --help
Usage: help echo <msg>
Regular argument help
📣 additional argument help
Flags:
-h, --help Show context-sensitive help.
--flag Regular flag help
Command handling
There are two ways to handle commands in Kong.
Switch on the command string
When you call kong.Parse() it will return a unique string representation of the command. Each command branch in the hierarchy will be a bare word and each branching argument or required positional argument will be the name surrounded by angle brackets. Here's an example:
There's an example of this pattern here.
eg.
package main
import "github.com/alecthomas/kong"
var CLI struct {
Rm struct {
Force bool `help:"Force removal."`
Recursive bool `help:"Recursively remove files."`
Paths []string `arg:"" name:"path" help:"Paths to remove." type:"path"`
} `cmd:"" help:"Remove files."`
Ls struct {
Paths []string `arg:"" optional:"" name:"path" help:"Paths to list." type:"path"`
} `cmd:"" help:"List paths."`
}
func main() {
ctx := kong.Parse(&CLI)
switch ctx.Command() {
case "rm <path>":
case "ls":
default:
panic(ctx.Command())
}
}
This has the advantage that it is convenient, but the downside that if you modify your CLI structure, the strings may change. This can be fragile.
Attach a Run(...) error method to each command
A more robust approach is to break each command out into their own structs:
- Break leaf commands out into separate structs.
- Attach a
Run(...) errormethod to all leaf commands. - Call
kong.Kong.Parse()to obtain akong.Context. - Call
kong.Context.Run(bindings...)to call the selected parsed command.
Once a command node is selected by Kong it will search from that node back to the root. Each
encountered command node with a Run(...) error will be called in reverse order. This allows
sub-trees to be re-used fairly conveniently.
In addition to values bound with the kong.Bind(...) option, any values
passed through to kong.Context.Run(...) are also bindable to the target's
Run() arguments.
Finally, hooks can also contribute bindings via kong.Context.Bind() and kong.Context.BindTo().
There's a full example emulating part of the Docker CLI here.
eg.
type Context struct {
Debug bool
}
type RmCmd struct {
Force bool `help:"Force removal."`
Recursive bool `help:"Recursively remove files."`
Paths []string `arg:"" name:"path" help:"Paths to remove." type:"path"`
}
func (r *RmCmd) Run(ctx *Context) error {
fmt.Println("rm", r.Paths)
return nil
}
type LsCmd struct {
Paths []string `arg:"" optional:"" name:"path" help:"Paths to list." type:"path"`
}
func (l *LsCmd) Run(ctx *Context) error {
fmt.Println("ls", l.Paths)
return nil
}
var cli struct {
Debug bool `help:"Enable debug mode."`
Rm RmCmd `cmd:"" help:"Remove files."`
Ls LsCmd `cmd:"" help:"List paths."`
}
func main() {
ctx := kong.Parse(&cli)
// Call the Run() method of the selected parsed command.
err := ctx.Run(&Context{Debug: cli.Debug})
ctx.FatalIfErrorf(err)
}
Hooks: BeforeReset(), BeforeResolve(), BeforeApply(), AfterApply()
If a node in the CLI, or any of its embedded fields, implements a BeforeReset(...) error, BeforeResolve (...) error, BeforeApply(...) error and/or AfterApply(...) error method, those will be called as Kong
resets, resolves, validates, and assigns values to the node.
| Hook | Description
