cli
Note: I don't really like this structure any more. I've opted for a more
simple, reflection-free structure. See https://github.com/pnelson/cli and
consider this package deprecated.
Package cli provides structure for command line applications with sub-commands.
This package uses a touch of reflection magic to dispatch to a method with
named arguments. Commands help and version are implemented by default. The
usage information is pretty printed in an opinionated format.
The most basic cli application is boring:
app := cli.New("myapp", "0.0.1")
app.Run()
Build and run your application:
$ ./myapp
Usage: myapp [options] []
help Output this usage information.
version Output the application version
Add commands:
type add struct {
showExtra *bool
example *string
number *int
}
func (c *add) Flags(flags *flag.FlagSet) {
c.showExtra = flags.Bool("show-extra", false, "Print extra arguments.")
c.example = flags.String("example", "", "An example string option.")
c.number = flags.Int("number", 0, "An example int option.")
}
func (c *add) Run(key, username string, extra []string) {
fmt.Printf("%s => %s\n", key, username)
if *c.showExtra {
fmt.Printf(" %v\n", extra)
}
}
func (c *add) String() string {
return "Add record key with username."
}
func main() {
app := cli.New("myapp", "0.0.1")
app.Rule(&add{}, "add", " []")
app.Run()
}
Try building and running your application again:
$ ./myapp
Usage: myapp [options] []
help Output this usage information.
version Output the application version.
add [options] [] Add record key with username.
-example= An example string option.
-number= An example int option.
-show-extra Print extra arguments.
Copyright (c) 2014 by Philip Nelson. See LICENSE for details.