2015-12-31 06:21:10 -05:00
|
|
|
// Copyright 2014-2016 The Docker & Go Authors. All rights reserved.
|
2013-12-23 14:43:54 -05:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-07-21 15:53:13 -04:00
|
|
|
// Package mflag implements command-line flag parsing.
|
|
|
|
//
|
|
|
|
// Usage:
|
|
|
|
//
|
|
|
|
// Define flags using flag.String(), Bool(), Int(), etc.
|
|
|
|
//
|
|
|
|
// This declares an integer flag, -f or --flagname, stored in the pointer ip, with type *int.
|
|
|
|
// import "flag /github.com/docker/docker/pkg/mflag"
|
|
|
|
// var ip = flag.Int([]string{"f", "-flagname"}, 1234, "help message for flagname")
|
|
|
|
// If you like, you can bind the flag to a variable using the Var() functions.
|
|
|
|
// var flagvar int
|
|
|
|
// func init() {
|
|
|
|
// // -flaghidden will work, but will be hidden from the usage
|
|
|
|
// flag.IntVar(&flagvar, []string{"f", "#flaghidden", "-flagname"}, 1234, "help message for flagname")
|
|
|
|
// }
|
|
|
|
// Or you can create custom flags that satisfy the Value interface (with
|
|
|
|
// pointer receivers) and couple them to flag parsing by
|
|
|
|
// flag.Var(&flagVal, []string{"name"}, "help message for flagname")
|
|
|
|
// For such flags, the default value is just the initial value of the variable.
|
|
|
|
//
|
|
|
|
// You can also add "deprecated" flags, they are still usable, but are not shown
|
|
|
|
// in the usage and will display a warning when you try to use them. `#` before
|
|
|
|
// an option means this option is deprecated, if there is an following option
|
|
|
|
// without `#` ahead, then that's the replacement, if not, it will just be removed:
|
|
|
|
// var ip = flag.Int([]string{"#f", "#flagname", "-flagname"}, 1234, "help message for flagname")
|
|
|
|
// this will display: `Warning: '-f' is deprecated, it will be replaced by '--flagname' soon. See usage.` or
|
|
|
|
// this will display: `Warning: '-flagname' is deprecated, it will be replaced by '--flagname' soon. See usage.`
|
|
|
|
// var ip = flag.Int([]string{"f", "#flagname"}, 1234, "help message for flagname")
|
|
|
|
// will display: `Warning: '-flagname' is deprecated, it will be removed soon. See usage.`
|
|
|
|
// so you can only use `-f`.
|
|
|
|
//
|
|
|
|
// You can also group one letter flags, bif you declare
|
|
|
|
// var v = flag.Bool([]string{"v", "-verbose"}, false, "help message for verbose")
|
|
|
|
// var s = flag.Bool([]string{"s", "-slow"}, false, "help message for slow")
|
|
|
|
// you will be able to use the -vs or -sv
|
|
|
|
//
|
|
|
|
// After all flags are defined, call
|
|
|
|
// flag.Parse()
|
|
|
|
// to parse the command line into the defined flags.
|
|
|
|
//
|
|
|
|
// Flags may then be used directly. If you're using the flags themselves,
|
|
|
|
// they are all pointers; if you bind to variables, they're values.
|
|
|
|
// fmt.Println("ip has value ", *ip)
|
|
|
|
// fmt.Println("flagvar has value ", flagvar)
|
|
|
|
//
|
|
|
|
// After parsing, the arguments after the flag are available as the
|
|
|
|
// slice flag.Args() or individually as flag.Arg(i).
|
|
|
|
// The arguments are indexed from 0 through flag.NArg()-1.
|
|
|
|
//
|
|
|
|
// Command line flag syntax:
|
|
|
|
// -flag
|
|
|
|
// -flag=x
|
|
|
|
// -flag="x"
|
|
|
|
// -flag='x'
|
|
|
|
// -flag x // non-boolean flags only
|
|
|
|
// One or two minus signs may be used; they are equivalent.
|
|
|
|
// The last form is not permitted for boolean flags because the
|
|
|
|
// meaning of the command
|
|
|
|
// cmd -x *
|
|
|
|
// will change if there is a file called 0, false, etc. You must
|
|
|
|
// use the -flag=false form to turn off a boolean flag.
|
|
|
|
//
|
|
|
|
// Flag parsing stops just before the first non-flag argument
|
|
|
|
// ("-" is a non-flag argument) or after the terminator "--".
|
|
|
|
//
|
|
|
|
// Integer flags accept 1234, 0664, 0x1234 and may be negative.
|
|
|
|
// Boolean flags may be 1, 0, t, f, true, false, TRUE, FALSE, True, False.
|
|
|
|
// Duration flags accept any input valid for time.ParseDuration.
|
|
|
|
//
|
|
|
|
// The default set of command-line flags is controlled by
|
|
|
|
// top-level functions. The FlagSet type allows one to define
|
|
|
|
// independent sets of flags, such as to implement subcommands
|
|
|
|
// in a command-line interface. The methods of FlagSet are
|
|
|
|
// analogous to the top-level functions for the command-line
|
|
|
|
// flag set.
|
2013-12-23 14:43:54 -05:00
|
|
|
|
|
|
|
package mflag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2015-02-06 17:17:34 -05:00
|
|
|
"runtime"
|
2013-12-23 14:43:54 -05:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-05-06 13:43:46 -04:00
|
|
|
"text/tabwriter"
|
2013-12-23 14:43:54 -05:00
|
|
|
"time"
|
2015-02-18 16:32:42 -05:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/homedir"
|
2013-12-23 14:43:54 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// ErrHelp is the error returned if the flag -help is invoked but no such flag is defined.
|
|
|
|
var ErrHelp = errors.New("flag: help requested")
|
|
|
|
|
2014-02-18 19:24:32 -05:00
|
|
|
// ErrRetry is the error returned if you need to try letter by letter
|
|
|
|
var ErrRetry = errors.New("flag: retry")
|
|
|
|
|
2013-12-23 14:43:54 -05:00
|
|
|
// -- bool Value
|
|
|
|
type boolValue bool
|
|
|
|
|
|
|
|
func newBoolValue(val bool, p *bool) *boolValue {
|
|
|
|
*p = val
|
|
|
|
return (*boolValue)(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *boolValue) Set(s string) error {
|
|
|
|
v, err := strconv.ParseBool(s)
|
|
|
|
*b = boolValue(v)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *boolValue) Get() interface{} { return bool(*b) }
|
|
|
|
|
|
|
|
func (b *boolValue) String() string { return fmt.Sprintf("%v", *b) }
|
|
|
|
|
|
|
|
func (b *boolValue) IsBoolFlag() bool { return true }
|
|
|
|
|
|
|
|
// optional interface to indicate boolean flags that can be
|
|
|
|
// supplied without "=value" text
|
|
|
|
type boolFlag interface {
|
|
|
|
Value
|
|
|
|
IsBoolFlag() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- int Value
|
|
|
|
type intValue int
|
|
|
|
|
|
|
|
func newIntValue(val int, p *int) *intValue {
|
|
|
|
*p = val
|
|
|
|
return (*intValue)(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *intValue) Set(s string) error {
|
|
|
|
v, err := strconv.ParseInt(s, 0, 64)
|
|
|
|
*i = intValue(v)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *intValue) Get() interface{} { return int(*i) }
|
|
|
|
|
|
|
|
func (i *intValue) String() string { return fmt.Sprintf("%v", *i) }
|
|
|
|
|
|
|
|
// -- int64 Value
|
|
|
|
type int64Value int64
|
|
|
|
|
|
|
|
func newInt64Value(val int64, p *int64) *int64Value {
|
|
|
|
*p = val
|
|
|
|
return (*int64Value)(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *int64Value) Set(s string) error {
|
|
|
|
v, err := strconv.ParseInt(s, 0, 64)
|
|
|
|
*i = int64Value(v)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *int64Value) Get() interface{} { return int64(*i) }
|
|
|
|
|
|
|
|
func (i *int64Value) String() string { return fmt.Sprintf("%v", *i) }
|
|
|
|
|
|
|
|
// -- uint Value
|
|
|
|
type uintValue uint
|
|
|
|
|
|
|
|
func newUintValue(val uint, p *uint) *uintValue {
|
|
|
|
*p = val
|
|
|
|
return (*uintValue)(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *uintValue) Set(s string) error {
|
|
|
|
v, err := strconv.ParseUint(s, 0, 64)
|
|
|
|
*i = uintValue(v)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *uintValue) Get() interface{} { return uint(*i) }
|
|
|
|
|
|
|
|
func (i *uintValue) String() string { return fmt.Sprintf("%v", *i) }
|
|
|
|
|
|
|
|
// -- uint64 Value
|
|
|
|
type uint64Value uint64
|
|
|
|
|
|
|
|
func newUint64Value(val uint64, p *uint64) *uint64Value {
|
|
|
|
*p = val
|
|
|
|
return (*uint64Value)(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *uint64Value) Set(s string) error {
|
|
|
|
v, err := strconv.ParseUint(s, 0, 64)
|
|
|
|
*i = uint64Value(v)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *uint64Value) Get() interface{} { return uint64(*i) }
|
|
|
|
|
|
|
|
func (i *uint64Value) String() string { return fmt.Sprintf("%v", *i) }
|
|
|
|
|
2015-10-06 11:21:43 -04:00
|
|
|
// -- uint16 Value
|
|
|
|
type uint16Value uint16
|
|
|
|
|
|
|
|
func newUint16Value(val uint16, p *uint16) *uint16Value {
|
|
|
|
*p = val
|
|
|
|
return (*uint16Value)(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *uint16Value) Set(s string) error {
|
|
|
|
v, err := strconv.ParseUint(s, 0, 16)
|
|
|
|
*i = uint16Value(v)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *uint16Value) Get() interface{} { return uint16(*i) }
|
|
|
|
|
|
|
|
func (i *uint16Value) String() string { return fmt.Sprintf("%v", *i) }
|
|
|
|
|
2013-12-23 14:43:54 -05:00
|
|
|
// -- string Value
|
|
|
|
type stringValue string
|
|
|
|
|
|
|
|
func newStringValue(val string, p *string) *stringValue {
|
|
|
|
*p = val
|
|
|
|
return (*stringValue)(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stringValue) Set(val string) error {
|
|
|
|
*s = stringValue(val)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stringValue) Get() interface{} { return string(*s) }
|
|
|
|
|
|
|
|
func (s *stringValue) String() string { return fmt.Sprintf("%s", *s) }
|
|
|
|
|
|
|
|
// -- float64 Value
|
|
|
|
type float64Value float64
|
|
|
|
|
|
|
|
func newFloat64Value(val float64, p *float64) *float64Value {
|
|
|
|
*p = val
|
|
|
|
return (*float64Value)(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *float64Value) Set(s string) error {
|
|
|
|
v, err := strconv.ParseFloat(s, 64)
|
|
|
|
*f = float64Value(v)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *float64Value) Get() interface{} { return float64(*f) }
|
|
|
|
|
|
|
|
func (f *float64Value) String() string { return fmt.Sprintf("%v", *f) }
|
|
|
|
|
|
|
|
// -- time.Duration Value
|
|
|
|
type durationValue time.Duration
|
|
|
|
|
|
|
|
func newDurationValue(val time.Duration, p *time.Duration) *durationValue {
|
|
|
|
*p = val
|
|
|
|
return (*durationValue)(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *durationValue) Set(s string) error {
|
|
|
|
v, err := time.ParseDuration(s)
|
|
|
|
*d = durationValue(v)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *durationValue) Get() interface{} { return time.Duration(*d) }
|
|
|
|
|
|
|
|
func (d *durationValue) String() string { return (*time.Duration)(d).String() }
|
|
|
|
|
|
|
|
// Value is the interface to the dynamic value stored in a flag.
|
|
|
|
// (The default value is represented as a string.)
|
|
|
|
//
|
|
|
|
// If a Value has an IsBoolFlag() bool method returning true,
|
|
|
|
// the command-line parser makes -name equivalent to -name=true
|
|
|
|
// rather than using the next command-line argument.
|
|
|
|
type Value interface {
|
|
|
|
String() string
|
|
|
|
Set(string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Getter is an interface that allows the contents of a Value to be retrieved.
|
|
|
|
// It wraps the Value interface, rather than being part of it, because it
|
|
|
|
// appeared after Go 1 and its compatibility rules. All Value types provided
|
|
|
|
// by this package satisfy the Getter interface.
|
|
|
|
type Getter interface {
|
|
|
|
Value
|
|
|
|
Get() interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrorHandling defines how to handle flag parsing errors.
|
|
|
|
type ErrorHandling int
|
|
|
|
|
2015-07-21 15:53:13 -04:00
|
|
|
// ErrorHandling strategies available when a flag parsing error occurs
|
2013-12-23 14:43:54 -05:00
|
|
|
const (
|
|
|
|
ContinueOnError ErrorHandling = iota
|
|
|
|
ExitOnError
|
|
|
|
PanicOnError
|
|
|
|
)
|
|
|
|
|
|
|
|
// A FlagSet represents a set of defined flags. The zero value of a FlagSet
|
|
|
|
// has no name and has ContinueOnError error handling.
|
|
|
|
type FlagSet struct {
|
|
|
|
// Usage is the function called when an error occurs while parsing flags.
|
|
|
|
// The field is a function (not a method) that may be changed to point to
|
|
|
|
// a custom error handler.
|
2015-03-31 03:11:03 -04:00
|
|
|
Usage func()
|
|
|
|
ShortUsage func()
|
2013-12-23 14:43:54 -05:00
|
|
|
|
2014-11-05 11:57:51 -05:00
|
|
|
name string
|
|
|
|
parsed bool
|
|
|
|
actual map[string]*Flag
|
|
|
|
formal map[string]*Flag
|
|
|
|
args []string // arguments after flags
|
|
|
|
errorHandling ErrorHandling
|
|
|
|
output io.Writer // nil means stderr; use Out() accessor
|
|
|
|
nArgRequirements []nArgRequirement
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// A Flag represents the state of a flag.
|
|
|
|
type Flag struct {
|
|
|
|
Names []string // name as it appears on command line
|
|
|
|
Usage string // help message
|
|
|
|
Value Value // value as set
|
|
|
|
DefValue string // default value (as text); for usage message
|
|
|
|
}
|
|
|
|
|
2014-03-06 21:20:59 -05:00
|
|
|
type flagSlice []string
|
|
|
|
|
|
|
|
func (p flagSlice) Len() int { return len(p) }
|
|
|
|
func (p flagSlice) Less(i, j int) bool {
|
2014-05-16 19:40:27 -04:00
|
|
|
pi, pj := strings.TrimPrefix(p[i], "-"), strings.TrimPrefix(p[j], "-")
|
|
|
|
lpi, lpj := strings.ToLower(pi), strings.ToLower(pj)
|
|
|
|
if lpi != lpj {
|
|
|
|
return lpi < lpj
|
2014-03-06 21:20:59 -05:00
|
|
|
}
|
|
|
|
return pi < pj
|
|
|
|
}
|
|
|
|
func (p flagSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
|
|
|
|
2013-12-23 14:43:54 -05:00
|
|
|
// sortFlags returns the flags as a slice in lexicographical sorted order.
|
|
|
|
func sortFlags(flags map[string]*Flag) []*Flag {
|
2014-03-06 21:20:59 -05:00
|
|
|
var list flagSlice
|
2014-08-29 00:55:49 -04:00
|
|
|
|
|
|
|
// The sorted list is based on the first name, when flag map might use the other names.
|
|
|
|
nameMap := make(map[string]string)
|
|
|
|
|
|
|
|
for n, f := range flags {
|
2014-03-05 14:45:57 -05:00
|
|
|
fName := strings.TrimPrefix(f.Names[0], "#")
|
2014-08-29 00:55:49 -04:00
|
|
|
nameMap[fName] = n
|
2014-01-31 14:33:48 -05:00
|
|
|
if len(f.Names) == 1 {
|
2014-03-05 14:45:57 -05:00
|
|
|
list = append(list, fName)
|
2014-01-31 14:33:48 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-12-23 14:43:54 -05:00
|
|
|
found := false
|
|
|
|
for _, name := range list {
|
|
|
|
if name == fName {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
list = append(list, fName)
|
|
|
|
}
|
|
|
|
}
|
2014-03-06 21:20:59 -05:00
|
|
|
sort.Sort(list)
|
2013-12-23 14:43:54 -05:00
|
|
|
result := make([]*Flag, len(list))
|
|
|
|
for i, name := range list {
|
2014-08-29 00:55:49 -04:00
|
|
|
result[i] = flags[nameMap[name]]
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2014-11-05 11:57:51 -05:00
|
|
|
// Name returns the name of the FlagSet.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) Name() string {
|
|
|
|
return fs.name
|
2014-11-05 11:57:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Out returns the destination for usage and error messages.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) Out() io.Writer {
|
|
|
|
if fs.output == nil {
|
2013-12-23 14:43:54 -05:00
|
|
|
return os.Stderr
|
|
|
|
}
|
2015-07-21 15:53:13 -04:00
|
|
|
return fs.output
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetOutput sets the destination for usage and error messages.
|
|
|
|
// If output is nil, os.Stderr is used.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) SetOutput(output io.Writer) {
|
|
|
|
fs.output = output
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// VisitAll visits the flags in lexicographical order, calling fn for each.
|
|
|
|
// It visits all flags, even those not set.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) VisitAll(fn func(*Flag)) {
|
|
|
|
for _, flag := range sortFlags(fs.formal) {
|
2013-12-23 14:43:54 -05:00
|
|
|
fn(flag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// VisitAll visits the command-line flags in lexicographical order, calling
|
|
|
|
// fn for each. It visits all flags, even those not set.
|
|
|
|
func VisitAll(fn func(*Flag)) {
|
|
|
|
CommandLine.VisitAll(fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Visit visits the flags in lexicographical order, calling fn for each.
|
|
|
|
// It visits only those flags that have been set.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) Visit(fn func(*Flag)) {
|
|
|
|
for _, flag := range sortFlags(fs.actual) {
|
2013-12-23 14:43:54 -05:00
|
|
|
fn(flag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Visit visits the command-line flags in lexicographical order, calling fn
|
|
|
|
// for each. It visits only those flags that have been set.
|
|
|
|
func Visit(fn func(*Flag)) {
|
|
|
|
CommandLine.Visit(fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup returns the Flag structure of the named flag, returning nil if none exists.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) Lookup(name string) *Flag {
|
|
|
|
return fs.formal[name]
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
|
2015-07-21 15:53:13 -04:00
|
|
|
// IsSet indicates whether the specified flag is set in the given FlagSet
|
|
|
|
func (fs *FlagSet) IsSet(name string) bool {
|
|
|
|
return fs.actual[name] != nil
|
2014-11-20 10:29:04 -05:00
|
|
|
}
|
|
|
|
|
2013-12-23 14:43:54 -05:00
|
|
|
// Lookup returns the Flag structure of the named command-line flag,
|
|
|
|
// returning nil if none exists.
|
|
|
|
func Lookup(name string) *Flag {
|
|
|
|
return CommandLine.formal[name]
|
|
|
|
}
|
|
|
|
|
2015-07-21 15:53:13 -04:00
|
|
|
// IsSet indicates whether the specified flag was specified at all on the cmd line.
|
2014-11-20 10:29:04 -05:00
|
|
|
func IsSet(name string) bool {
|
|
|
|
return CommandLine.IsSet(name)
|
|
|
|
}
|
|
|
|
|
2014-11-05 11:57:51 -05:00
|
|
|
type nArgRequirementType int
|
|
|
|
|
2014-10-15 17:14:12 -04:00
|
|
|
// Indicator used to pass to BadArgs function
|
|
|
|
const (
|
2014-11-05 11:57:51 -05:00
|
|
|
Exact nArgRequirementType = iota
|
|
|
|
Max
|
|
|
|
Min
|
2014-10-15 17:14:12 -04:00
|
|
|
)
|
|
|
|
|
2014-11-05 11:57:51 -05:00
|
|
|
type nArgRequirement struct {
|
|
|
|
Type nArgRequirementType
|
|
|
|
N int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Require adds a requirement about the number of arguments for the FlagSet.
|
|
|
|
// The first parameter can be Exact, Max, or Min to respectively specify the exact,
|
|
|
|
// the maximum, or the minimal number of arguments required.
|
|
|
|
// The actual check is done in FlagSet.CheckArgs().
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) Require(nArgRequirementType nArgRequirementType, nArg int) {
|
|
|
|
fs.nArgRequirements = append(fs.nArgRequirements, nArgRequirement{nArgRequirementType, nArg})
|
2014-11-05 11:57:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckArgs uses the requirements set by FlagSet.Require() to validate
|
|
|
|
// the number of arguments. If the requirements are not met,
|
|
|
|
// an error message string is returned.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) CheckArgs() (message string) {
|
|
|
|
for _, req := range fs.nArgRequirements {
|
2014-11-05 11:57:51 -05:00
|
|
|
var arguments string
|
|
|
|
if req.N == 1 {
|
|
|
|
arguments = "1 argument"
|
2014-10-15 17:14:12 -04:00
|
|
|
} else {
|
2014-11-05 11:57:51 -05:00
|
|
|
arguments = fmt.Sprintf("%d arguments", req.N)
|
2014-10-15 17:14:12 -04:00
|
|
|
}
|
2014-11-05 11:57:51 -05:00
|
|
|
|
|
|
|
str := func(kind string) string {
|
2015-07-21 15:53:13 -04:00
|
|
|
return fmt.Sprintf("%q requires %s%s", fs.name, kind, arguments)
|
2014-10-15 17:14:12 -04:00
|
|
|
}
|
2014-11-05 11:57:51 -05:00
|
|
|
|
|
|
|
switch req.Type {
|
|
|
|
case Exact:
|
2015-07-21 15:53:13 -04:00
|
|
|
if fs.NArg() != req.N {
|
2014-11-05 11:57:51 -05:00
|
|
|
return str("")
|
|
|
|
}
|
|
|
|
case Max:
|
2015-07-21 15:53:13 -04:00
|
|
|
if fs.NArg() > req.N {
|
2014-11-05 11:57:51 -05:00
|
|
|
return str("a maximum of ")
|
|
|
|
}
|
|
|
|
case Min:
|
2015-07-21 15:53:13 -04:00
|
|
|
if fs.NArg() < req.N {
|
2014-11-05 11:57:51 -05:00
|
|
|
return str("a minimum of ")
|
|
|
|
}
|
2014-10-15 17:14:12 -04:00
|
|
|
}
|
|
|
|
}
|
2014-11-05 11:57:51 -05:00
|
|
|
return ""
|
2014-10-15 17:14:12 -04:00
|
|
|
}
|
|
|
|
|
2013-12-23 14:43:54 -05:00
|
|
|
// Set sets the value of the named flag.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) Set(name, value string) error {
|
|
|
|
flag, ok := fs.formal[name]
|
2013-12-23 14:43:54 -05:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("no such flag -%v", name)
|
|
|
|
}
|
2015-04-26 12:50:25 -04:00
|
|
|
if err := flag.Value.Set(value); err != nil {
|
2013-12-23 14:43:54 -05:00
|
|
|
return err
|
|
|
|
}
|
2015-07-21 15:53:13 -04:00
|
|
|
if fs.actual == nil {
|
|
|
|
fs.actual = make(map[string]*Flag)
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
2015-07-21 15:53:13 -04:00
|
|
|
fs.actual[name] = flag
|
2013-12-23 14:43:54 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets the value of the named command-line flag.
|
|
|
|
func Set(name, value string) error {
|
|
|
|
return CommandLine.Set(name, value)
|
|
|
|
}
|
|
|
|
|
2015-11-18 21:27:59 -05:00
|
|
|
// isZeroValue guesses whether the string represents the zero
|
|
|
|
// value for a flag. It is not accurate but in practice works OK.
|
|
|
|
func isZeroValue(value string) bool {
|
|
|
|
switch value {
|
|
|
|
case "false":
|
|
|
|
return true
|
|
|
|
case "":
|
|
|
|
return true
|
|
|
|
case "0":
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2013-12-23 14:43:54 -05:00
|
|
|
// PrintDefaults prints, to standard error unless configured
|
|
|
|
// otherwise, the default values of all defined flags in the set.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) PrintDefaults() {
|
|
|
|
writer := tabwriter.NewWriter(fs.Out(), 20, 1, 3, ' ', 0)
|
2015-03-11 05:14:49 -04:00
|
|
|
home := homedir.Get()
|
|
|
|
|
|
|
|
// Don't substitute when HOME is /
|
|
|
|
if runtime.GOOS != "windows" && home == "/" {
|
|
|
|
home = ""
|
2015-02-06 17:17:34 -05:00
|
|
|
}
|
2015-05-23 11:57:50 -04:00
|
|
|
|
|
|
|
// Add a blank line between cmd description and list of options
|
2015-07-21 15:53:13 -04:00
|
|
|
if fs.FlagCount() > 0 {
|
2015-05-23 11:57:50 -04:00
|
|
|
fmt.Fprintln(writer, "")
|
|
|
|
}
|
|
|
|
|
2015-07-21 15:53:13 -04:00
|
|
|
fs.VisitAll(func(flag *Flag) {
|
2013-12-23 14:43:54 -05:00
|
|
|
names := []string{}
|
|
|
|
for _, name := range flag.Names {
|
|
|
|
if name[0] != '#' {
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
}
|
2015-05-05 00:18:28 -04:00
|
|
|
if len(names) > 0 && len(flag.Usage) > 0 {
|
2015-02-03 22:51:35 -05:00
|
|
|
val := flag.DefValue
|
|
|
|
|
|
|
|
if home != "" && strings.HasPrefix(val, home) {
|
2015-02-18 16:32:42 -05:00
|
|
|
val = homedir.GetShortcutString() + val[len(home):]
|
2015-02-03 22:51:35 -05:00
|
|
|
}
|
|
|
|
|
2015-11-18 21:27:59 -05:00
|
|
|
if isZeroValue(val) {
|
|
|
|
format := " -%s"
|
|
|
|
fmt.Fprintf(writer, format, strings.Join(names, ", -"))
|
|
|
|
} else {
|
|
|
|
format := " -%s=%s"
|
|
|
|
fmt.Fprintf(writer, format, strings.Join(names, ", -"), val)
|
|
|
|
}
|
2015-12-03 02:16:53 -05:00
|
|
|
for _, line := range strings.Split(flag.Usage, "\n") {
|
2014-05-06 13:43:46 -04:00
|
|
|
fmt.Fprintln(writer, "\t", line)
|
|
|
|
}
|
2014-03-05 14:27:39 -05:00
|
|
|
}
|
2013-12-23 14:43:54 -05:00
|
|
|
})
|
2014-05-06 13:43:46 -04:00
|
|
|
writer.Flush()
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrintDefaults prints to standard error the default values of all defined command-line flags.
|
|
|
|
func PrintDefaults() {
|
|
|
|
CommandLine.PrintDefaults()
|
|
|
|
}
|
|
|
|
|
|
|
|
// defaultUsage is the default function to print a usage message.
|
2015-07-21 15:53:13 -04:00
|
|
|
func defaultUsage(fs *FlagSet) {
|
|
|
|
if fs.name == "" {
|
|
|
|
fmt.Fprintf(fs.Out(), "Usage:\n")
|
2013-12-23 14:43:54 -05:00
|
|
|
} else {
|
2015-07-21 15:53:13 -04:00
|
|
|
fmt.Fprintf(fs.Out(), "Usage of %s:\n", fs.name)
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
2015-07-21 15:53:13 -04:00
|
|
|
fs.PrintDefaults()
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Usage is not just defaultUsage(CommandLine)
|
|
|
|
// because it serves (via godoc flag Usage) as the example
|
|
|
|
// for how to write your own usage function.
|
|
|
|
|
|
|
|
// Usage prints to standard error a usage message documenting all defined command-line flags.
|
|
|
|
// The function is a variable that may be changed to point to a custom function.
|
|
|
|
var Usage = func() {
|
2015-05-18 05:33:51 -04:00
|
|
|
fmt.Fprintf(CommandLine.Out(), "Usage of %s:\n", os.Args[0])
|
2013-12-23 14:43:54 -05:00
|
|
|
PrintDefaults()
|
|
|
|
}
|
|
|
|
|
2015-11-02 11:02:25 -05:00
|
|
|
// ShortUsage prints to standard error a usage message documenting the standard command layout
|
2015-03-31 03:11:03 -04:00
|
|
|
// The function is a variable that may be changed to point to a custom function.
|
|
|
|
var ShortUsage = func() {
|
|
|
|
fmt.Fprintf(CommandLine.output, "Usage of %s:\n", os.Args[0])
|
|
|
|
}
|
|
|
|
|
2014-08-24 21:53:31 -04:00
|
|
|
// FlagCount returns the number of flags that have been defined.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) FlagCount() int { return len(sortFlags(fs.formal)) }
|
2014-08-24 21:53:31 -04:00
|
|
|
|
|
|
|
// FlagCountUndeprecated returns the number of undeprecated flags that have been defined.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) FlagCountUndeprecated() int {
|
2014-08-24 21:53:31 -04:00
|
|
|
count := 0
|
2015-07-21 15:53:13 -04:00
|
|
|
for _, flag := range sortFlags(fs.formal) {
|
2014-08-24 21:53:31 -04:00
|
|
|
for _, name := range flag.Names {
|
|
|
|
if name[0] != '#' {
|
|
|
|
count++
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2013-12-23 14:43:54 -05:00
|
|
|
// NFlag returns the number of flags that have been set.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) NFlag() int { return len(fs.actual) }
|
2013-12-23 14:43:54 -05:00
|
|
|
|
|
|
|
// NFlag returns the number of command-line flags that have been set.
|
|
|
|
func NFlag() int { return len(CommandLine.actual) }
|
|
|
|
|
|
|
|
// Arg returns the i'th argument. Arg(0) is the first remaining argument
|
|
|
|
// after flags have been processed.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) Arg(i int) string {
|
|
|
|
if i < 0 || i >= len(fs.args) {
|
2013-12-23 14:43:54 -05:00
|
|
|
return ""
|
|
|
|
}
|
2015-07-21 15:53:13 -04:00
|
|
|
return fs.args[i]
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Arg returns the i'th command-line argument. Arg(0) is the first remaining argument
|
|
|
|
// after flags have been processed.
|
|
|
|
func Arg(i int) string {
|
|
|
|
return CommandLine.Arg(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NArg is the number of arguments remaining after flags have been processed.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) NArg() int { return len(fs.args) }
|
2013-12-23 14:43:54 -05:00
|
|
|
|
|
|
|
// NArg is the number of arguments remaining after flags have been processed.
|
|
|
|
func NArg() int { return len(CommandLine.args) }
|
|
|
|
|
|
|
|
// Args returns the non-flag arguments.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) Args() []string { return fs.args }
|
2013-12-23 14:43:54 -05:00
|
|
|
|
|
|
|
// Args returns the non-flag command-line arguments.
|
|
|
|
func Args() []string { return CommandLine.args }
|
|
|
|
|
|
|
|
// BoolVar defines a bool flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a bool variable in which to store the value of the flag.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) BoolVar(p *bool, names []string, value bool, usage string) {
|
|
|
|
fs.Var(newBoolValue(value, p), names, usage)
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// BoolVar defines a bool flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a bool variable in which to store the value of the flag.
|
|
|
|
func BoolVar(p *bool, names []string, value bool, usage string) {
|
|
|
|
CommandLine.Var(newBoolValue(value, p), names, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bool defines a bool flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a bool variable that stores the value of the flag.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) Bool(names []string, value bool, usage string) *bool {
|
2013-12-23 14:43:54 -05:00
|
|
|
p := new(bool)
|
2015-07-21 15:53:13 -04:00
|
|
|
fs.BoolVar(p, names, value, usage)
|
2013-12-23 14:43:54 -05:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bool defines a bool flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a bool variable that stores the value of the flag.
|
|
|
|
func Bool(names []string, value bool, usage string) *bool {
|
|
|
|
return CommandLine.Bool(names, value, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntVar defines an int flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to an int variable in which to store the value of the flag.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) IntVar(p *int, names []string, value int, usage string) {
|
|
|
|
fs.Var(newIntValue(value, p), names, usage)
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// IntVar defines an int flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to an int variable in which to store the value of the flag.
|
|
|
|
func IntVar(p *int, names []string, value int, usage string) {
|
|
|
|
CommandLine.Var(newIntValue(value, p), names, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Int defines an int flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of an int variable that stores the value of the flag.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) Int(names []string, value int, usage string) *int {
|
2013-12-23 14:43:54 -05:00
|
|
|
p := new(int)
|
2015-07-21 15:53:13 -04:00
|
|
|
fs.IntVar(p, names, value, usage)
|
2013-12-23 14:43:54 -05:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Int defines an int flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of an int variable that stores the value of the flag.
|
|
|
|
func Int(names []string, value int, usage string) *int {
|
|
|
|
return CommandLine.Int(names, value, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Int64Var defines an int64 flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to an int64 variable in which to store the value of the flag.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) Int64Var(p *int64, names []string, value int64, usage string) {
|
|
|
|
fs.Var(newInt64Value(value, p), names, usage)
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Int64Var defines an int64 flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to an int64 variable in which to store the value of the flag.
|
|
|
|
func Int64Var(p *int64, names []string, value int64, usage string) {
|
|
|
|
CommandLine.Var(newInt64Value(value, p), names, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Int64 defines an int64 flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of an int64 variable that stores the value of the flag.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) Int64(names []string, value int64, usage string) *int64 {
|
2013-12-23 14:43:54 -05:00
|
|
|
p := new(int64)
|
2015-07-21 15:53:13 -04:00
|
|
|
fs.Int64Var(p, names, value, usage)
|
2013-12-23 14:43:54 -05:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Int64 defines an int64 flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of an int64 variable that stores the value of the flag.
|
|
|
|
func Int64(names []string, value int64, usage string) *int64 {
|
|
|
|
return CommandLine.Int64(names, value, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UintVar defines a uint flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a uint variable in which to store the value of the flag.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) UintVar(p *uint, names []string, value uint, usage string) {
|
|
|
|
fs.Var(newUintValue(value, p), names, usage)
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// UintVar defines a uint flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a uint variable in which to store the value of the flag.
|
|
|
|
func UintVar(p *uint, names []string, value uint, usage string) {
|
|
|
|
CommandLine.Var(newUintValue(value, p), names, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uint defines a uint flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a uint variable that stores the value of the flag.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) Uint(names []string, value uint, usage string) *uint {
|
2013-12-23 14:43:54 -05:00
|
|
|
p := new(uint)
|
2015-07-21 15:53:13 -04:00
|
|
|
fs.UintVar(p, names, value, usage)
|
2013-12-23 14:43:54 -05:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uint defines a uint flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a uint variable that stores the value of the flag.
|
|
|
|
func Uint(names []string, value uint, usage string) *uint {
|
|
|
|
return CommandLine.Uint(names, value, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uint64Var defines a uint64 flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a uint64 variable in which to store the value of the flag.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) Uint64Var(p *uint64, names []string, value uint64, usage string) {
|
|
|
|
fs.Var(newUint64Value(value, p), names, usage)
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Uint64Var defines a uint64 flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a uint64 variable in which to store the value of the flag.
|
|
|
|
func Uint64Var(p *uint64, names []string, value uint64, usage string) {
|
|
|
|
CommandLine.Var(newUint64Value(value, p), names, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uint64 defines a uint64 flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a uint64 variable that stores the value of the flag.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) Uint64(names []string, value uint64, usage string) *uint64 {
|
2013-12-23 14:43:54 -05:00
|
|
|
p := new(uint64)
|
2015-07-21 15:53:13 -04:00
|
|
|
fs.Uint64Var(p, names, value, usage)
|
2013-12-23 14:43:54 -05:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uint64 defines a uint64 flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a uint64 variable that stores the value of the flag.
|
|
|
|
func Uint64(names []string, value uint64, usage string) *uint64 {
|
|
|
|
return CommandLine.Uint64(names, value, usage)
|
|
|
|
}
|
|
|
|
|
2015-10-06 11:21:43 -04:00
|
|
|
// Uint16Var defines a uint16 flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a uint16 variable in which to store the value of the flag.
|
|
|
|
func (fs *FlagSet) Uint16Var(p *uint16, names []string, value uint16, usage string) {
|
|
|
|
fs.Var(newUint16Value(value, p), names, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uint16Var defines a uint16 flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a uint16 variable in which to store the value of the flag.
|
|
|
|
func Uint16Var(p *uint16, names []string, value uint16, usage string) {
|
|
|
|
CommandLine.Var(newUint16Value(value, p), names, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uint16 defines a uint16 flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a uint16 variable that stores the value of the flag.
|
|
|
|
func (fs *FlagSet) Uint16(names []string, value uint16, usage string) *uint16 {
|
|
|
|
p := new(uint16)
|
|
|
|
fs.Uint16Var(p, names, value, usage)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uint16 defines a uint16 flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a uint16 variable that stores the value of the flag.
|
|
|
|
func Uint16(names []string, value uint16, usage string) *uint16 {
|
|
|
|
return CommandLine.Uint16(names, value, usage)
|
|
|
|
}
|
|
|
|
|
2013-12-23 14:43:54 -05:00
|
|
|
// StringVar defines a string flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a string variable in which to store the value of the flag.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) StringVar(p *string, names []string, value string, usage string) {
|
|
|
|
fs.Var(newStringValue(value, p), names, usage)
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// StringVar defines a string flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a string variable in which to store the value of the flag.
|
|
|
|
func StringVar(p *string, names []string, value string, usage string) {
|
|
|
|
CommandLine.Var(newStringValue(value, p), names, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// String defines a string flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a string variable that stores the value of the flag.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) String(names []string, value string, usage string) *string {
|
2013-12-23 14:43:54 -05:00
|
|
|
p := new(string)
|
2015-07-21 15:53:13 -04:00
|
|
|
fs.StringVar(p, names, value, usage)
|
2013-12-23 14:43:54 -05:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// String defines a string flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a string variable that stores the value of the flag.
|
|
|
|
func String(names []string, value string, usage string) *string {
|
|
|
|
return CommandLine.String(names, value, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Float64Var defines a float64 flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a float64 variable in which to store the value of the flag.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) Float64Var(p *float64, names []string, value float64, usage string) {
|
|
|
|
fs.Var(newFloat64Value(value, p), names, usage)
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Float64Var defines a float64 flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a float64 variable in which to store the value of the flag.
|
|
|
|
func Float64Var(p *float64, names []string, value float64, usage string) {
|
|
|
|
CommandLine.Var(newFloat64Value(value, p), names, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Float64 defines a float64 flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a float64 variable that stores the value of the flag.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) Float64(names []string, value float64, usage string) *float64 {
|
2013-12-23 14:43:54 -05:00
|
|
|
p := new(float64)
|
2015-07-21 15:53:13 -04:00
|
|
|
fs.Float64Var(p, names, value, usage)
|
2013-12-23 14:43:54 -05:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Float64 defines a float64 flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a float64 variable that stores the value of the flag.
|
|
|
|
func Float64(names []string, value float64, usage string) *float64 {
|
|
|
|
return CommandLine.Float64(names, value, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DurationVar defines a time.Duration flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a time.Duration variable in which to store the value of the flag.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) DurationVar(p *time.Duration, names []string, value time.Duration, usage string) {
|
|
|
|
fs.Var(newDurationValue(value, p), names, usage)
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// DurationVar defines a time.Duration flag with specified name, default value, and usage string.
|
|
|
|
// The argument p points to a time.Duration variable in which to store the value of the flag.
|
|
|
|
func DurationVar(p *time.Duration, names []string, value time.Duration, usage string) {
|
|
|
|
CommandLine.Var(newDurationValue(value, p), names, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Duration defines a time.Duration flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a time.Duration variable that stores the value of the flag.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) Duration(names []string, value time.Duration, usage string) *time.Duration {
|
2013-12-23 14:43:54 -05:00
|
|
|
p := new(time.Duration)
|
2015-07-21 15:53:13 -04:00
|
|
|
fs.DurationVar(p, names, value, usage)
|
2013-12-23 14:43:54 -05:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Duration defines a time.Duration flag with specified name, default value, and usage string.
|
|
|
|
// The return value is the address of a time.Duration variable that stores the value of the flag.
|
|
|
|
func Duration(names []string, value time.Duration, usage string) *time.Duration {
|
|
|
|
return CommandLine.Duration(names, value, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Var defines a flag with the specified name and usage string. The type and
|
|
|
|
// value of the flag are represented by the first argument, of type Value, which
|
|
|
|
// typically holds a user-defined implementation of Value. For instance, the
|
|
|
|
// caller could create a flag that turns a comma-separated string into a slice
|
|
|
|
// of strings by giving the slice the methods of Value; in particular, Set would
|
|
|
|
// decompose the comma-separated string into the slice.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) Var(value Value, names []string, usage string) {
|
2013-12-23 14:43:54 -05:00
|
|
|
// Remember the default value as a string; it won't change.
|
|
|
|
flag := &Flag{names, usage, value, value.String()}
|
|
|
|
for _, name := range names {
|
|
|
|
name = strings.TrimPrefix(name, "#")
|
2015-07-21 15:53:13 -04:00
|
|
|
_, alreadythere := fs.formal[name]
|
2013-12-23 14:43:54 -05:00
|
|
|
if alreadythere {
|
|
|
|
var msg string
|
2015-07-21 15:53:13 -04:00
|
|
|
if fs.name == "" {
|
2013-12-23 14:43:54 -05:00
|
|
|
msg = fmt.Sprintf("flag redefined: %s", name)
|
|
|
|
} else {
|
2015-07-21 15:53:13 -04:00
|
|
|
msg = fmt.Sprintf("%s flag redefined: %s", fs.name, name)
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
2015-07-21 15:53:13 -04:00
|
|
|
fmt.Fprintln(fs.Out(), msg)
|
2013-12-23 14:43:54 -05:00
|
|
|
panic(msg) // Happens only if flags are declared with identical names
|
|
|
|
}
|
2015-07-21 15:53:13 -04:00
|
|
|
if fs.formal == nil {
|
|
|
|
fs.formal = make(map[string]*Flag)
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
2015-07-21 15:53:13 -04:00
|
|
|
fs.formal[name] = flag
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Var defines a flag with the specified name and usage string. The type and
|
|
|
|
// value of the flag are represented by the first argument, of type Value, which
|
|
|
|
// typically holds a user-defined implementation of Value. For instance, the
|
|
|
|
// caller could create a flag that turns a comma-separated string into a slice
|
|
|
|
// of strings by giving the slice the methods of Value; in particular, Set would
|
|
|
|
// decompose the comma-separated string into the slice.
|
|
|
|
func Var(value Value, names []string, usage string) {
|
|
|
|
CommandLine.Var(value, names, usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// failf prints to standard error a formatted error and usage message and
|
|
|
|
// returns the error.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) failf(format string, a ...interface{}) error {
|
2013-12-23 14:43:54 -05:00
|
|
|
err := fmt.Errorf(format, a...)
|
2015-07-21 15:53:13 -04:00
|
|
|
fmt.Fprintln(fs.Out(), err)
|
|
|
|
if os.Args[0] == fs.name {
|
|
|
|
fmt.Fprintf(fs.Out(), "See '%s --help'.\n", os.Args[0])
|
2014-11-13 09:39:51 -05:00
|
|
|
} else {
|
2015-07-21 15:53:13 -04:00
|
|
|
fmt.Fprintf(fs.Out(), "See '%s %s --help'.\n", os.Args[0], fs.name)
|
2014-11-13 09:39:51 -05:00
|
|
|
}
|
2013-12-23 14:43:54 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// usage calls the Usage method for the flag set, or the usage function if
|
|
|
|
// the flag set is CommandLine.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) usage() {
|
|
|
|
if fs == CommandLine {
|
2013-12-23 14:43:54 -05:00
|
|
|
Usage()
|
2015-07-21 15:53:13 -04:00
|
|
|
} else if fs.Usage == nil {
|
|
|
|
defaultUsage(fs)
|
2013-12-23 14:43:54 -05:00
|
|
|
} else {
|
2015-07-21 15:53:13 -04:00
|
|
|
fs.Usage()
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-01 22:13:34 -04:00
|
|
|
func trimQuotes(str string) string {
|
2014-06-17 16:45:57 -04:00
|
|
|
if len(str) == 0 {
|
|
|
|
return str
|
|
|
|
}
|
2014-06-01 22:13:34 -04:00
|
|
|
type quote struct {
|
|
|
|
start, end byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// All valid quote types.
|
|
|
|
quotes := []quote{
|
|
|
|
// Double quotes
|
|
|
|
{
|
|
|
|
start: '"',
|
|
|
|
end: '"',
|
|
|
|
},
|
|
|
|
|
|
|
|
// Single quotes
|
|
|
|
{
|
|
|
|
start: '\'',
|
|
|
|
end: '\'',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, quote := range quotes {
|
|
|
|
// Only strip if outermost match.
|
|
|
|
if str[0] == quote.start && str[len(str)-1] == quote.end {
|
|
|
|
str = str[1 : len(str)-1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2013-12-23 14:43:54 -05:00
|
|
|
// parseOne parses one flag. It reports whether a flag was seen.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) parseOne() (bool, string, error) {
|
|
|
|
if len(fs.args) == 0 {
|
2014-02-18 19:24:32 -05:00
|
|
|
return false, "", nil
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
2015-07-21 15:53:13 -04:00
|
|
|
s := fs.args[0]
|
2013-12-23 14:43:54 -05:00
|
|
|
if len(s) == 0 || s[0] != '-' || len(s) == 1 {
|
2014-02-18 19:24:32 -05:00
|
|
|
return false, "", nil
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
if s[1] == '-' && len(s) == 2 { // "--" terminates the flags
|
2015-07-21 15:53:13 -04:00
|
|
|
fs.args = fs.args[1:]
|
2014-02-18 19:24:32 -05:00
|
|
|
return false, "", nil
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
name := s[1:]
|
|
|
|
if len(name) == 0 || name[0] == '=' {
|
2015-07-21 15:53:13 -04:00
|
|
|
return false, "", fs.failf("bad flag syntax: %s", s)
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// it's a flag. does it have an argument?
|
2015-07-21 15:53:13 -04:00
|
|
|
fs.args = fs.args[1:]
|
2015-03-25 21:40:23 -04:00
|
|
|
hasValue := false
|
2013-12-23 14:43:54 -05:00
|
|
|
value := ""
|
2014-05-16 19:40:27 -04:00
|
|
|
if i := strings.Index(name, "="); i != -1 {
|
|
|
|
value = trimQuotes(name[i+1:])
|
2015-03-25 21:40:23 -04:00
|
|
|
hasValue = true
|
2014-05-16 19:40:27 -04:00
|
|
|
name = name[:i]
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
2014-05-16 19:40:27 -04:00
|
|
|
|
2015-07-21 15:53:13 -04:00
|
|
|
m := fs.formal
|
2013-12-23 14:43:54 -05:00
|
|
|
flag, alreadythere := m[name] // BUG
|
|
|
|
if !alreadythere {
|
|
|
|
if name == "-help" || name == "help" || name == "h" { // special case for nice help message.
|
2015-07-21 15:53:13 -04:00
|
|
|
fs.usage()
|
2014-02-18 19:24:32 -05:00
|
|
|
return false, "", ErrHelp
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
2014-03-03 14:17:28 -05:00
|
|
|
if len(name) > 0 && name[0] == '-' {
|
2015-07-21 15:53:13 -04:00
|
|
|
return false, "", fs.failf("flag provided but not defined: -%s", name)
|
2014-03-03 14:17:28 -05:00
|
|
|
}
|
2014-02-18 19:24:32 -05:00
|
|
|
return false, name, ErrRetry
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
if fv, ok := flag.Value.(boolFlag); ok && fv.IsBoolFlag() { // special case: doesn't need an arg
|
2015-03-25 21:40:23 -04:00
|
|
|
if hasValue {
|
2013-12-23 14:43:54 -05:00
|
|
|
if err := fv.Set(value); err != nil {
|
2015-07-21 15:53:13 -04:00
|
|
|
return false, "", fs.failf("invalid boolean value %q for -%s: %v", value, name, err)
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fv.Set("true")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// It must have a value, which might be the next argument.
|
2015-07-21 15:53:13 -04:00
|
|
|
if !hasValue && len(fs.args) > 0 {
|
2013-12-23 14:43:54 -05:00
|
|
|
// value is the next arg
|
2015-03-25 21:40:23 -04:00
|
|
|
hasValue = true
|
2015-07-21 15:53:13 -04:00
|
|
|
value, fs.args = fs.args[0], fs.args[1:]
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
2015-03-25 21:40:23 -04:00
|
|
|
if !hasValue {
|
2015-07-21 15:53:13 -04:00
|
|
|
return false, "", fs.failf("flag needs an argument: -%s", name)
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
if err := flag.Value.Set(value); err != nil {
|
2015-07-21 15:53:13 -04:00
|
|
|
return false, "", fs.failf("invalid value %q for flag -%s: %v", value, name, err)
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
}
|
2015-07-21 15:53:13 -04:00
|
|
|
if fs.actual == nil {
|
|
|
|
fs.actual = make(map[string]*Flag)
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
2015-07-21 15:53:13 -04:00
|
|
|
fs.actual[name] = flag
|
2014-03-11 20:51:46 -04:00
|
|
|
for i, n := range flag.Names {
|
2014-03-03 14:57:05 -05:00
|
|
|
if n == fmt.Sprintf("#%s", name) {
|
2014-03-11 20:51:46 -04:00
|
|
|
replacement := ""
|
|
|
|
for j := i; j < len(flag.Names); j++ {
|
|
|
|
if flag.Names[j][0] != '#' {
|
|
|
|
replacement = flag.Names[j]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if replacement != "" {
|
2015-07-21 15:53:13 -04:00
|
|
|
fmt.Fprintf(fs.Out(), "Warning: '-%s' is deprecated, it will be replaced by '-%s' soon. See usage.\n", name, replacement)
|
2014-03-11 20:51:46 -04:00
|
|
|
} else {
|
2015-07-21 15:53:13 -04:00
|
|
|
fmt.Fprintf(fs.Out(), "Warning: '-%s' is deprecated, it will be removed soon. See usage.\n", name)
|
2014-03-11 20:51:46 -04:00
|
|
|
}
|
2014-03-03 14:57:05 -05:00
|
|
|
}
|
|
|
|
}
|
2014-02-18 19:24:32 -05:00
|
|
|
return true, "", nil
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse parses flag definitions from the argument list, which should not
|
|
|
|
// include the command name. Must be called after all flags in the FlagSet
|
|
|
|
// are defined and before flags are accessed by the program.
|
|
|
|
// The return value will be ErrHelp if -help was set but not defined.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) Parse(arguments []string) error {
|
|
|
|
fs.parsed = true
|
|
|
|
fs.args = arguments
|
2013-12-23 14:43:54 -05:00
|
|
|
for {
|
2015-07-21 15:53:13 -04:00
|
|
|
seen, name, err := fs.parseOne()
|
2013-12-23 14:43:54 -05:00
|
|
|
if seen {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2014-02-18 19:24:32 -05:00
|
|
|
if err == ErrRetry {
|
|
|
|
if len(name) > 1 {
|
|
|
|
err = nil
|
|
|
|
for _, letter := range strings.Split(name, "") {
|
2015-07-21 15:53:13 -04:00
|
|
|
fs.args = append([]string{"-" + letter}, fs.args...)
|
|
|
|
seen2, _, err2 := fs.parseOne()
|
2014-02-18 19:24:32 -05:00
|
|
|
if seen2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err2 != nil {
|
2015-07-21 15:53:13 -04:00
|
|
|
err = fs.failf("flag provided but not defined: -%s", name)
|
2014-02-18 19:24:32 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
2015-07-21 15:53:13 -04:00
|
|
|
err = fs.failf("flag provided but not defined: -%s", name)
|
2014-02-18 19:24:32 -05:00
|
|
|
}
|
|
|
|
}
|
2015-07-21 15:53:13 -04:00
|
|
|
switch fs.errorHandling {
|
2013-12-23 14:43:54 -05:00
|
|
|
case ContinueOnError:
|
|
|
|
return err
|
|
|
|
case ExitOnError:
|
2015-07-29 08:21:16 -04:00
|
|
|
os.Exit(125)
|
2013-12-23 14:43:54 -05:00
|
|
|
case PanicOnError:
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-28 21:22:46 -04:00
|
|
|
// ParseFlags is a utility function that adds a help flag if withHelp is true,
|
2015-07-21 15:53:13 -04:00
|
|
|
// calls fs.Parse(args) and prints a relevant error message if there are
|
2015-03-28 21:22:46 -04:00
|
|
|
// incorrect number of arguments. It returns error only if error handling is
|
|
|
|
// set to ContinueOnError and parsing fails. If error handling is set to
|
|
|
|
// ExitOnError, it's safe to ignore the return value.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) ParseFlags(args []string, withHelp bool) error {
|
2015-03-28 21:22:46 -04:00
|
|
|
var help *bool
|
|
|
|
if withHelp {
|
2015-07-21 15:53:13 -04:00
|
|
|
help = fs.Bool([]string{"#help", "-help"}, false, "Print usage")
|
2015-03-28 21:22:46 -04:00
|
|
|
}
|
2015-07-21 15:53:13 -04:00
|
|
|
if err := fs.Parse(args); err != nil {
|
2015-03-28 21:22:46 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if help != nil && *help {
|
2015-07-21 15:53:13 -04:00
|
|
|
fs.SetOutput(os.Stdout)
|
|
|
|
fs.Usage()
|
2015-03-28 21:22:46 -04:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
2015-07-21 15:53:13 -04:00
|
|
|
if str := fs.CheckArgs(); str != "" {
|
|
|
|
fs.SetOutput(os.Stderr)
|
|
|
|
fs.ReportError(str, withHelp)
|
|
|
|
fs.ShortUsage()
|
2015-03-31 03:11:03 -04:00
|
|
|
os.Exit(1)
|
2015-03-28 21:22:46 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-21 15:53:13 -04:00
|
|
|
// ReportError is a utility method that prints a user-friendly message
|
2015-08-07 18:24:18 -04:00
|
|
|
// containing the error that occurred during parsing and a suggestion to get help
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) ReportError(str string, withHelp bool) {
|
2015-03-28 21:22:46 -04:00
|
|
|
if withHelp {
|
2015-07-21 15:53:13 -04:00
|
|
|
if os.Args[0] == fs.Name() {
|
2015-05-23 11:57:50 -04:00
|
|
|
str += ".\nSee '" + os.Args[0] + " --help'"
|
2015-03-28 21:22:46 -04:00
|
|
|
} else {
|
2015-07-21 15:53:13 -04:00
|
|
|
str += ".\nSee '" + os.Args[0] + " " + fs.Name() + " --help'"
|
2015-03-28 21:22:46 -04:00
|
|
|
}
|
|
|
|
}
|
2015-07-21 15:53:13 -04:00
|
|
|
fmt.Fprintf(fs.Out(), "docker: %s.\n", str)
|
2015-03-28 21:22:46 -04:00
|
|
|
}
|
|
|
|
|
2015-07-21 15:53:13 -04:00
|
|
|
// Parsed reports whether fs.Parse has been called.
|
|
|
|
func (fs *FlagSet) Parsed() bool {
|
|
|
|
return fs.parsed
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse parses the command-line flags from os.Args[1:]. Must be called
|
|
|
|
// after all flags are defined and before flags are accessed by the program.
|
|
|
|
func Parse() {
|
|
|
|
// Ignore errors; CommandLine is set for ExitOnError.
|
|
|
|
CommandLine.Parse(os.Args[1:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parsed returns true if the command-line flags have been parsed.
|
|
|
|
func Parsed() bool {
|
|
|
|
return CommandLine.Parsed()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CommandLine is the default set of command-line flags, parsed from os.Args.
|
|
|
|
// The top-level functions such as BoolVar, Arg, and on are wrappers for the
|
|
|
|
// methods of CommandLine.
|
|
|
|
var CommandLine = NewFlagSet(os.Args[0], ExitOnError)
|
|
|
|
|
|
|
|
// NewFlagSet returns a new, empty flag set with the specified name and
|
|
|
|
// error handling property.
|
|
|
|
func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet {
|
|
|
|
f := &FlagSet{
|
|
|
|
name: name,
|
|
|
|
errorHandling: errorHandling,
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init sets the name and error handling property for a flag set.
|
|
|
|
// By default, the zero FlagSet uses an empty name and the
|
|
|
|
// ContinueOnError error handling policy.
|
2015-07-21 15:53:13 -04:00
|
|
|
func (fs *FlagSet) Init(name string, errorHandling ErrorHandling) {
|
|
|
|
fs.name = name
|
|
|
|
fs.errorHandling = errorHandling
|
2013-12-23 14:43:54 -05:00
|
|
|
}
|
2015-05-05 00:18:28 -04:00
|
|
|
|
|
|
|
type mergeVal struct {
|
|
|
|
Value
|
|
|
|
key string
|
|
|
|
fset *FlagSet
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v mergeVal) Set(s string) error {
|
|
|
|
return v.fset.Set(v.key, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v mergeVal) IsBoolFlag() bool {
|
|
|
|
if b, ok := v.Value.(boolFlag); ok {
|
|
|
|
return b.IsBoolFlag()
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-07-21 15:53:13 -04:00
|
|
|
// Merge is an helper function that merges n FlagSets into a single dest FlagSet
|
|
|
|
// In case of name collision between the flagsets it will apply
|
2015-12-13 11:00:39 -05:00
|
|
|
// the destination FlagSet's errorHandling behavior.
|
2015-05-05 00:18:28 -04:00
|
|
|
func Merge(dest *FlagSet, flagsets ...*FlagSet) error {
|
|
|
|
for _, fset := range flagsets {
|
|
|
|
for k, f := range fset.formal {
|
|
|
|
if _, ok := dest.formal[k]; ok {
|
|
|
|
var err error
|
|
|
|
if fset.name == "" {
|
|
|
|
err = fmt.Errorf("flag redefined: %s", k)
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("%s flag redefined: %s", fset.name, k)
|
|
|
|
}
|
|
|
|
fmt.Fprintln(fset.Out(), err.Error())
|
|
|
|
// Happens only if flags are declared with identical names
|
|
|
|
switch dest.errorHandling {
|
|
|
|
case ContinueOnError:
|
|
|
|
return err
|
|
|
|
case ExitOnError:
|
|
|
|
os.Exit(2)
|
|
|
|
case PanicOnError:
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newF := *f
|
|
|
|
newF.Value = mergeVal{f.Value, k, fset}
|
|
|
|
dest.formal[k] = &newF
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-21 15:53:13 -04:00
|
|
|
// IsEmpty reports if the FlagSet is actually empty.
|
|
|
|
func (fs *FlagSet) IsEmpty() bool {
|
|
|
|
return len(fs.actual) == 0
|
2015-05-05 00:18:28 -04:00
|
|
|
}
|