forked from Shiloh/githaven
792b4dba2c
* update github.com/blevesearch/bleve v2.0.2 -> v2.0.3 * github.com/denisenkom/go-mssqldb v0.9.0 -> v0.10.0 * github.com/editorconfig/editorconfig-core-go v2.4.1 -> v2.4.2 * github.com/go-chi/cors v1.1.1 -> v1.2.0 * github.com/go-git/go-billy v5.0.0 -> v5.1.0 * github.com/go-git/go-git v5.2.0 -> v5.3.0 * github.com/go-ldap/ldap v3.2.4 -> v3.3.0 * github.com/go-redis/redis v8.6.0 -> v8.8.2 * github.com/go-sql-driver/mysql v1.5.0 -> v1.6.0 * github.com/go-swagger/go-swagger v0.26.1 -> v0.27.0 * github.com/lib/pq v1.9.0 -> v1.10.1 * github.com/mattn/go-sqlite3 v1.14.6 -> v1.14.7 * github.com/go-testfixtures/testfixtures v3.5.0 -> v3.6.0 * github.com/issue9/identicon v1.0.1 -> v1.2.0 * github.com/klauspost/compress v1.11.8 -> v1.12.1 * github.com/mgechev/revive v1.0.3 -> v1.0.6 * github.com/microcosm-cc/bluemonday v1.0.7 -> v1.0.8 * github.com/niklasfasching/go-org v1.4.0 -> v1.5.0 * github.com/olivere/elastic v7.0.22 -> v7.0.24 * github.com/pelletier/go-toml v1.8.1 -> v1.9.0 * github.com/prometheus/client_golang v1.9.0 -> v1.10.0 * github.com/xanzy/go-gitlab v0.44.0 -> v0.48.0 * github.com/yuin/goldmark v1.3.3 -> v1.3.5 * github.com/6543/go-version v1.2.4 -> v1.3.1 * do github.com/lib/pq v1.10.0 -> v1.10.1 again ...
140 lines
5.0 KiB
Markdown
Vendored
140 lines
5.0 KiB
Markdown
Vendored
go-flags: a go library for parsing command line arguments
|
|
=========================================================
|
|
|
|
[![GoDoc](https://godoc.org/github.com/jessevdk/go-flags?status.png)](https://godoc.org/github.com/jessevdk/go-flags) [![Build Status](https://travis-ci.org/jessevdk/go-flags.svg?branch=master)](https://travis-ci.org/jessevdk/go-flags) [![Coverage Status](https://img.shields.io/coveralls/jessevdk/go-flags.svg)](https://coveralls.io/r/jessevdk/go-flags?branch=master)
|
|
|
|
This library provides similar functionality to the builtin flag library of
|
|
go, but provides much more functionality and nicer formatting. From the
|
|
documentation:
|
|
|
|
Package flags provides an extensive command line option parser.
|
|
The flags package is similar in functionality to the go builtin flag package
|
|
but provides more options and uses reflection to provide a convenient and
|
|
succinct way of specifying command line options.
|
|
|
|
Supported features:
|
|
* Options with short names (-v)
|
|
* Options with long names (--verbose)
|
|
* Options with and without arguments (bool v.s. other type)
|
|
* Options with optional arguments and default values
|
|
* Multiple option groups each containing a set of options
|
|
* Generate and print well-formatted help message
|
|
* Passing remaining command line arguments after -- (optional)
|
|
* Ignoring unknown command line options (optional)
|
|
* Supports -I/usr/include -I=/usr/include -I /usr/include option argument specification
|
|
* Supports multiple short options -aux
|
|
* Supports all primitive go types (string, int{8..64}, uint{8..64}, float)
|
|
* Supports same option multiple times (can store in slice or last option counts)
|
|
* Supports maps
|
|
* Supports function callbacks
|
|
* Supports namespaces for (nested) option groups
|
|
|
|
The flags package uses structs, reflection and struct field tags
|
|
to allow users to specify command line options. This results in very simple
|
|
and concise specification of your application options. For example:
|
|
|
|
```go
|
|
type Options struct {
|
|
Verbose []bool `short:"v" long:"verbose" description:"Show verbose debug information"`
|
|
}
|
|
```
|
|
|
|
This specifies one option with a short name -v and a long name --verbose.
|
|
When either -v or --verbose is found on the command line, a 'true' value
|
|
will be appended to the Verbose field. e.g. when specifying -vvv, the
|
|
resulting value of Verbose will be {[true, true, true]}.
|
|
|
|
Example:
|
|
--------
|
|
```go
|
|
var opts struct {
|
|
// Slice of bool will append 'true' each time the option
|
|
// is encountered (can be set multiple times, like -vvv)
|
|
Verbose []bool `short:"v" long:"verbose" description:"Show verbose debug information"`
|
|
|
|
// Example of automatic marshalling to desired type (uint)
|
|
Offset uint `long:"offset" description:"Offset"`
|
|
|
|
// Example of a callback, called each time the option is found.
|
|
Call func(string) `short:"c" description:"Call phone number"`
|
|
|
|
// Example of a required flag
|
|
Name string `short:"n" long:"name" description:"A name" required:"true"`
|
|
|
|
// Example of a flag restricted to a pre-defined set of strings
|
|
Animal string `long:"animal" choice:"cat" choice:"dog"`
|
|
|
|
// Example of a value name
|
|
File string `short:"f" long:"file" description:"A file" value-name:"FILE"`
|
|
|
|
// Example of a pointer
|
|
Ptr *int `short:"p" description:"A pointer to an integer"`
|
|
|
|
// Example of a slice of strings
|
|
StringSlice []string `short:"s" description:"A slice of strings"`
|
|
|
|
// Example of a slice of pointers
|
|
PtrSlice []*string `long:"ptrslice" description:"A slice of pointers to string"`
|
|
|
|
// Example of a map
|
|
IntMap map[string]int `long:"intmap" description:"A map from string to int"`
|
|
}
|
|
|
|
// Callback which will invoke callto:<argument> to call a number.
|
|
// Note that this works just on OS X (and probably only with
|
|
// Skype) but it shows the idea.
|
|
opts.Call = func(num string) {
|
|
cmd := exec.Command("open", "callto:"+num)
|
|
cmd.Start()
|
|
cmd.Process.Release()
|
|
}
|
|
|
|
// Make some fake arguments to parse.
|
|
args := []string{
|
|
"-vv",
|
|
"--offset=5",
|
|
"-n", "Me",
|
|
"--animal", "dog", // anything other than "cat" or "dog" will raise an error
|
|
"-p", "3",
|
|
"-s", "hello",
|
|
"-s", "world",
|
|
"--ptrslice", "hello",
|
|
"--ptrslice", "world",
|
|
"--intmap", "a:1",
|
|
"--intmap", "b:5",
|
|
"arg1",
|
|
"arg2",
|
|
"arg3",
|
|
}
|
|
|
|
// Parse flags from `args'. Note that here we use flags.ParseArgs for
|
|
// the sake of making a working example. Normally, you would simply use
|
|
// flags.Parse(&opts) which uses os.Args
|
|
args, err := flags.ParseArgs(&opts, args)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Printf("Verbosity: %v\n", opts.Verbose)
|
|
fmt.Printf("Offset: %d\n", opts.Offset)
|
|
fmt.Printf("Name: %s\n", opts.Name)
|
|
fmt.Printf("Animal: %s\n", opts.Animal)
|
|
fmt.Printf("Ptr: %d\n", *opts.Ptr)
|
|
fmt.Printf("StringSlice: %v\n", opts.StringSlice)
|
|
fmt.Printf("PtrSlice: [%v %v]\n", *opts.PtrSlice[0], *opts.PtrSlice[1])
|
|
fmt.Printf("IntMap: [a:%v b:%v]\n", opts.IntMap["a"], opts.IntMap["b"])
|
|
fmt.Printf("Remaining args: %s\n", strings.Join(args, " "))
|
|
|
|
// Output: Verbosity: [true true]
|
|
// Offset: 5
|
|
// Name: Me
|
|
// Ptr: 3
|
|
// StringSlice: [hello world]
|
|
// PtrSlice: [hello world]
|
|
// IntMap: [a:1 b:5]
|
|
// Remaining args: arg1 arg2 arg3
|
|
```
|
|
|
|
More information can be found in the godocs: <http://godoc.org/github.com/jessevdk/go-flags>
|