ab5cf7e6ef7323c2c6726612f06181833ed123b4
* Add tracing to the parser. * Synthesize a --help flag. * Parsing now occurs in multiple phases. 1. Reset target. 2. Parse command-line into a "trace" (no values are written to target). 3. Apply traced, parsed values to the target fields. This is another step in facilitating context-sensitive help and completion. * Detect duplicate flags.
Kong is a command-line parser for Go 
It parses a command-line into a struct. eg.
package main
import "github.com/alecthomas/kong"
var CLI struct {
Rm struct {
Force bool `kong:"help='Force removal.'"`
Recursive bool `kong:"help='Recursively remove files.'"`
Paths []string `kong:"help='Paths to remove.',type='path'"`
} `kong:"help='Remove files.'"`
Ls struct {
Paths []string `kong:"help='Paths to list.',type='path'"`
} `kong:"help='List paths.'"`
}
func main() {
kong.Parse(&CLI)
}
Decoders
Command-line arguments are mapped to Go values via the Decoder interface:
// A Decoder knows how to decode text into a Go value.
type Decoder interface {
// Decode scan into target.
//
// "ctx" contains context about the value being decoded that may be useful
// to some decoders.
Decode(ctx *DecoderContext, scan *Scanner, target reflect.Value) error
}
All builtin Go types (as well as a bunch of useful stdlib types like time.Time) have decoders registered by default. Decoders for custom types can be added using kong.RegisterDecoder(decoder). Decoders are mapped from fields in three ways:
- By registering a
kong.NamedDecoderand using the tagtype:"<name>". - By registering a
kong.KindDecoderwith areflect.Kind. - By registering a
kong.TypeDecoderwith areflect.Type.
Description
Languages
Go
99.6%
Shell
0.4%