Update TOC.

This commit is contained in:
Alec Thomas
2018-09-13 17:13:48 +10:00
parent 63a402d3b2
commit 9cbf4a5d4b
+48 -24
View File
@@ -3,35 +3,36 @@
# Kong is a command-line parser for Go [![](https://godoc.org/github.com/alecthomas/kong?status.svg)](http://godoc.org/github.com/alecthomas/kong) [![CircleCI](https://img.shields.io/circleci/project/github/alecthomas/kong.svg)](https://circleci.com/gh/alecthomas/kong) # Kong is a command-line parser for Go [![](https://godoc.org/github.com/alecthomas/kong?status.svg)](http://godoc.org/github.com/alecthomas/kong) [![CircleCI](https://img.shields.io/circleci/project/github/alecthomas/kong.svg)](https://circleci.com/gh/alecthomas/kong)
<!-- MarkdownTOC --> <!-- TOC -->
1. [Introduction](#introduction) 1. [Introduction](#introduction)
1. [Help](#help) 2. [Help](#help)
1. [Command handling](#command-handling) 3. [Command handling](#command-handling)
1. [Switch on the command string](#switch-on-the-command-string) 1. [Switch on the command string](#switch-on-the-command-string)
1. [Attach a `Run(...) error` method to each command](#attach-a-run-error-method-to-each-command) 2. [Attach a `Run(...) error` method to each command](#attach-a-run-error-method-to-each-command)
1. [BeforeApply\(\), AfterApply\(\) and the Bind\(\) option](#BeforeApply-AfterApply-and-the-bind-option) 4. [Hooks: BeforeResolve(), BeforeApply(), AfterApply() and the Bind() option](#hooks-beforeresolve-beforeapply-afterapply-and-the-bind-option)
1. [Flags](#flags) 5. [Flags](#flags)
1. [Commands and sub-commands](#commands-and-sub-commands) 6. [Commands and sub-commands](#commands-and-sub-commands)
1. [Branching positional arguments](#branching-positional-arguments) 7. [Branching positional arguments](#branching-positional-arguments)
1. [Terminating positional arguments](#terminating-positional-arguments) 8. [Terminating positional arguments](#terminating-positional-arguments)
1. [Slices](#slices) 9. [Slices](#slices)
1. [Maps](#maps) 10. [Maps](#maps)
1. [Custom named decoders](#custom-named-decoders) 11. [Custom named decoders](#custom-named-decoders)
1. [Custom decoders \(mappers\)](#custom-decoders-mappers) 12. [Custom decoders (mappers)](#custom-decoders-mappers)
1. [Supported tags](#supported-tags) 13. [Supported tags](#supported-tags)
1. [Variable interpolation](#variable-interpolation) 14. [Variable interpolation](#variable-interpolation)
1. [Modifying Kong's behaviour](#modifying-kongs-behaviour) 15. [Modifying Kong's behaviour](#modifying-kongs-behaviour)
1. [`Name(help)` and `Description(help)` - set the application name description](#namehelp-and-descriptionhelp---set-the-application-name-description) 1. [`Name(help)` and `Description(help)` - set the application name description](#namehelp-and-descriptionhelp---set-the-application-name-description)
1. [`Configuration(loader, paths...)` - load defaults from configuration files](#configurationloader-paths---load-defaults-from-configuration-files) 2. [`Configuration(loader, paths...)` - load defaults from configuration files](#configurationloader-paths---load-defaults-from-configuration-files)
1. [`Resolver(...)` - support for default values from external sources](#resolver---support-for-default-values-from-external-sources) 3. [`Resolver(...)` - support for default values from external sources](#resolver---support-for-default-values-from-external-sources)
1. [`*Mapper(...)` - customising how the command-line is mapped to Go values](#mapper---customising-how-the-command-line-is-mapped-to-go-values) 4. [`*Mapper(...)` - customising how the command-line is mapped to Go values](#mapper---customising-how-the-command-line-is-mapped-to-go-values)
1. [`ConfigureHelp(HelpOptions)` and `Help(HelpFunc)` - customising help](#configurehelphelpoptions-and-helphelpfunc---customising-help) 5. [`ConfigureHelp(HelpOptions)` and `Help(HelpFunc)` - customising help](#configurehelphelpoptions-and-helphelpfunc---customising-help)
1. [`Bind(...)` - bind values for callback hooks and Run\(\) methods](#bind---bind-values-for-callback-hooks-and-run-methods) 6. [`Bind(...)` - bind values for callback hooks and Run() methods](#bind---bind-values-for-callback-hooks-and-run-methods)
1. [Other options](#other-options) 7. [Other options](#other-options)
<!-- /MarkdownTOC --> <!-- /TOC -->
<a id="markdown-introduction" name="introduction"></a>
## Introduction ## Introduction
Kong aims to support arbitrarily complex command-line structures with as little developer effort as possible. Kong aims to support arbitrarily complex command-line structures with as little developer effort as possible.
@@ -74,6 +75,7 @@ func main() {
} }
``` ```
<a id="markdown-help" name="help"></a>
## Help ## Help
Help is automatically generated. With no other arguments provided, help will display a full summary of all available commands. Help is automatically generated. With no other arguments provided, help will display a full summary of all available commands.
@@ -114,10 +116,12 @@ eg.
-f, --force Force removal. -f, --force Force removal.
-r, --recursive Recursively remove files. -r, --recursive Recursively remove files.
<a id="markdown-command-handling" name="command-handling"></a>
## Command handling ## Command handling
There are two ways to handle commands in Kong. There are two ways to handle commands in Kong.
<a id="markdown-switch-on-the-command-string" name="switch-on-the-command-string"></a>
### Switch on the command string ### 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: 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:
@@ -157,6 +161,7 @@ func main() {
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. 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.
<a id="markdown-attach-a-run-error-method-to-each-command" name="attach-a-run-error-method-to-each-command"></a>
### Attach a `Run(...) error` method to each command ### Attach a `Run(...) error` method to each command
A more robust approach is to break each command out into their own structs: A more robust approach is to break each command out into their own structs:
@@ -212,7 +217,8 @@ func main() {
``` ```
## Hooks: BeforeResolve(), BeforeSet(), AfterSet() and the Bind() option <a id="markdown-hooks-beforeresolve-beforeapply-afterapply-and-the-bind-option" name="hooks-beforeresolve-beforeapply-afterapply-and-the-bind-option"></a>
## Hooks: BeforeResolve(), BeforeApply(), AfterApply() and the Bind() option
If a node in the grammar has a `BeforeResolve(...)`, `BeforeApply(...) error` and/or `AfterApply(...) error` method, those methods will be called before validation/assignment and after validation/assignment, respectively. If a node in the grammar has a `BeforeResolve(...)`, `BeforeApply(...) error` and/or `AfterApply(...) error` method, those methods will be called before validation/assignment and after validation/assignment, respectively.
@@ -245,6 +251,7 @@ func main() {
} }
``` ```
<a id="markdown-flags" name="flags"></a>
## Flags ## Flags
Any [mapped](#mapper---customising-how-the-command-line-is-mapped-to-go-values) field in the command structure *not* tagged with `cmd` or `arg` will be a flag. Flags are optional by default. Any [mapped](#mapper---customising-how-the-command-line-is-mapped-to-go-values) field in the command structure *not* tagged with `cmd` or `arg` will be a flag. Flags are optional by default.
@@ -257,6 +264,7 @@ type CLI struct {
} }
``` ```
<a id="markdown-commands-and-sub-commands" name="commands-and-sub-commands"></a>
## Commands and sub-commands ## Commands and sub-commands
Sub-commands are specified by tagging a struct field with `cmd`. Kong supports arbitrarily nested commands. Sub-commands are specified by tagging a struct field with `cmd`. Kong supports arbitrarily nested commands.
@@ -274,6 +282,7 @@ type CLI struct {
} }
``` ```
<a id="markdown-branching-positional-arguments" name="branching-positional-arguments"></a>
## Branching positional arguments ## Branching positional arguments
In addition to sub-commands, structs can also be configured as branching positional arguments. In addition to sub-commands, structs can also be configured as branching positional arguments.
@@ -301,12 +310,14 @@ var CLI struct {
This looks a little verbose in this contrived example, but typically this will not be the case. This looks a little verbose in this contrived example, but typically this will not be the case.
<a id="markdown-terminating-positional-arguments" name="terminating-positional-arguments"></a>
## Terminating positional arguments ## Terminating positional arguments
If a [mapped type](#mapper---customising-how-the-command-line-is-mapped-to-go-values) is tagged with `arg` it will be treated as the final positional values to be parsed on the command line. If a [mapped type](#mapper---customising-how-the-command-line-is-mapped-to-go-values) is tagged with `arg` it will be treated as the final positional values to be parsed on the command line.
If a positional argument is a slice, all remaining arguments will be appended to that slice. If a positional argument is a slice, all remaining arguments will be appended to that slice.
<a id="markdown-slices" name="slices"></a>
## Slices ## Slices
Slice values are treated specially. First the input is split on the `sep:"<rune>"` tag (defaults to `,`), then each element is parsed by the slice element type and appended to the slice. If the same value is encountered multiple times, elements continue to be appended. Slice values are treated specially. First the input is split on the `sep:"<rune>"` tag (defaults to `,`), then each element is parsed by the slice element type and appended to the slice. If the same value is encountered multiple times, elements continue to be appended.
@@ -325,6 +336,7 @@ var CLI struct {
} }
``` ```
<a id="markdown-maps" name="maps"></a>
## Maps ## Maps
Maps are similar to slices except that only one key/value pair can be assigned per value, and the `sep` tag denotes the assignment character and defaults to `=`. Maps are similar to slices except that only one key/value pair can be assigned per value, and the `sep` tag denotes the assignment character and defaults to `=`.
@@ -347,6 +359,7 @@ var CLI struct {
For flags, multiple key+value pairs should be separated by `;` eg. `--set="key1=value1;key2=value2"`. For flags, multiple key+value pairs should be separated by `;` eg. `--set="key1=value1;key2=value2"`.
<a id="markdown-custom-named-decoders" name="custom-named-decoders"></a>
## Custom named decoders ## Custom named decoders
Kong includes a number of builtin custom type mappers. These can be used by Kong includes a number of builtin custom type mappers. These can be used by
@@ -365,11 +378,13 @@ specifies the element type. For maps, the tag has the format
`tag:"[<key>]:[<value>]"` where either may be omitted. `tag:"[<key>]:[<value>]"` where either may be omitted.
<a id="markdown-custom-decoders-mappers" name="custom-decoders-mappers"></a>
## Custom decoders (mappers) ## Custom decoders (mappers)
If a field implements the [MapperValue](https://godoc.org/github.com/alecthomas/kong#MapperValue) If a field implements the [MapperValue](https://godoc.org/github.com/alecthomas/kong#MapperValue)
interface it will be used to decode arguments into the field. interface it will be used to decode arguments into the field.
<a id="markdown-supported-tags" name="supported-tags"></a>
## Supported tags ## Supported tags
Tags can be in two forms: Tags can be in two forms:
@@ -399,6 +414,7 @@ Both can coexist with standard Tag parsing.
| `group:"X"` | Logical group for a flag or command. | | `group:"X"` | Logical group for a flag or command. |
| `prefix:"X"` | Prefix for all sub-flags. | | `prefix:"X"` | Prefix for all sub-flags. |
<a id="markdown-variable-interpolation" name="variable-interpolation"></a>
## Variable interpolation ## Variable interpolation
Kong supports limited variable interpolation into help strings, enum lists and Kong supports limited variable interpolation into help strings, enum lists and
@@ -433,12 +449,14 @@ func main() {
} }
``` ```
<a id="markdown-modifying-kongs-behaviour" name="modifying-kongs-behaviour"></a>
## Modifying Kong's behaviour ## Modifying Kong's behaviour
Each Kong parser can be configured via functional options passed to `New(cli interface{}, options...Option)`. Each Kong parser can be configured via functional options passed to `New(cli interface{}, options...Option)`.
The full set of options can be found [here](https://godoc.org/github.com/alecthomas/kong#Option). The full set of options can be found [here](https://godoc.org/github.com/alecthomas/kong#Option).
<a id="markdown-namehelp-and-descriptionhelp---set-the-application-name-description" name="namehelp-and-descriptionhelp---set-the-application-name-description"></a>
### `Name(help)` and `Description(help)` - set the application name description ### `Name(help)` and `Description(help)` - set the application name description
Set the application name and/or description. Set the application name and/or description.
@@ -447,6 +465,7 @@ The name of the application will default to the binary name, but can be overridd
As with all help in Kong, text will be wrapped to the terminal. As with all help in Kong, text will be wrapped to the terminal.
<a id="markdown-configurationloader-paths---load-defaults-from-configuration-files" name="configurationloader-paths---load-defaults-from-configuration-files"></a>
### `Configuration(loader, paths...)` - load defaults from configuration files ### `Configuration(loader, paths...)` - load defaults from configuration files
This option provides Kong with support for loading defaults from a set of configuration files. Each file is opened, if possible, and the loader called to create a resolver for that file. This option provides Kong with support for loading defaults from a set of configuration files. Each file is opened, if possible, and the loader called to create a resolver for that file.
@@ -457,12 +476,14 @@ eg.
kong.Parse(&cli, kong.Configuration(kong.JSON, "/etc/myapp.json", "~/.myapp.json")) kong.Parse(&cli, kong.Configuration(kong.JSON, "/etc/myapp.json", "~/.myapp.json"))
``` ```
<a id="markdown-resolver---support-for-default-values-from-external-sources" name="resolver---support-for-default-values-from-external-sources"></a>
### `Resolver(...)` - support for default values from external sources ### `Resolver(...)` - support for default values from external sources
Resolvers are Kong's extension point for providing default values from external sources. As an example, support for environment variables via the `env` tag is provided by a resolver. There's also a builtin resolver for JSON configuration files. Resolvers are Kong's extension point for providing default values from external sources. As an example, support for environment variables via the `env` tag is provided by a resolver. There's also a builtin resolver for JSON configuration files.
Example resolvers can be found in [resolver.go](https://github.com/alecthomas/kong/blob/master/resolver.go). Example resolvers can be found in [resolver.go](https://github.com/alecthomas/kong/blob/master/resolver.go).
<a id="markdown-mapper---customising-how-the-command-line-is-mapped-to-go-values" name="mapper---customising-how-the-command-line-is-mapped-to-go-values"></a>
### `*Mapper(...)` - customising how the command-line is mapped to Go values ### `*Mapper(...)` - customising how the command-line is mapped to Go values
Command-line arguments are mapped to Go values via the Mapper interface: Command-line arguments are mapped to Go values via the Mapper interface:
@@ -485,6 +506,7 @@ All builtin Go types (as well as a bunch of useful stdlib types like `time.Time`
3. `TypeMapper(reflect.Type, Mapper)`. 3. `TypeMapper(reflect.Type, Mapper)`.
4. `ValueMapper(interface{}, Mapper)`, passing in a pointer to a field of the grammar. 4. `ValueMapper(interface{}, Mapper)`, passing in a pointer to a field of the grammar.
<a id="markdown-configurehelphelpoptions-and-helphelpfunc---customising-help" name="configurehelphelpoptions-and-helphelpfunc---customising-help"></a>
### `ConfigureHelp(HelpOptions)` and `Help(HelpFunc)` - customising help ### `ConfigureHelp(HelpOptions)` and `Help(HelpFunc)` - customising help
The default help output is usually sufficient, but if not there are two solutions. The default help output is usually sufficient, but if not there are two solutions.
@@ -492,10 +514,12 @@ The default help output is usually sufficient, but if not there are two solution
1. Use `ConfigureHelp(HelpOptions)` to configure how help is formatted (see [HelpOptions](https://godoc.org/github.com/alecthomas/kong#HelpOptions) for details). 1. Use `ConfigureHelp(HelpOptions)` to configure how help is formatted (see [HelpOptions](https://godoc.org/github.com/alecthomas/kong#HelpOptions) for details).
2. Custom help can be wired into Kong via the `Help(HelpFunc)` option. The `HelpFunc` is passed a `Context`, which contains the parsed context for the current command-line. See the implementation of `PrintHelp` for an example. 2. Custom help can be wired into Kong via the `Help(HelpFunc)` option. The `HelpFunc` is passed a `Context`, which contains the parsed context for the current command-line. See the implementation of `PrintHelp` for an example.
<a id="markdown-bind---bind-values-for-callback-hooks-and-run-methods" name="bind---bind-values-for-callback-hooks-and-run-methods"></a>
### `Bind(...)` - bind values for callback hooks and Run() methods ### `Bind(...)` - bind values for callback hooks and Run() methods
See the [section on hooks](#BeforeApply-AfterApply-and-the-bind-option) for details. See the [section on hooks](#BeforeApply-AfterApply-and-the-bind-option) for details.
<a id="markdown-other-options" name="other-options"></a>
### Other options ### Other options
The full set of options can be found [here](https://godoc.org/github.com/alecthomas/kong#Option). The full set of options can be found [here](https://godoc.org/github.com/alecthomas/kong#Option).