pax_global_header00006660000000000000000000000064122423605120014507gustar00rootroot0000000000000052 comment=37c8226983775d404b6edfebd44be1078bd0fe95 go-flags-1/000077500000000000000000000000001224236051200127105ustar00rootroot00000000000000go-flags-1/LICENSE000066400000000000000000000027531224236051200137240ustar00rootroot00000000000000Copyright (c) 2012 Jesse van den Kieboom. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Google Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. go-flags-1/README.md000066400000000000000000000107231224236051200141720ustar00rootroot00000000000000go-flags: a go library for parsing command line arguments ========================================================= 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 The flags package uses structs, reflection and struct field tags to allow users to specify command line options. This results in very simple and consise specification of your application options. For example: 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: -------- 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 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: 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", "-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) os.Exit(1) } fmt.Printf("Verbosity: %v\n", opts.Verbose) fmt.Printf("Offset: %d\n", opts.Offset) fmt.Printf("Name: %s\n", opts.Name) 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: go-flags-1/check_crosscompile.sh000077500000000000000000000005411224236051200171060ustar00rootroot00000000000000#!/bin/bash set -e echo '# linux arm7' GOARM=7 GOARCH=arm GOOS=linux go build echo '# linux arm5' GOARM=5 GOARCH=arm GOOS=linux go build echo '# windows 386' GOARCH=386 GOOS=windows go build echo '# windows amd64' GOARCH=amd64 GOOS=windows go build echo '# darwin' GOARCH=amd64 GOOS=darwin go build echo '# freebsd' GOARCH=amd64 GOOS=freebsd go build go-flags-1/commander.go000066400000000000000000000006631224236051200152110ustar00rootroot00000000000000package flags import ( "sort" ) type Commander struct { Commands map[string]*Group } func (x *Commander) sortedNames() []string { ret := make([]string, 0, len(x.Commands)) for k, _ := range x.Commands { ret = append(ret, k) } sort.Strings(ret) return ret } func (x *Commander) EachCommand(cb func(command string, grp *Group)) { for k, v := range x.Commands { cb(k, v) // Recurse v.Commander.EachCommand(cb) } } go-flags-1/convert.go000066400000000000000000000142531224236051200147240ustar00rootroot00000000000000// Copyright 2012 Jesse van den Kieboom. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package flags import ( "fmt" "reflect" "strconv" "strings" "time" ) // Marshaler is the interface implemented by types that can marshal themselves // to a string representation of the flag. type Marshaler interface { MarshalFlag() (string, error) } // Unmarshaler is the interface implemented by types that can unmarshal a flag // argument to themselves. The provided value is directly passed from the // command line. type Unmarshaler interface { UnmarshalFlag(value string) error } func getBase(options multiTag, base int) (int, error) { sbase := options.Get("base") var err error var ivbase int64 if sbase != "" { ivbase, err = strconv.ParseInt(sbase, 10, 32) base = int(ivbase) } return base, err } func convertMarshal(val reflect.Value) (bool, string, error) { // Check first for the Marshaler interface if val.Type().NumMethod() > 0 && val.CanInterface() { if marshaler, ok := val.Interface().(Marshaler); ok { ret, err := marshaler.MarshalFlag() return true, ret, err } } return false, "", nil } func convertToString(val reflect.Value, options multiTag) (string, error) { if ok, ret, err := convertMarshal(val); ok { return ret, err } tp := val.Type() // Support for time.Duration if tp == reflect.TypeOf((*time.Duration)(nil)).Elem() { stringer := val.Interface().(fmt.Stringer) return stringer.String(), nil } switch tp.Kind() { case reflect.String: return val.String(), nil case reflect.Bool: if val.Bool() { return "true", nil } else { return "false", nil } case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: base, _ := getBase(options, 10) return strconv.FormatInt(val.Int(), base), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: base, _ := getBase(options, 10) return strconv.FormatUint(val.Uint(), base), nil case reflect.Float32, reflect.Float64: return strconv.FormatFloat(val.Float(), 'g', -1, tp.Bits()), nil case reflect.Slice: if val.Len() == 0 { return "", nil } ret := "[" for i := 0; i < val.Len(); i++ { if i != 0 { ret += ", " } item, err := convertToString(val.Index(i), options) if err != nil { return "", err } ret += item } return ret + "]", nil case reflect.Map: ret := "{" for i, key := range val.MapKeys() { if i != 0 { ret += ", " } item, err := convertToString(val.MapIndex(key), options) if err != nil { return "", err } ret += item } return ret + "}", nil case reflect.Ptr: return convertToString(reflect.Indirect(val), options) case reflect.Interface: if !val.IsNil() { return convertToString(val.Elem(), options) } } return "", nil } func convertUnmarshal(val string, retval reflect.Value) (bool, error) { if retval.Type().NumMethod() > 0 && retval.CanInterface() { if unmarshaler, ok := retval.Interface().(Unmarshaler); ok { return true, unmarshaler.UnmarshalFlag(val) } } if retval.Type().Kind() != reflect.Ptr && retval.CanAddr() { return convertUnmarshal(val, retval.Addr()) } if retval.Type().Kind() == reflect.Interface && !retval.IsNil() { return convertUnmarshal(val, retval.Elem()) } return false, nil } func convert(val string, retval reflect.Value, options multiTag) error { if ok, err := convertUnmarshal(val, retval); ok { return err } tp := retval.Type() // Support for time.Duration if tp == reflect.TypeOf((*time.Duration)(nil)).Elem() { parsed, err := time.ParseDuration(val) if err != nil { return err } retval.SetInt(int64(parsed)) return nil } switch tp.Kind() { case reflect.String: retval.SetString(val) case reflect.Bool: if val == "" { retval.SetBool(true) } else { b, err := strconv.ParseBool(val) if err != nil { return err } retval.SetBool(b) } case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: base, err := getBase(options, 10) if err != nil { return err } parsed, err := strconv.ParseInt(val, base, tp.Bits()) if err != nil { return err } retval.SetInt(parsed) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: base, err := getBase(options, 10) if err != nil { return err } parsed, err := strconv.ParseUint(val, base, tp.Bits()) if err != nil { return err } retval.SetUint(parsed) case reflect.Float32, reflect.Float64: parsed, err := strconv.ParseFloat(val, tp.Bits()) if err != nil { return err } retval.SetFloat(parsed) case reflect.Slice: elemtp := tp.Elem() elemvalptr := reflect.New(elemtp) elemval := reflect.Indirect(elemvalptr) if err := convert(val, elemval, options); err != nil { return err } retval.Set(reflect.Append(retval, elemval)) case reflect.Map: parts := strings.SplitN(val, ":", 2) key := parts[0] var value string if len(parts) == 2 { value = parts[1] } keytp := tp.Key() keyval := reflect.New(keytp) if err := convert(key, keyval, options); err != nil { return err } valuetp := tp.Elem() valueval := reflect.New(valuetp) if err := convert(value, valueval, options); err != nil { return err } if retval.IsNil() { retval.Set(reflect.MakeMap(tp)) } retval.SetMapIndex(reflect.Indirect(keyval), reflect.Indirect(valueval)) case reflect.Ptr: if retval.IsNil() { retval.Set(reflect.New(retval.Type().Elem())) } return convert(val, reflect.Indirect(retval), options) case reflect.Interface: if !retval.IsNil() { return convert(val, retval.Elem(), options) } } return nil } func wrapText(s string, l int, prefix string) string { // Basic text wrapping of s at spaces to fit in l var ret string s = strings.TrimSpace(s) for len(s) > l { // Try to split on space suffix := "" pos := strings.LastIndex(s[:l], " ") if pos < 0 { pos = l - 1 suffix = "-\n" } if len(ret) != 0 { ret += "\n" + prefix } ret += strings.TrimSpace(s[:pos]) + suffix s = strings.TrimSpace(s[pos:]) } if len(s) > 0 { if len(ret) != 0 { ret += "\n" + prefix } return ret + s } return ret } go-flags-1/error.go000066400000000000000000000020411224236051200143650ustar00rootroot00000000000000package flags // ErrorType represents the type of error. type ErrorType uint const ( // Unknown or generic error ErrUnknown ErrorType = iota // Expected an argument but got none ErrExpectedArgument // Unknown flag ErrUnknownFlag // Unknown group ErrUnknownGroup // Failed to marshal value ErrMarshal // The error contains the builtin help message ErrHelp // An argument for a boolean value was specified ErrNoArgumentForBool // A required flag was not specified ErrRequired ) // Error represents a parser error. The error returned from Parse is of this // type. The error contains both a Type and Message. type Error struct { // The type of error Type ErrorType // The error message Message string } // Get the errors error message. func (e *Error) Error() string { return e.Message } func newError(tp ErrorType, message string) *Error { return &Error{ Type: tp, Message: message, } } func wrapError(err error) error { if _, ok := err.(*Error); !ok { return newError(ErrUnknown, err.Error()) } return err } go-flags-1/example_test.go000066400000000000000000000052111224236051200157300ustar00rootroot00000000000000// Example of use of the flags package. package flags_test import ( "fmt" "github.com/jessevdk/go-flags" "os" "os/exec" "strings" ) func Example() { 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 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: 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", "-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) os.Exit(1) } fmt.Printf("Verbosity: %v\n", opts.Verbose) fmt.Printf("Offset: %d\n", opts.Offset) fmt.Printf("Name: %s\n", opts.Name) 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 } go-flags-1/examples/000077500000000000000000000000001224236051200145265ustar00rootroot00000000000000go-flags-1/examples/add.go000066400000000000000000000006411224236051200156060ustar00rootroot00000000000000package main import ( "fmt" ) type AddCommand struct { All bool `short:"a" long:"all" description:"Add all files"` } var addCommand AddCommand func (x *AddCommand) Execute(args []string) error { fmt.Printf("Adding (all=%v): %#v\n", x.All, args) return nil } func init() { parser.AddCommand("add", "Add a file", "The add command adds a file to the repository. Use -a to add all files.", &addCommand) } go-flags-1/examples/main.go000066400000000000000000000030721224236051200160030ustar00rootroot00000000000000package main import ( "errors" "fmt" "github.com/jessevdk/go-flags" "os" "strconv" "strings" ) type EditorOptions struct { Input string `short:"i" long:"input" description:"Input file" default:"-"` Output string `short:"o" long:"output" description:"Output file" default:"-"` } type Point struct { X, Y int } func (p *Point) UnmarshalFlag(value string) error { parts := strings.Split(value, ",") if len(parts) != 2 { return errors.New("Expected two numbers separated by a ,") } x, err := strconv.ParseInt(parts[0], 10, 32) if err != nil { return err } y, err := strconv.ParseInt(parts[1], 10, 32) if err != nil { return err } p.X = int(x) p.Y = int(y) return nil } func (p Point) MarshalFlag() (string, error) { return fmt.Sprintf("%d,%d", p.X, p.Y), nil } type Options struct { // Example of verbosity with level Verbose []bool `short:"v" long:"verbose" description:"Verbose output"` // Example of optional value User string `short:"u" long:"user" description:"User name" optional:"yes" optional-value:"pancake"` // Example of map with multiple default values Users map[string]string `long:"users" description:"User e-mail map" default:"system:system@example.org" default:"admin:admin@example.org"` // Example of option group Editor EditorOptions `group:"Editor Options"` // Example of custom type Marshal/Unmarshal Point Point `long:"point" description:"A x,y point" default:"1,2"` } var options Options var parser = flags.NewParser(&options, flags.Default) func main() { if _, err := parser.Parse(); err != nil { os.Exit(1) } } go-flags-1/examples/rm.go000066400000000000000000000006741224236051200155020ustar00rootroot00000000000000package main import ( "fmt" ) type RmCommand struct { Force bool `short:"f" long:"force" description:"Force removal of files"` } var rmCommand RmCommand func (x *RmCommand) Execute(args []string) error { fmt.Printf("Removing (force=%v): %#v\n", x.Force, args) return nil } func init() { parser.AddCommand("rm", "Remove a file", "The rm command removes a file to the repository. Use -f to force removal of files.", &rmCommand) } go-flags-1/flags.go000066400000000000000000000160361224236051200143410ustar00rootroot00000000000000// Copyright 2012 Jesse van den Kieboom. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // 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 // // Additional features specific to Windows: // Options with short names (/v) // Options with long names (/verbose) // Windows-style options with arguments use a colon as the delimiter // Modify generated help message with Windows-style / options // // The flags package uses structs, reflection and struct field tags // to allow users to specify command line options. This results in very simple // and consise specification of your application options. For example: // // 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]}. // // Slice options work exactly the same as primitive type options, except that // whenever the option is encountered, a value is appended to the slice. // // Map options from string to primitive type are also supported. On the command // line, you specify the value for such an option as key:value. For example // // type Options struct { // AuthorInfo string[string] `short:"a"` // } // // Then, the AuthorInfo map can be filled with something like // -a name:Jesse -a "surname:van den Kieboom". // // Finally, for full control over the conversion between command line argument // values and options, user defined types can choose to implement the Marshaler // and Unmarshaler interfaces. // // Available field tags: // short: the short name of the option (single character) // long: the long name of the option // description: the description of the option (optional) // optional: whether an argument of the option is optional (optional) // optional-value: the value of an optional option when the option occurs // without an argument. This tag can be specified multiple // times in the case of maps or slices (optional) // default: the default value of an option. This tag can be specified // multiple times in the case of slices or maps (optional). // default-mask: when specified, this value will be displayed in the help // instead of the actual default value. This is useful // mostly for hiding otherwise sensitive information from // showing up in the help. If default-mask takes the special // value "-", then no default value will be shown at all // (optional) // required: whether an option is required to appear on the command // line. If a required option is not present, the parser // will return ErrRequired. // base: a base (radix) used to convert strings to integer values, // the default base is 10 (i.e. decimal) (optional) // value-name: the name of the argument value (to be shown in the help, // (optional) // group: when specified on a struct field, makes the struct field // a separate group with the given name (optional). // command: when specified on a struct field, makes the struct field // a (sub)command with the given name (optional). // name: the display name of the command. This name is the name // shown in the builtin generated help and can be used // to provide a more informative title of a command. If not // specified this defaults to the name given in the command // command tag (optional). // // Either short: or long: must be specified to make the field eligible as an // option. // // // Option groups: // // Option groups are a simple way to semantically separate your options. The // only real difference is in how your options will appear in the builtin // generated help. All options in a particular group are shown together in the // help under the name of the group. // // There are currently three ways to specify option groups. // // 1. Use NewNamedParser specifying the various option groups. // 2. Use AddGroup to add a group to an existing parser. // 3. Add a struct field to the toplevel options annotated with the // group:"group-name" tag. // // // // Commands: // // The flags package also has basic support for commands. Commands are often // used in monolithic applications that support various commands or actions. // Take git for example, all of the add, commit, checkout, etc. are called // commands. Using commands you can easily separate multiple functions of your // application. // // There are currently two ways to specifiy a command. // // 1. Use AddCommand on an existing parser. // 2. Add a struct field to your options struct annotated with the // command:"command-name" tag. // // The most common, idiomatic way to implement commands is to define a global // parser instance and implement each command in a separate file. These // command files should define a go init function which calls AddCommand on // the global parser. // // When parsing ends and there is an active command and that command implements // the Command interface, then its Execute method will be run providing the // remaining arguments. // // Command structs can have options which become valid to parse after the // command has been specified on the command line. It is currently not valid // to specify options from the parent level of the command after the command // name has occurred. Thus, given a toplevel option "-v" and a command "add": // // Valid: ./app -v add // Invalid: ./app add -v // package flags go-flags-1/group.go000066400000000000000000000045351224236051200144020ustar00rootroot00000000000000// Copyright 2012 Jesse van den Kieboom. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package flags import ( "errors" ) // The provided container is not a pointer to a struct var ErrNotPointerToStruct = errors.New("provided data is not a pointer to struct") // The provided short name is longer than a single character var ErrShortNameTooLong = errors.New("short names can only be 1 character") // An option group. The option group has a name and a set of options. type Group struct { Commander // The name of the group. Name string Names map[string]*Option IniNames map[string]*Option // A map of long names to option option descriptions. LongNames map[string]*Option // A map of short names to option option descriptions. ShortNames map[rune]*Option // A list of all the options in the group. Options []*Option // An error which occurred when creating the group. Error error // Groups embedded in this group EmbeddedGroups []*Group IsCommand bool LongDescription string data interface{} } // The command interface should be implemented by any command added in the // options. When implemented, the Execute method will be called for the last // specified (sub)command providing the remaining command line arguments. type Command interface { // Execute will be called for the last active (sub)command. The // args argument contains the remaining command line arguments. The // error that Execute returns will be eventually passed out of the // Parse method of the Parser. Execute(args []string) error } type Usage interface { Usage() string } // NewGroup creates a new option group with a given name and underlying data // container. The data container is a pointer to a struct. The fields of the // struct represent the command line options (using field tags) and their values // will be set when their corresponding options appear in the command line // arguments. func NewGroup(name string, data interface{}) *Group { ret := &Group{ Commander: Commander{ Commands: make(map[string]*Group), }, Name: name, Names: make(map[string]*Option), IniNames: make(map[string]*Option), LongNames: make(map[string]*Option), ShortNames: make(map[rune]*Option), IsCommand: false, data: data, } ret.Error = ret.scan() return ret } go-flags-1/group_private.go000066400000000000000000000113541224236051200161310ustar00rootroot00000000000000package flags import ( "reflect" "sort" "strings" "unicode/utf8" "unsafe" ) func (g *Group) lookupByName(name string, ini bool) (*Option, string) { name = strings.ToLower(name) if ini { if ret := g.IniNames[name]; ret != nil { return ret, ret.tag.Get("ini-name") } if ret := g.Names[name]; ret != nil { return ret, ret.Field.Name } } if ret := g.LongNames[name]; ret != nil { return ret, ret.LongName } if utf8.RuneCountInString(name) == 1 { r, _ := utf8.DecodeRuneInString(name) if ret := g.ShortNames[r]; ret != nil { data := make([]byte, utf8.RuneLen(ret.ShortName)) utf8.EncodeRune(data, ret.ShortName) return ret, string(data) } } return nil, "" } func (g *Group) storeDefaults() { for _, option := range g.Options { // First. empty out the value if len(option.Default) > 0 { option.clear() } for _, d := range option.Default { option.Set(&d) } if !option.Value.CanSet() { continue } option.defaultValue = reflect.ValueOf(option.Value.Interface()) } } func (g *Group) scanStruct(realval reflect.Value, sfield *reflect.StructField) error { stype := realval.Type() if sfield != nil { mtag := newMultiTag(string(sfield.Tag)) groupName := mtag.Get("group") commandName := mtag.Get("command") name := mtag.Get("name") description := mtag.Get("description") iscommand := false if len(commandName) != 0 { iscommand = true if len(name) != 0 { groupName = name } else if len(commandName) != 0 { groupName = commandName } } if len(groupName) != 0 { ptrval := reflect.NewAt(realval.Type(), unsafe.Pointer(realval.UnsafeAddr())) newGroup := NewGroup(groupName, ptrval.Interface()) if iscommand { newGroup.IsCommand = true g.Commands[commandName] = newGroup } newGroup.LongDescription = description g.EmbeddedGroups = append(g.EmbeddedGroups, newGroup) return g.Error } } for i := 0; i < stype.NumField(); i++ { field := stype.Field(i) // PkgName is set only for non-exported fields, which we ignore if field.PkgPath != "" { continue } mtag := newMultiTag(string(field.Tag)) // Skip fields with the no-flag tag if mtag.Get("no-flag") != "" { continue } // Dive deep into structs or pointers to structs kind := field.Type.Kind() if kind == reflect.Struct { err := g.scanStruct(realval.Field(i), &field) if err != nil { return err } } else if kind == reflect.Ptr && field.Type.Elem().Kind() == reflect.Struct && !realval.Field(i).IsNil() { err := g.scanStruct(reflect.Indirect(realval.Field(i)), &field) if err != nil { return err } } longname := mtag.Get("long") shortname := mtag.Get("short") if longname == "" && shortname == "" { continue } short := rune(0) rc := utf8.RuneCountInString(shortname) if rc > 1 { return ErrShortNameTooLong } else if rc == 1 { short, _ = utf8.DecodeRuneInString(shortname) } description := mtag.Get("description") def := mtag.GetMany("default") optionalValue := mtag.GetMany("optional-value") valueName := mtag.Get("value-name") defaultMask := mtag.Get("default-mask") optional := (mtag.Get("optional") != "") required := (mtag.Get("required") != "") option := &Option{ Description: description, ShortName: short, LongName: longname, Default: def, OptionalArgument: optional, OptionalValue: optionalValue, Required: required, Field: field, Value: realval.Field(i), ValueName: valueName, tag: mtag, defaultMask: defaultMask, } g.Options = append(g.Options, option) if option.ShortName != 0 { g.ShortNames[option.ShortName] = option } if option.LongName != "" { g.LongNames[strings.ToLower(option.LongName)] = option } g.Names[strings.ToLower(field.Name)] = option ininame := mtag.Get("ini-name") if len(ininame) != 0 { g.IniNames[strings.ToLower(ininame)] = option } } return nil } func (g *Group) each(index int, cb func(int, *Group)) int { cb(index, g) index++ for _, group := range g.EmbeddedGroups { group.each(index, cb) index++ } return index } func (g *Group) eachCommand(cb func(string, *Group)) { cmds := make([]string, len(g.Commands)) i := 0 for k, _ := range g.Commands { cmds[i] = k } sort.Strings(cmds) for _, k := range cmds { cb(k, g.Commands[k]) } } func (g *Group) scan() error { // Get all the public fields in the data struct ptrval := reflect.ValueOf(g.data) if ptrval.Type().Kind() != reflect.Ptr { panic(ErrNotPointerToStruct) } stype := ptrval.Type().Elem() if stype.Kind() != reflect.Struct { panic(ErrNotPointerToStruct) } realval := reflect.Indirect(ptrval) return g.scanStruct(realval, nil) } go-flags-1/help.go000066400000000000000000000130711224236051200141710ustar00rootroot00000000000000// Copyright 2012 Jesse van den Kieboom. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package flags import ( "bufio" "bytes" "fmt" "io" "reflect" "strings" "unicode/utf8" ) type alignmentInfo struct { maxLongLen int hasShort bool hasValueName bool terminalColumns int } func (p *Parser) getAlignmentInfo() alignmentInfo { ret := alignmentInfo{ maxLongLen: 0, hasShort: false, hasValueName: false, terminalColumns: getTerminalColumns(), } if ret.terminalColumns <= 0 { ret.terminalColumns = 80 } alfunc := func(index int, grp *Group) { for _, info := range grp.Options { if info.ShortName != 0 { ret.hasShort = true } lv := utf8.RuneCountInString(info.ValueName) if lv != 0 { ret.hasValueName = true } l := utf8.RuneCountInString(info.LongName) + lv if l > ret.maxLongLen { ret.maxLongLen = l } } } if p.currentCommand != nil { // Make sure to also check for toplevel arguments for the // alignment since they are included in the help output also p.eachTopLevelGroup(alfunc) } p.EachGroup(alfunc) return ret } func (p *Parser) writeHelpOption(writer *bufio.Writer, option *Option, info alignmentInfo) { line := &bytes.Buffer{} distanceBetweenOptionAndDescription := 2 paddingBeforeOption := 2 line.WriteString(strings.Repeat(" ", paddingBeforeOption)) if option.ShortName != 0 { line.WriteRune(defaultShortOptDelimiter) line.WriteRune(option.ShortName) } else if info.hasShort { line.WriteString(" ") } descstart := info.maxLongLen + paddingBeforeOption + distanceBetweenOptionAndDescription if info.hasShort { descstart += 2 } if info.maxLongLen > 0 { descstart += 4 } if info.hasValueName { descstart += 3 } if len(option.LongName) > 0 { if option.ShortName != 0 { line.WriteString(", ") } else if info.hasShort { line.WriteString(" ") } line.WriteString(defaultLongOptDelimiter) line.WriteString(option.LongName) } if !option.isBool() { line.WriteRune(defaultNameArgDelimiter) if len(option.ValueName) > 0 { line.WriteString(option.ValueName) } } written := line.Len() line.WriteTo(writer) if option.Description != "" { dw := descstart - written writer.WriteString(strings.Repeat(" ", dw)) def := "" defs := option.Default if len(option.defaultMask) != 0 { if option.defaultMask != "-" { def = option.defaultMask } } else if len(defs) == 0 && !option.isBool() { var showdef bool switch option.Field.Type.Kind() { case reflect.Func, reflect.Ptr: showdef = !option.Value.IsNil() case reflect.Slice, reflect.String, reflect.Array: showdef = option.Value.Len() > 0 case reflect.Map: showdef = !option.Value.IsNil() && option.Value.Len() > 0 default: zeroval := reflect.Zero(option.Field.Type) showdef = !reflect.DeepEqual(zeroval.Interface(), option.Value.Interface()) } if showdef { def, _ = convertToString(option.Value, option.tag) } } else if len(defs) != 0 { def = strings.Join(defs, ", ") } var desc string if def != "" { desc = fmt.Sprintf("%s (%v)", option.Description, def) } else { desc = option.Description } writer.WriteString(wrapText(desc, info.terminalColumns-descstart, strings.Repeat(" ", descstart))) } writer.WriteString("\n") } // WriteHelp writes a help message containing all the possible options and // their descriptions to the provided writer. Note that the HelpFlag parser // option provides a convenient way to add a -h/--help option group to the // command line parser which will automatically show the help messages using // this method. func (p *Parser) WriteHelp(writer io.Writer) { if writer == nil { return } wr := bufio.NewWriter(writer) aligninfo := p.getAlignmentInfo() if p.ApplicationName != "" { wr.WriteString("Usage:\n") fmt.Fprintf(wr, " %s", p.ApplicationName) if p.Usage != "" { fmt.Fprintf(wr, " %s", p.Usage) } if len(p.currentCommandString) > 0 { cmdusage := fmt.Sprintf("[%s-OPTIONS]", p.currentCommandString[len(p.currentCommandString)-1]) if p.currentCommand != nil { if us, ok := p.currentCommand.data.(Usage); ok { cmdusage = us.Usage() } } fmt.Fprintf(wr, " %s %s", strings.Join(p.currentCommandString, " "), cmdusage) } fmt.Fprintln(wr) if p.currentCommand != nil && len(p.currentCommand.LongDescription) != 0 { fmt.Fprintln(wr) t := wrapText(p.currentCommand.LongDescription, aligninfo.terminalColumns, "") fmt.Fprintln(wr, t) } } seen := make(map[*Group]bool) writeHelp := func(index int, grp *Group) { if len(grp.Options) == 0 || seen[grp] { return } seen[grp] = true wr.WriteString("\n") fmt.Fprintf(wr, "%s:\n", grp.Name) for _, info := range grp.Options { p.writeHelpOption(wr, info, aligninfo) } } // If there is a command, still write all the toplevel help too if p.currentCommand != nil { p.eachTopLevelGroup(writeHelp) } p.EachGroup(writeHelp) commander := p.currentCommander() names := commander.sortedNames() if len(names) > 0 { maxnamelen := len(names[0]) for i := 1; i < len(names); i++ { l := len(names[i]) if l > maxnamelen { maxnamelen = l } } fmt.Fprintln(wr) fmt.Fprintln(wr, "Available commands:") for _, name := range names { fmt.Fprintf(wr, " %s", name) cmd := commander.Commands[name] if len(cmd.Name) > 0 { pad := strings.Repeat(" ", maxnamelen-len(name)) fmt.Fprintf(wr, "%s %s", pad, cmd.Name) } fmt.Fprintln(wr) } } wr.Flush() } go-flags-1/ini.go000066400000000000000000000071571224236051200140300ustar00rootroot00000000000000package flags import ( "bufio" "fmt" "io" "os" "reflect" "strings" ) type IniValue struct { Name string Value string } type IniSection []IniValue type Ini map[string]IniSection func readFullLine(reader *bufio.Reader) (string, error) { var line []byte for { l, more, err := reader.ReadLine() if err != nil { return "", err } if line == nil && !more { return string(l), nil } line = append(line, l...) if !more { break } } return string(line), nil } type IniError struct { Message string File string LineNumber uint } func (x *IniError) Error() string { return fmt.Sprintf("%s:%d: %s", x.File, x.LineNumber, x.Message) } type IniOptions uint const ( IniNone IniOptions = 0 IniIncludeDefaults = 1 << iota IniIncludeComments IniDefault = IniIncludeComments ) func writeIni(parser *Parser, writer io.Writer, options IniOptions) { parser.EachGroup(func(i int, group *Group) { if i != 0 { io.WriteString(writer, "\n") } fmt.Fprintf(writer, "[%s]\n", group.Name) for _, option := range group.Options { if option.isFunc() { continue } if len(option.tag.Get("no-ini")) != 0 { continue } val := option.Value if (options&IniIncludeDefaults) == IniNone && reflect.DeepEqual(val, option.defaultValue) { continue } if (options & IniIncludeComments) != IniNone { fmt.Fprintf(writer, "; %s\n", option.Description) } switch val.Type().Kind() { case reflect.Slice: for idx := 0; idx < val.Len(); idx++ { v, _ := convertToString(val.Index(idx), option.tag) fmt.Fprintf(writer, "%s = %s\n", option.iniName(), v) } case reflect.Map: for _, key := range val.MapKeys() { k, _ := convertToString(key, option.tag) v, _ := convertToString(val.MapIndex(key), option.tag) fmt.Fprintf(writer, "%s = %s:%s\n", option.iniName(), k, v) } default: v, _ := convertToString(val, option.tag) fmt.Fprintf(writer, "%s = %s\n", option.iniName(), v) } } }) } func readIniFromFile(filename string) (Ini, error) { file, err := os.Open(filename) if err != nil { return nil, err } defer file.Close() return readIni(file, filename) } func readIni(contents io.Reader, filename string) (Ini, error) { ret := make(Ini) reader := bufio.NewReader(contents) var section IniSection var sectionname string var lineno uint for { line, err := readFullLine(reader) if err == io.EOF { break } if err != nil { return nil, err } lineno++ line = strings.TrimSpace(line) // Skip empty lines and lines starting with ; (comments) if len(line) == 0 || line[0] == ';' { continue } if section == nil { if line[0] != '[' || line[len(line)-1] != ']' { return nil, &IniError{ Message: "malformed section header", File: filename, LineNumber: lineno, } } name := strings.TrimSpace(line[1 : len(line)-1]) if len(name) == 0 { return nil, &IniError{ Message: "empty section name", File: filename, LineNumber: lineno, } } sectionname = name section = ret[name] if section == nil { section = make(IniSection, 0, 10) ret[name] = section } continue } // Parse option here keyval := strings.SplitN(line, "=", 2) if len(keyval) != 2 { return nil, &IniError{ Message: "malformed key=value", File: filename, LineNumber: lineno, } } name := strings.TrimSpace(keyval[0]) value := strings.TrimSpace(keyval[1]) section = append(section, IniValue{ Name: name, Value: value, }) ret[sectionname] = section } return ret, nil } go-flags-1/man.go000066400000000000000000000045131224236051200140150ustar00rootroot00000000000000package flags import ( "fmt" "io" "strings" "time" ) func (x *Parser) formatForMan(wr io.Writer, s string) { for { idx := strings.IndexRune(s, '`') if idx < 0 { fmt.Fprintf(wr, "%s", s) break } fmt.Fprintf(wr, "%s", s[:idx]) s = s[idx+1:] idx = strings.IndexRune(s, '\'') if idx < 0 { fmt.Fprintf(wr, "%s", s) break } fmt.Fprintf(wr, "\\fB%s\\fP", s[:idx]) s = s[idx+1:] } } func (x *Parser) writeManPageOptions(wr io.Writer, groups ...*Group) { for _, group := range groups { for _, opt := range group.Options { fmt.Fprintln(wr, ".TP") fmt.Fprintf(wr, "\\fB") if opt.ShortName != 0 { fmt.Fprintf(wr, "-%c", opt.ShortName) } if len(opt.LongName) != 0 { if opt.ShortName != 0 { fmt.Fprintf(wr, ", ") } fmt.Fprintf(wr, "--%s", opt.LongName) } fmt.Fprintln(wr, "\\fP") x.formatForMan(wr, opt.Description) fmt.Fprintln(wr, "") } } } func (x *Parser) writeManPageCommand(wr io.Writer, command string, grp *Group) { fmt.Fprintf(wr, ".SS %s\n", command) fmt.Fprintln(wr, grp.Name) if len(grp.LongDescription) > 0 { fmt.Fprintln(wr, "") cmdstart := fmt.Sprintf("The %s command", command) if strings.HasPrefix(grp.LongDescription, cmdstart) { fmt.Fprintf(wr, "The \\fI%s\\fP command", command) x.formatForMan(wr, grp.LongDescription[len(cmdstart):]) fmt.Fprintln(wr, "") } else { x.formatForMan(wr, grp.LongDescription) fmt.Fprintln(wr, "") } } x.writeManPageOptions(wr, grp) for k, v := range grp.Commander.Commands { x.writeManPageCommand(wr, command+" "+k, v) } } // WriteManPage writes a basic man page in groff format to the specified // writer. func (x *Parser) WriteManPage(wr io.Writer, description string) { t := time.Now() fmt.Fprintf(wr, ".TH %s 1 \"%s\"\n", x.ApplicationName, t.Format("2 January 2006")) fmt.Fprintln(wr, ".SH NAME") fmt.Fprintf(wr, "%s \\- %s\n", x.ApplicationName, x.Description) fmt.Fprintln(wr, ".SH SYNOPSIS") fmt.Fprintf(wr, "\\fB%s\\fP %s\n", x.ApplicationName, x.Usage) fmt.Fprintln(wr, ".SH DESCRIPTION") x.formatForMan(wr, description) fmt.Fprintln(wr, "") fmt.Fprintln(wr, ".SH OPTIONS") x.writeManPageOptions(wr, x.Groups...) if len(x.Commander.Commands) > 0 { fmt.Fprintln(wr, ".SH COMMANDS") for k, v := range x.Commander.Commands { x.writeManPageCommand(wr, k, v) } } } go-flags-1/multitag.go000066400000000000000000000023701224236051200150670ustar00rootroot00000000000000package flags import ( "strconv" ) type multiTag struct { value string cache map[string][]string } func newMultiTag(v string) multiTag { return multiTag{ value: v, } } func (x *multiTag) scan() map[string][]string { v := x.value ret := make(map[string][]string) // This is mostly copied from reflect.StructTAg.Get for v != "" { i := 0 // Skip whitespace for i < len(v) && v[i] == ' ' { i++ } v = v[i:] if v == "" { break } // Scan to colon to find key i = 0 for i < len(v) && v[i] != ' ' && v[i] != ':' && v[i] != '"' { i++ } if i+1 >= len(v) || v[i] != ':' || v[i+1] != '"' { break } name := v[:i] v = v[i+1:] // Scan quoted string to find value i = 1 for i < len(v) && v[i] != '"' { if v[i] == '\\' { i++ } i++ } if i >= len(v) { break } val, _ := strconv.Unquote(v[:i+1]) v = v[i+1:] ret[name] = append(ret[name], val) } return ret } func (x *multiTag) cached() map[string][]string { if x.cache == nil { x.cache = x.scan() } return x.cache } func (x *multiTag) Get(key string) string { c := x.cached() if v, ok := c[key]; ok { return v[len(v)-1] } return "" } func (x *multiTag) GetMany(key string) []string { c := x.cached() return c[key] } go-flags-1/option.go000066400000000000000000000055731224236051200145610ustar00rootroot00000000000000package flags import ( "fmt" "reflect" "unicode/utf8" ) // Option flag information. Contains a description of the option, short and // long name as well as a default value and whether an argument for this // flag is optional. type Option struct { // The short name of the option (a single character). If not 0, the // option flag can be 'activated' using -. Either ShortName // or LongName needs to be non-empty. ShortName rune // The long name of the option. If not "", the option flag can be // activated using --. Either ShortName or LongName needs // to be non-empty. LongName string // The description of the option flag. This description is shown // automatically in the builtin help. Description string // The default value of the option. Default []string // If true, specifies that the argument to an option flag is optional. // When no argument to the flag is specified on the command line, the // value of Default will be set in the field this option represents. // This is only valid for non-boolean options. OptionalArgument bool // The optional value of the option. The optional value is used when // the option flag is marked as having an OptionalArgument. This means // that when the flag is specified, but no option argument is given, // the value of the field this option represents will be set to // OptionalValue. This is only valid for non-boolean options. OptionalValue []string // If true, the option _must_ be specified on the command line. If the // option is not specified, the parser will generate an ErrRequired type // error. Required bool // A name for the value of an option shown in the Help as --flag [ValueName] ValueName string // The struct field which the option represents. Field reflect.StructField // The struct field value which the option represents. Value reflect.Value defaultValue reflect.Value defaultMask string iniUsedName string tag multiTag } // Set the value of an option to the specified value. An error will be returned // if the specified value could not be converted to the corresponding option // value type. func (option *Option) Set(value *string) error { if option.isFunc() { return option.call(value) } else if value != nil { return convert(*value, option.Value, option.tag) } else { return convert("", option.Value, option.tag) } return nil } // Convert an option to a human friendly readable string describing the option. func (option *Option) String() string { var s string var short string if option.ShortName != 0 { data := make([]byte, utf8.RuneLen(option.ShortName)) utf8.EncodeRune(data, option.ShortName) short = string(data) if len(option.LongName) != 0 { s = fmt.Sprintf("-%s, --%s", short, option.LongName) } else { s = fmt.Sprintf("-%s", short) } } else if len(option.LongName) != 0 { s = fmt.Sprintf("--%s", option.LongName) } return s } go-flags-1/option_private.go000066400000000000000000000027201224236051200163020ustar00rootroot00000000000000package flags import ( "reflect" ) func (option *Option) canArgument() bool { return !option.isBool() } func (option *Option) clear() { tp := option.Value.Type() switch tp.Kind() { case reflect.Func: // Skip case reflect.Map: // Empty the map option.Value.Set(reflect.MakeMap(tp)) default: zeroval := reflect.Zero(tp) option.Value.Set(zeroval) } } func (option *Option) isBool() bool { tp := option.Value.Type() switch tp.Kind() { case reflect.Bool: return true case reflect.Slice: return (tp.Elem().Kind() == reflect.Bool) case reflect.Func: return tp.NumIn() == 0 } return false } func (option *Option) isFunc() bool { return option.Value.Type().Kind() == reflect.Func } func (option *Option) iniName() string { if len(option.iniUsedName) != 0 { return option.iniUsedName } name := option.tag.Get("ini-name") if len(name) != 0 { return name } return option.Field.Name } func (option *Option) call(value *string) error { var retval []reflect.Value if value == nil { retval = option.Value.Call(nil) } else { tp := option.Value.Type().In(0) val := reflect.New(tp) val = reflect.Indirect(val) if err := convert(*value, val, option.tag); err != nil { return err } retval = option.Value.Call([]reflect.Value{val}) } if len(retval) == 1 && retval[0].Type() == reflect.TypeOf((*error)(nil)).Elem() { if retval[0].Interface() == nil { return nil } return retval[0].Interface().(error) } return nil } go-flags-1/optstyle_other.go000066400000000000000000000023061224236051200163240ustar00rootroot00000000000000// +build !windows package flags import ( "strings" ) const ( defaultShortOptDelimiter = '-' defaultLongOptDelimiter = "--" defaultNameArgDelimiter = '=' ) func argumentIsOption(arg string) bool { return len(arg) > 0 && arg[0] == '-' } // stripOptionPrefix returns the option without the prefix and whether or // not the option is a long option or not. func stripOptionPrefix(optname string) (string, bool) { if strings.HasPrefix(optname, "--") { return optname[2:], true } else if strings.HasPrefix(optname, "-") { return optname[1:], false } return optname, false } // splitOption attempts to split the passed option into a name and an argument. // When there is no argument specified, nil will be returned for it. func splitOption(option string) (string, *string) { pos := strings.Index(option, "=") if pos >= 0 { rest := option[pos+1:] return option[:pos], &rest } return option, nil } // newHelpGroup returns a new group that contains default help parameters. func newHelpGroup(showHelp func() error) *Group { var help struct { ShowHelp func() error `short:"h" long:"help" description:"Show this help message"` } help.ShowHelp = showHelp return NewGroup("Help Options", &help) } go-flags-1/optstyle_windows.go000066400000000000000000000045661224236051200167070ustar00rootroot00000000000000package flags import ( "strings" ) // Windows uses a front slash for both short and long options. Also it uses // a colon for name/argument delimter. const ( defaultShortOptDelimiter = '/' defaultLongOptDelimiter = "/" defaultNameArgDelimiter = ':' ) func argumentIsOption(arg string) bool { // Windows-style options allow front slash for the option // delimiter. return len(arg) > 0 && (arg[0] == '-' || arg[0] == '/') } // stripOptionPrefix returns the option without the prefix and whether or // not the option is a long option or not. func stripOptionPrefix(optname string) (string, bool) { // Determine if the argument is a long option or not. Windows // typically supports both long and short options with a single // front slash as the option delimiter, so handle this situation // nicely. if strings.HasPrefix(optname, "--") { return optname[2:], true } else if strings.HasPrefix(optname, "-") { return optname[1:], false } else if strings.HasPrefix(optname, "/") { optname = optname[1:] if len(optname) > 1 { return optname, true } return optname, false } return optname, false } // splitOption attempts to split the passed option into a name and an argument. // When there is no argument specified, nil will be returned for it. func splitOption(option string) (string, *string) { if len(option) == 0 { return option, nil } // Windows typically uses a colon for the option name and argument // delimiter while POSIX typically uses an equals. Support both styles, // but don't allow the two to be mixed. That is to say /foo:bar and // --foo=bar are acceptable, but /foo=bar and --foo:bar are not. var pos int if option[0] == '/' { pos = strings.Index(option, ":") } else if option[0] == '-' { pos = strings.Index(option, "=") } if pos >= 0 { rest := option[pos+1:] return option[:pos], &rest } return option, nil } // newHelpGroup returns a new group that contains default help parameters. func newHelpGroup(showHelp func() error) *Group { // Windows CLI applications typically use /? for help, so make both // that available as well as the POSIX style h and help. var help struct { ShowHelp func() error `short:"?" description:"Show this help message"` ShowHelp2 func() error `short:"h" long:"help" description:"Show this help message"` } help.ShowHelp = showHelp help.ShowHelp2 = showHelp return NewGroup("Help Options", &help) } go-flags-1/parser.go000066400000000000000000000336721224236051200145460ustar00rootroot00000000000000// Copyright 2012 Jesse van den Kieboom. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package flags import ( "bytes" "fmt" "io" "os" "path" "sort" "strings" "unicode/utf8" ) // A Parser provides command line option parsing. It can contain several // option groups each with their own set of options. type Parser struct { Commander // The option groups available to the parser Groups []*Group GroupsMap map[string]*Group // The parser application name ApplicationName string // The application short description Description string // The usage (e.g. [OPTIONS] ) Usage string Options Options currentCommand *Group currentCommandString []string } // Parser options type Options uint const ( // No options None Options = 0 // Add a default Help Options group to the parser containing -h and // --help options. When either -h or --help is specified on the command // line, a pretty formatted help message will be printed to os.Stderr. // The parser will return ErrHelp. HelpFlag = 1 << iota // Pass all arguments after a double dash, --, as remaining command line // arguments (i.e. they will not be parsed for flags) PassDoubleDash // Ignore any unknown options and pass them as remaining command line // arguments IgnoreUnknown // Print any errors which occured during parsing to os.Stderr PrintErrors // Pass all arguments after the first non option. This is equivalent // to strict POSIX processing PassAfterNonOption // A convenient default set of options Default = HelpFlag | PrintErrors | PassDoubleDash ) // Parse is a convenience function to parse command line options with default // settings. The provided data is a pointer to a struct representing the // default option group (named "Application Options"). For more control, use // flags.NewParser. func Parse(data interface{}) ([]string, error) { return NewParser(data, Default).Parse() } // ParseArgs is a convenience function to parse command line options with default // settings. The provided data is a pointer to a struct representing the // default option group (named "Application Options"). The args argument is // the list of command line arguments to parse. If you just want to parse the // default program command line arguments (i.e. os.Args), then use flags.Parse // instead. For more control, use flags.NewParser. func ParseArgs(data interface{}, args []string) ([]string, error) { return NewParser(data, Default).ParseArgs(args) } // ParseIni is a convenience function to parse command line options with default // settings from an ini file. The provided data is a pointer to a struct // representing the default option group (named "Application Options"). For // more control, use flags.NewParser. func ParseIni(filename string, data interface{}) error { return NewParser(data, Default).ParseIniFile(filename) } // NewParser creates a new parser. It uses os.Args[0] as the application // name and then calls Parser.NewNamedParser (see Parser.NewNamedParser for // more details). The provided data is a pointer to a struct representing the // default option group (named "Application Options"), or nil if the default // group should not be added. The options parameter specifies a set of options // for the parser. func NewParser(data interface{}, options Options) *Parser { if data == nil { return NewNamedParser(path.Base(os.Args[0]), options) } return NewNamedParser(path.Base(os.Args[0]), options, NewGroup("Application Options", data)) } // NewNamedParser creates a new parser. The appname is used to display the // executable name in the builtin help message. An initial set of option groups // can be specified when constructing a parser, but you can also add additional // option groups later (see Parser.AddGroup). func NewNamedParser(appname string, options Options, groups ...*Group) *Parser { ret := &Parser{ Commander: Commander{ Commands: make(map[string]*Group), }, ApplicationName: appname, Groups: groups, GroupsMap: make(map[string]*Group), Options: options, Usage: "[OPTIONS]", } ret.EachGroup(func(index int, grp *Group) { ret.GroupsMap[strings.ToLower(grp.Name)] = grp }) return ret } // AddGroup adds a new group to the parser with the given name and data. The // data needs to be a pointer to a struct from which the fields indicate which // options are in the group. func (p *Parser) AddGroup(name string, data interface{}) *Parser { group := NewGroup(name, data) p.Groups = append(p.Groups, group) group.each(0, func(index int, grp *Group) { p.GroupsMap[strings.ToLower(group.Name)] = grp }) return p } // AddCommand adds a new command to the parser with the given name and data. The // data needs to be a pointer to a struct from which the fields indicate which // options are in the command. func (p *Parser) AddCommand(command string, description string, longDescription string, data interface{}) *Parser { p.AddGroup(description, data) group := p.Groups[len(p.Groups)-1] group.IsCommand = true group.LongDescription = longDescription p.Commands[command] = group return p } // Parse parses the command line arguments from os.Args using Parser.ParseArgs. // For more detailed information see ParseArgs. func (p *Parser) Parse() ([]string, error) { return p.ParseArgs(os.Args[1:]) } // ParseIniFile parses flags from an ini formatted file. See ParseIni for more // information on the ini file foramt. The returned errors can be of the type // flags.Error or flags.IniError. func (p *Parser) ParseIniFile(filename string) error { p.storeDefaults() ini, err := readIniFromFile(filename) if err != nil { return err } return p.parseIni(ini) } func (p *Parser) EachGroup(cb func(int, *Group)) { if p.currentCommand != nil { p.currentCommand.each(0, cb) } else { p.eachTopLevelGroup(cb) } } // ParseIni parses flags from an ini format. You can use ParseIniFile as a // convenience function to parse from a filename instead of a general // io.Reader. // // The format of the ini file is as follows: // // [Option group name] // option = value // // Each section in the ini file represents an option group in the flags parser. // The default flags parser option group (i.e. when using flags.Parse) is // named 'Application Options'. The ini option name is matched in the following // order: // // 1. Compared to the ini-name tag on the option struct field (if present) // 2. Compared to the struct field name // 3. Compared to the option long name (if present) // 4. Compared to the option short name (if present) // // The returned errors can be of the type flags.Error or // flags.IniError. func (p *Parser) ParseIni(reader io.Reader) error { p.storeDefaults() ini, err := readIni(reader, "") if err != nil { return err } return p.parseIni(ini) } // WriteIniToFile writes the flags as ini format into a file. See WriteIni // for more information. The returned error occurs when the specified file // could not be opened for writing. func (p *Parser) WriteIniToFile(filename string, options IniOptions) error { file, err := os.Create(filename) if err != nil { return err } defer file.Close() p.WriteIni(file, options) return nil } // WriteIni writes the current values of all the flags to an ini format. // See ParseIni for more information on the ini file format. You typically // call this only after settings have been parsed since the default values of each // option are stored just before parsing the flags (this is only relevant when // IniIncludeDefaults is _not_ set in options). func (p *Parser) WriteIni(writer io.Writer, options IniOptions) { writeIni(p, writer, options) } func (p *Parser) levenshtein(s string, t string) int { if len(s) == 0 { return len(t) } if len(t) == 0 { return len(s) } var l1, l2, l3 int if len(s) == 1 { l1 = len(t) + 1 } else { l1 = p.levenshtein(s[1:len(s)-1], t) + 1 } if len(t) == 1 { l2 = len(s) + 1 } else { l2 = p.levenshtein(t[1:len(t)-1], s) + 1 } l3 = p.levenshtein(s[1:len(s)], t[1:len(t)]) if s[0] != t[0] { l3 += 1 } if l2 < l1 { l1 = l2 } if l1 < l3 { return l1 } return l3 } func (p *Parser) closest(cmd string, commands []string) (string, int) { if len(commands) == 0 { return "", 0 } mincmd := -1 mindist := -1 for i, c := range commands { l := p.levenshtein(cmd, c) if mincmd < 0 || l < mindist { mindist = l mincmd = i } } return commands[mincmd], mindist } // ParseArgs parses the command line arguments according to the option groups that // were added to the parser. On successful parsing of the arguments, the // remaining, non-option, arguments (if any) are returned. The returned error // indicates a parsing error and can be used with PrintError to display // contextual information on where the error occurred exactly. // // When the common help group has been added (AddHelp) and either -h or --help // was specified in the command line arguments, a help message will be // automatically printed. Furthermore, the special error type ErrHelp is returned. // It is up to the caller to exit the program if so desired. func (p *Parser) ParseArgs(args []string) ([]string, error) { ret := make([]string, 0, len(args)) i := 0 p.storeDefaults() if (p.Options & HelpFlag) != None { var showHelp = func() error { var b bytes.Buffer p.WriteHelp(&b) return newError(ErrHelp, b.String()) } // Windows CLI applications typically use /? for help while // POSIX typically uses -h and --help. Get a help group // appropriate to the OS. helpgrp := newHelpGroup(showHelp) // Append the help group to toplevel p.Groups = append([]*Group{helpgrp}, p.Groups...) // Also append the help group to every command p.Commander.EachCommand(func(command string, grp *Group) { grp.EmbeddedGroups = append([]*Group{helpgrp}, grp.EmbeddedGroups...) }) p.Options &^= HelpFlag } required := make(map[*Option]struct{}) commands := make(map[string]*Group) for command, subgroup := range p.Commands { commands[command] = subgroup } // Mark required arguments in a map for _, group := range p.Groups { for _, option := range group.Options { if option.Required { required[option] = struct{}{} } } // Initial set of commands for command, subgroup := range group.Commands { commands[command] = subgroup } } for i < len(args) { arg := args[i] i++ // When PassDoubleDash is set and we encounter a --, then // simply append all the rest as arguments and break out if (p.Options&PassDoubleDash) != None && arg == "--" { ret = append(ret, args[i:]...) break } // If the argument is not an option then // 1) Check for subcommand // 2) Append it to the rest if subcommand is not found if !argumentIsOption(arg) { if cmdgroup := commands[arg]; cmdgroup != nil { // Set current 'root' group p.currentCommand = cmdgroup p.currentCommandString = append(p.currentCommandString, arg) commands = cmdgroup.Commands } else { if (p.Options & PassAfterNonOption) != None { ret = append(ret, args[(i-1):]...) break } else { ret = append(ret, arg) } } continue } var err error var option *Option optname, argument := splitOption(arg) optname, islong := stripOptionPrefix(optname) if islong { err, i, option = p.parseLong(args, optname, argument, i) } else { for j, c := range optname { clen := utf8.RuneLen(c) islast := (j+clen == len(optname)) if !islast && argument == nil { rr := optname[j+clen:] next, _ := utf8.DecodeRuneInString(rr) info, _ := p.getShort(c) if info != nil && info.canArgument() { if snext, _ := p.getShort(next); snext == nil { // Consider the next stuff as an argument argument = &rr islast = true } } } err, i, option = p.parseShort(args, c, islast, argument, i) if err != nil || islast { break } } } if err != nil { ignoreUnknown := (p.Options & IgnoreUnknown) != None parseErr, ok := err.(*Error) if !ok { parseErr = newError(ErrUnknown, err.Error()) } if ignoreUnknown { ret = append(ret, arg) } if !(parseErr.Type == ErrUnknownFlag && ignoreUnknown) { if (p.Options & PrintErrors) != None { if parseErr.Type == ErrHelp { fmt.Fprintln(os.Stderr, err) } else { fmt.Fprintf(os.Stderr, "Flags error: %s\n", err.Error()) } } return nil, wrapError(err) } } else { delete(required, option) } } if len(required) > 0 { names := make([]string, 0, len(required)) for k, _ := range required { names = append(names, "`"+k.String()+"'") } var msg string if len(names) == 1 { msg = fmt.Sprintf("the required flag %s was not specified", names[0]) } else { msg = fmt.Sprintf("the required flags %s and %s were not specified", strings.Join(names[:len(names)-1], ", "), names[len(names)-1]) } err := newError(ErrRequired, msg) if (p.Options & PrintErrors) != None { fmt.Fprintln(os.Stderr, err) } return nil, err } if p.currentCommand != nil { // Execute group cmd, ok := p.currentCommand.data.(Command) if ok { err := cmd.Execute(ret) if err != nil && (p.Options&PrintErrors) != None { fmt.Fprintln(os.Stderr, err) } return nil, err } } else if len(commands) != 0 { cmdnames := make([]string, 0, len(commands)) for k, _ := range commands { cmdnames = append(cmdnames, k) } sort.Strings(cmdnames) var msg string if len(ret) != 0 { c, l := p.closest(ret[0], cmdnames) msg = fmt.Sprintf("Unknown command `%s'", ret[0]) if float32(l)/float32(len(c)) < 0.5 { msg = fmt.Sprintf("%s, did you mean `%s'?", msg, c) } else { msg = fmt.Sprintf("%s. Please specify one command of: %s", msg, strings.Join(cmdnames, ", ")) } } else { msg = fmt.Sprintf("Please specify one command of: %s", strings.Join(cmdnames, ", ")) } err := newError(ErrRequired, msg) if (p.Options & PrintErrors) != None { fmt.Fprintln(os.Stderr, msg) } return nil, err } return ret, nil } go-flags-1/parser_private.go000066400000000000000000000074011224236051200162670ustar00rootroot00000000000000package flags import ( "fmt" "strings" "unicode/utf8" ) func (p *Parser) storeDefaults() { p.EachGroup(func(index int, grp *Group) { grp.storeDefaults() }) } func (p *Parser) parseOption(group *Group, args []string, name string, option *Option, canarg bool, argument *string, index int) (error, int, *Option) { var err error if !option.canArgument() { if canarg && argument != nil { return newError(ErrNoArgumentForBool, fmt.Sprintf("bool flag `%s' cannot have an argument", option)), index, option } err = option.Set(nil) } else if canarg && (argument != nil || index < len(args) && !argumentIsOption(args[index])) { if argument == nil { argument = &args[index] index++ } err = option.Set(argument) } else if option.OptionalArgument { option.clear() for _, v := range option.OptionalValue { err = option.Set(&v) if err != nil { break } } } else { return newError(ErrExpectedArgument, fmt.Sprintf("expected argument for flag `%s'", option)), index, option } if err != nil { if _, ok := err.(*Error); !ok { err = newError(ErrMarshal, fmt.Sprintf("invalid argument for flag `%s' (expected %s): %s", option, option.Value.Type(), err.Error())) } } return err, index, option } func (p *Parser) parseLong(args []string, name string, argument *string, index int) (error, int, *Option) { name = strings.ToLower(name) var option *Option var group *Group p.EachGroup(func(index int, grp *Group) { if opt := grp.LongNames[name]; opt != nil { option = opt group = grp } }) if option != nil { return p.parseOption(group, args, name, option, true, argument, index) } return newError(ErrUnknownFlag, fmt.Sprintf("unknown flag `%s'", name)), index, nil } func (p *Parser) getShort(name rune) (*Option, *Group) { var option *Option var group *Group p.EachGroup(func(index int, grp *Group) { if opt := grp.ShortNames[name]; opt != nil { option = opt group = grp } }) if option != nil { return option, group } return nil, nil } func (p *Parser) parseShort(args []string, name rune, islast bool, argument *string, index int) (error, int, *Option) { names := make([]byte, utf8.RuneLen(name)) utf8.EncodeRune(names, name) option, grp := p.getShort(name) if option != nil { if option.canArgument() && !islast && !option.OptionalArgument { return newError(ErrExpectedArgument, fmt.Sprintf("expected argument for flag `%s'", option)), index, option } return p.parseOption(grp, args, string(names), option, islast, argument, index) } return newError(ErrUnknownFlag, fmt.Sprintf("unknown flag `%s'", string(names))), index, nil } func (p *Parser) parseIni(ini Ini) error { for groupName, section := range ini { group := p.GroupsMap[strings.ToLower(groupName)] if group == nil { return newError(ErrUnknownGroup, fmt.Sprintf("could not find option group `%s'", groupName)) } for _, inival := range section { opt, usedName := group.lookupByName(inival.Name, true) if opt == nil { if (p.Options & IgnoreUnknown) == None { return newError(ErrUnknownFlag, fmt.Sprintf("unknown option: %s", inival.Name)) } continue } if opt.tag.Get("no-ini") != "" { continue } opt.iniUsedName = usedName pval := &inival.Value if opt.isBool() && len(inival.Value) == 0 { pval = nil } if err := opt.Set(pval); err != nil { return wrapError(err) } } } return nil } func (p *Parser) currentCommander() *Commander { if p.currentCommand != nil { return &p.currentCommand.Commander } return &p.Commander } func (p *Parser) eachTopLevelGroup(cb func(int, *Group)) { index := 0 for _, group := range p.Groups { if !group.IsCommand { index = group.each(index, cb) } } } go-flags-1/termsize.go000066400000000000000000000005661224236051200151100ustar00rootroot00000000000000// +build !windows package flags import ( "syscall" "unsafe" ) type winsize struct { ws_row, ws_col uint16 ws_xpixel, ws_ypixel uint16 } func getTerminalColumns() int { ws := winsize{} if TIOCGWINSZ != 0 { syscall.Syscall(syscall.SYS_IOCTL, uintptr(0), uintptr(TIOCGWINSZ), uintptr(unsafe.Pointer(&ws))) return int(ws.ws_col) } return 80 } go-flags-1/termsize_linux.go000066400000000000000000000000771224236051200163240ustar00rootroot00000000000000// +build linux package flags const ( TIOCGWINSZ = 0x5413 ) go-flags-1/termsize_other.go000066400000000000000000000001351224236051200163010ustar00rootroot00000000000000// +build !darwin,!freebsd,!netbsd,!openbsd,!linux package flags const ( TIOCGWINSZ = 0 ) go-flags-1/termsize_unix.go000066400000000000000000000001331224236051200161410ustar00rootroot00000000000000// +build darwin freebsd netbsd openbsd package flags const ( TIOCGWINSZ = 0x40087468 ) go-flags-1/termsize_windows.go000066400000000000000000000000741224236051200166540ustar00rootroot00000000000000package flags func getTerminalColumns() int { return 80 } go-flags-1/unknown_test.go000066400000000000000000000020041224236051200157710ustar00rootroot00000000000000package flags import ( "github.com/jessevdk/go-flags" "testing" ) func TestUnknownFlags(t *testing.T) { var opts = struct { Verbose []bool `short:"v" long:"verbose" description:"Verbose output"` }{} args := []string{ "-f", } p := flags.NewParser(&opts, 0) args, err := p.ParseArgs(args) if err == nil { t.Fatal("Expected error for unknown argument") } } func TestIgnoreUnknownFlags(t *testing.T) { var opts = struct { Verbose []bool `short:"v" long:"verbose" description:"Verbose output"` }{} args := []string{ "hello", "world", "-v", "--foo=bar", "--verbose", "-f", } p := flags.NewParser(&opts, flags.IgnoreUnknown) args, err := p.ParseArgs(args) if err != nil { t.Fatal(err) } exargs := []string{ "hello", "world", "--foo=bar", "-f", } issame := (len(args) == len(exargs)) if issame { for i := 0; i < len(args); i++ { if args[i] != exargs[i] { issame = false break } } } if !issame { t.Fatalf("Expected %v but got %v", exargs, args) } }