Documentation ¶
Overview ¶
golang CLI parameter handling
This is dynport's version of a golang CLI handler. Given a struct that implements an interface with fields that have annotations, an CLI is built that can be run against a list of strings (taken from os.Args for example).
See the basic example on how to use this library with a router. The other example shows the short annotation notation and the direct usage of actions without a router.
The following constraints or special behaviors are to be taken into account:
- Options (type "opt") are given in short or long form ("-h" vs. "--help"). Each option must have at least one modifier set.
- Required options must be present. A default value is preset in the struct.
- Options with a boolean value are internally handled as flags, i.e. presence of the flag indicates true.
- Ordering of arguments is defined by the position in the action's struct (first come first serve).
- Arguments (type "arg") may be variadic (type in the struct must be a slice), i.e. arbitrary can be given. If the argument is required, at least one value must be present. Only the last arguments can be variadic.
- Non variadic arguments must always be given.
- Routes use a fuzzy matching algorithm, i.e. for `do something` its sufficient to give `d s` as long as the fragments are not ambiguous.
Example (Basic) ¶
Basic example that shows how to register an action to a route and execute it.
package main // Struct used to configure an action. type ExampleRunner struct { Verbose bool `cli:"type=opt short=v long=verbose"` // Flag (boolean option) example. This is either set or not. Command string `cli:"type=opt short=c long=command required=true"` // Option that has a default value. Hosts string `cli:"type=arg required=true"` // Argument with at least one required. } // Run the action. Called when the cli.Run function is called with a route matching the one of this action. func (er *ExampleRunner) Run() error { // Called when action matches route. if er.Verbose { logger.Printf("Going to execute %q at the following hosts: %v", er.Command, er.Hosts) } // [..] Executing the SSH command is left to the reader. return nil } // Basic example that shows how to register an action to a route and execute it. func main() { router := NewRouter() router.Register("run/on/hosts", &ExampleRunner{}, "This is an example that pretends to run a given command on a set of hosts.") router.Run("run", "on", "host", "-v", "-c", "uname -a", "192.168.1.1") router.RunWithArgs() // Run with args given on the command line. }
Output:
Example (ShortNotation) ¶
Example that shows how to run a single action without using a router.
package main import ( "log" ) // Struct used to configure an action. type ExampleShortNotationRunner struct { Verbose bool `cli:"opt -v --verbose"` // Flag (boolean option) example. This is either set or not. Command string `cli:"opt -c --command required"` // Option that has a default value. Hosts string `cli:"arg required"` // Argument with at least one required. } // Run the action. Called when the cli.Run function is called with a route matching the one of this action. func (er *ExampleShortNotationRunner) Run() error { // Called when action matches route. if er.Verbose { log.Printf("Going to execute %q at the following hosts: %v", er.Command, er.Hosts) } // [..] Executing the SSH command is left to the reader. return nil } // Example that shows how to run a single action without using a router. func main() { RunAction(&ExampleShortNotationRunner{}, "-v", "-c", "uname -a", "192.168.1.1") RunActionWithArgs(&ExampleShortNotationRunner{}) }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrorNoRoute = fmt.Errorf("no route matched") ErrorHelpRequested = fmt.Errorf("help requested") )
Functions ¶
func RunAction ¶
Run the given action with given arguments. This is useful for programms that don't need the routing features, i.e. only have one action to run. The example on the short notation uses this feature.
func RunActionWithArgs ¶
Run the given action with the arguments given on the command line.
Types ¶
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
The basic datastructure used in cli. Actions are added for different paths to a router via the "Register" and "RegisterFunc" methods. These actions can be executed using the "Run" and "RunWithArgs" methods.
func NewRouter ¶
func NewRouter() *Router
Create a new router that will be used to register and run the actions of the application.
func (*Router) Register ¶
Register the given action (some struct implementing the Runner interface) for the given route.
func (*Router) RegisterFunc ¶
Register the given function as handler for the given route. This is a shortcut for actions that don't need options or arguments. A description can be provided as an optional argument.
func (*Router) Run ¶
Run the given arguments against the registered actions, i.e. try to find a matching route and run the according action.
func (*Router) RunWithArgs ¶
Run the arguments from the commandline (aka os.Args) against the registered actions, i.e. try to find a matching route and run the according action.