Fix golint for pkg/mflag
Signed-off-by: Félix Cantournet <felix.cantournet@cloudwatt.com>
This commit is contained in:
parent
0715aa3a13
commit
26a545e3bc
1 changed files with 230 additions and 224 deletions
454
mflag/flag.go
454
mflag/flag.go
|
@ -2,83 +2,82 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
/*
|
// Package mflag implements command-line flag parsing.
|
||||||
Package flag 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.
|
||||||
|
|
||||||
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.
|
|
||||||
*/
|
|
||||||
package mflag
|
package mflag
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -277,6 +276,7 @@ type Getter interface {
|
||||||
// ErrorHandling defines how to handle flag parsing errors.
|
// ErrorHandling defines how to handle flag parsing errors.
|
||||||
type ErrorHandling int
|
type ErrorHandling int
|
||||||
|
|
||||||
|
// ErrorHandling strategies available when a flag parsing error occurs
|
||||||
const (
|
const (
|
||||||
ContinueOnError ErrorHandling = iota
|
ContinueOnError ErrorHandling = iota
|
||||||
ExitOnError
|
ExitOnError
|
||||||
|
@ -358,28 +358,28 @@ func sortFlags(flags map[string]*Flag) []*Flag {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name returns the name of the FlagSet.
|
// Name returns the name of the FlagSet.
|
||||||
func (f *FlagSet) Name() string {
|
func (fs *FlagSet) Name() string {
|
||||||
return f.name
|
return fs.name
|
||||||
}
|
}
|
||||||
|
|
||||||
// Out returns the destination for usage and error messages.
|
// Out returns the destination for usage and error messages.
|
||||||
func (f *FlagSet) Out() io.Writer {
|
func (fs *FlagSet) Out() io.Writer {
|
||||||
if f.output == nil {
|
if fs.output == nil {
|
||||||
return os.Stderr
|
return os.Stderr
|
||||||
}
|
}
|
||||||
return f.output
|
return fs.output
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetOutput sets the destination for usage and error messages.
|
// SetOutput sets the destination for usage and error messages.
|
||||||
// If output is nil, os.Stderr is used.
|
// If output is nil, os.Stderr is used.
|
||||||
func (f *FlagSet) SetOutput(output io.Writer) {
|
func (fs *FlagSet) SetOutput(output io.Writer) {
|
||||||
f.output = output
|
fs.output = output
|
||||||
}
|
}
|
||||||
|
|
||||||
// VisitAll visits the flags in lexicographical order, calling fn for each.
|
// VisitAll visits the flags in lexicographical order, calling fn for each.
|
||||||
// It visits all flags, even those not set.
|
// It visits all flags, even those not set.
|
||||||
func (f *FlagSet) VisitAll(fn func(*Flag)) {
|
func (fs *FlagSet) VisitAll(fn func(*Flag)) {
|
||||||
for _, flag := range sortFlags(f.formal) {
|
for _, flag := range sortFlags(fs.formal) {
|
||||||
fn(flag)
|
fn(flag)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -392,8 +392,8 @@ func VisitAll(fn func(*Flag)) {
|
||||||
|
|
||||||
// Visit visits the flags in lexicographical order, calling fn for each.
|
// Visit visits the flags in lexicographical order, calling fn for each.
|
||||||
// It visits only those flags that have been set.
|
// It visits only those flags that have been set.
|
||||||
func (f *FlagSet) Visit(fn func(*Flag)) {
|
func (fs *FlagSet) Visit(fn func(*Flag)) {
|
||||||
for _, flag := range sortFlags(f.actual) {
|
for _, flag := range sortFlags(fs.actual) {
|
||||||
fn(flag)
|
fn(flag)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -405,13 +405,13 @@ func Visit(fn func(*Flag)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lookup returns the Flag structure of the named flag, returning nil if none exists.
|
// Lookup returns the Flag structure of the named flag, returning nil if none exists.
|
||||||
func (f *FlagSet) Lookup(name string) *Flag {
|
func (fs *FlagSet) Lookup(name string) *Flag {
|
||||||
return f.formal[name]
|
return fs.formal[name]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Indicates whether the specified flag was specified at all on the cmd line
|
// IsSet indicates whether the specified flag is set in the given FlagSet
|
||||||
func (f *FlagSet) IsSet(name string) bool {
|
func (fs *FlagSet) IsSet(name string) bool {
|
||||||
return f.actual[name] != nil
|
return fs.actual[name] != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lookup returns the Flag structure of the named command-line flag,
|
// Lookup returns the Flag structure of the named command-line flag,
|
||||||
|
@ -420,7 +420,7 @@ func Lookup(name string) *Flag {
|
||||||
return CommandLine.formal[name]
|
return CommandLine.formal[name]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Indicates whether the specified flag was specified at all on the cmd line
|
// IsSet indicates whether the specified flag was specified at all on the cmd line.
|
||||||
func IsSet(name string) bool {
|
func IsSet(name string) bool {
|
||||||
return CommandLine.IsSet(name)
|
return CommandLine.IsSet(name)
|
||||||
}
|
}
|
||||||
|
@ -443,15 +443,15 @@ type nArgRequirement struct {
|
||||||
// The first parameter can be Exact, Max, or Min to respectively specify the exact,
|
// The first parameter can be Exact, Max, or Min to respectively specify the exact,
|
||||||
// the maximum, or the minimal number of arguments required.
|
// the maximum, or the minimal number of arguments required.
|
||||||
// The actual check is done in FlagSet.CheckArgs().
|
// The actual check is done in FlagSet.CheckArgs().
|
||||||
func (f *FlagSet) Require(nArgRequirementType nArgRequirementType, nArg int) {
|
func (fs *FlagSet) Require(nArgRequirementType nArgRequirementType, nArg int) {
|
||||||
f.nArgRequirements = append(f.nArgRequirements, nArgRequirement{nArgRequirementType, nArg})
|
fs.nArgRequirements = append(fs.nArgRequirements, nArgRequirement{nArgRequirementType, nArg})
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckArgs uses the requirements set by FlagSet.Require() to validate
|
// CheckArgs uses the requirements set by FlagSet.Require() to validate
|
||||||
// the number of arguments. If the requirements are not met,
|
// the number of arguments. If the requirements are not met,
|
||||||
// an error message string is returned.
|
// an error message string is returned.
|
||||||
func (f *FlagSet) CheckArgs() (message string) {
|
func (fs *FlagSet) CheckArgs() (message string) {
|
||||||
for _, req := range f.nArgRequirements {
|
for _, req := range fs.nArgRequirements {
|
||||||
var arguments string
|
var arguments string
|
||||||
if req.N == 1 {
|
if req.N == 1 {
|
||||||
arguments = "1 argument"
|
arguments = "1 argument"
|
||||||
|
@ -460,20 +460,20 @@ func (f *FlagSet) CheckArgs() (message string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
str := func(kind string) string {
|
str := func(kind string) string {
|
||||||
return fmt.Sprintf("%q requires %s%s", f.name, kind, arguments)
|
return fmt.Sprintf("%q requires %s%s", fs.name, kind, arguments)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch req.Type {
|
switch req.Type {
|
||||||
case Exact:
|
case Exact:
|
||||||
if f.NArg() != req.N {
|
if fs.NArg() != req.N {
|
||||||
return str("")
|
return str("")
|
||||||
}
|
}
|
||||||
case Max:
|
case Max:
|
||||||
if f.NArg() > req.N {
|
if fs.NArg() > req.N {
|
||||||
return str("a maximum of ")
|
return str("a maximum of ")
|
||||||
}
|
}
|
||||||
case Min:
|
case Min:
|
||||||
if f.NArg() < req.N {
|
if fs.NArg() < req.N {
|
||||||
return str("a minimum of ")
|
return str("a minimum of ")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -482,18 +482,18 @@ func (f *FlagSet) CheckArgs() (message string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set sets the value of the named flag.
|
// Set sets the value of the named flag.
|
||||||
func (f *FlagSet) Set(name, value string) error {
|
func (fs *FlagSet) Set(name, value string) error {
|
||||||
flag, ok := f.formal[name]
|
flag, ok := fs.formal[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("no such flag -%v", name)
|
return fmt.Errorf("no such flag -%v", name)
|
||||||
}
|
}
|
||||||
if err := flag.Value.Set(value); err != nil {
|
if err := flag.Value.Set(value); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if f.actual == nil {
|
if fs.actual == nil {
|
||||||
f.actual = make(map[string]*Flag)
|
fs.actual = make(map[string]*Flag)
|
||||||
}
|
}
|
||||||
f.actual[name] = flag
|
fs.actual[name] = flag
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -504,8 +504,8 @@ func Set(name, value string) error {
|
||||||
|
|
||||||
// PrintDefaults prints, to standard error unless configured
|
// PrintDefaults prints, to standard error unless configured
|
||||||
// otherwise, the default values of all defined flags in the set.
|
// otherwise, the default values of all defined flags in the set.
|
||||||
func (f *FlagSet) PrintDefaults() {
|
func (fs *FlagSet) PrintDefaults() {
|
||||||
writer := tabwriter.NewWriter(f.Out(), 20, 1, 3, ' ', 0)
|
writer := tabwriter.NewWriter(fs.Out(), 20, 1, 3, ' ', 0)
|
||||||
home := homedir.Get()
|
home := homedir.Get()
|
||||||
|
|
||||||
// Don't substitute when HOME is /
|
// Don't substitute when HOME is /
|
||||||
|
@ -514,11 +514,11 @@ func (f *FlagSet) PrintDefaults() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a blank line between cmd description and list of options
|
// Add a blank line between cmd description and list of options
|
||||||
if f.FlagCount() > 0 {
|
if fs.FlagCount() > 0 {
|
||||||
fmt.Fprintln(writer, "")
|
fmt.Fprintln(writer, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
f.VisitAll(func(flag *Flag) {
|
fs.VisitAll(func(flag *Flag) {
|
||||||
format := " -%s=%s"
|
format := " -%s=%s"
|
||||||
names := []string{}
|
names := []string{}
|
||||||
for _, name := range flag.Names {
|
for _, name := range flag.Names {
|
||||||
|
@ -551,13 +551,13 @@ func PrintDefaults() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// defaultUsage is the default function to print a usage message.
|
// defaultUsage is the default function to print a usage message.
|
||||||
func defaultUsage(f *FlagSet) {
|
func defaultUsage(fs *FlagSet) {
|
||||||
if f.name == "" {
|
if fs.name == "" {
|
||||||
fmt.Fprintf(f.Out(), "Usage:\n")
|
fmt.Fprintf(fs.Out(), "Usage:\n")
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(f.Out(), "Usage of %s:\n", f.name)
|
fmt.Fprintf(fs.Out(), "Usage of %s:\n", fs.name)
|
||||||
}
|
}
|
||||||
f.PrintDefaults()
|
fs.PrintDefaults()
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: Usage is not just defaultUsage(CommandLine)
|
// NOTE: Usage is not just defaultUsage(CommandLine)
|
||||||
|
@ -578,12 +578,12 @@ var ShortUsage = func() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// FlagCount returns the number of flags that have been defined.
|
// FlagCount returns the number of flags that have been defined.
|
||||||
func (f *FlagSet) FlagCount() int { return len(sortFlags(f.formal)) }
|
func (fs *FlagSet) FlagCount() int { return len(sortFlags(fs.formal)) }
|
||||||
|
|
||||||
// FlagCountUndeprecated returns the number of undeprecated flags that have been defined.
|
// FlagCountUndeprecated returns the number of undeprecated flags that have been defined.
|
||||||
func (f *FlagSet) FlagCountUndeprecated() int {
|
func (fs *FlagSet) FlagCountUndeprecated() int {
|
||||||
count := 0
|
count := 0
|
||||||
for _, flag := range sortFlags(f.formal) {
|
for _, flag := range sortFlags(fs.formal) {
|
||||||
for _, name := range flag.Names {
|
for _, name := range flag.Names {
|
||||||
if name[0] != '#' {
|
if name[0] != '#' {
|
||||||
count++
|
count++
|
||||||
|
@ -595,18 +595,18 @@ func (f *FlagSet) FlagCountUndeprecated() int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NFlag returns the number of flags that have been set.
|
// NFlag returns the number of flags that have been set.
|
||||||
func (f *FlagSet) NFlag() int { return len(f.actual) }
|
func (fs *FlagSet) NFlag() int { return len(fs.actual) }
|
||||||
|
|
||||||
// NFlag returns the number of command-line flags that have been set.
|
// NFlag returns the number of command-line flags that have been set.
|
||||||
func NFlag() int { return len(CommandLine.actual) }
|
func NFlag() int { return len(CommandLine.actual) }
|
||||||
|
|
||||||
// Arg returns the i'th argument. Arg(0) is the first remaining argument
|
// Arg returns the i'th argument. Arg(0) is the first remaining argument
|
||||||
// after flags have been processed.
|
// after flags have been processed.
|
||||||
func (f *FlagSet) Arg(i int) string {
|
func (fs *FlagSet) Arg(i int) string {
|
||||||
if i < 0 || i >= len(f.args) {
|
if i < 0 || i >= len(fs.args) {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return f.args[i]
|
return fs.args[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Arg returns the i'th command-line argument. Arg(0) is the first remaining argument
|
// Arg returns the i'th command-line argument. Arg(0) is the first remaining argument
|
||||||
|
@ -616,21 +616,21 @@ func Arg(i int) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NArg is the number of arguments remaining after flags have been processed.
|
// NArg is the number of arguments remaining after flags have been processed.
|
||||||
func (f *FlagSet) NArg() int { return len(f.args) }
|
func (fs *FlagSet) NArg() int { return len(fs.args) }
|
||||||
|
|
||||||
// NArg is the number of arguments remaining after flags have been processed.
|
// NArg is the number of arguments remaining after flags have been processed.
|
||||||
func NArg() int { return len(CommandLine.args) }
|
func NArg() int { return len(CommandLine.args) }
|
||||||
|
|
||||||
// Args returns the non-flag arguments.
|
// Args returns the non-flag arguments.
|
||||||
func (f *FlagSet) Args() []string { return f.args }
|
func (fs *FlagSet) Args() []string { return fs.args }
|
||||||
|
|
||||||
// Args returns the non-flag command-line arguments.
|
// Args returns the non-flag command-line arguments.
|
||||||
func Args() []string { return CommandLine.args }
|
func Args() []string { return CommandLine.args }
|
||||||
|
|
||||||
// BoolVar defines a bool flag with specified name, default value, and usage string.
|
// 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.
|
// The argument p points to a bool variable in which to store the value of the flag.
|
||||||
func (f *FlagSet) BoolVar(p *bool, names []string, value bool, usage string) {
|
func (fs *FlagSet) BoolVar(p *bool, names []string, value bool, usage string) {
|
||||||
f.Var(newBoolValue(value, p), names, usage)
|
fs.Var(newBoolValue(value, p), names, usage)
|
||||||
}
|
}
|
||||||
|
|
||||||
// BoolVar defines a bool flag with specified name, default value, and usage string.
|
// BoolVar defines a bool flag with specified name, default value, and usage string.
|
||||||
|
@ -641,9 +641,9 @@ func BoolVar(p *bool, names []string, value bool, usage string) {
|
||||||
|
|
||||||
// Bool defines a bool flag with specified name, default value, and usage string.
|
// 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.
|
// The return value is the address of a bool variable that stores the value of the flag.
|
||||||
func (f *FlagSet) Bool(names []string, value bool, usage string) *bool {
|
func (fs *FlagSet) Bool(names []string, value bool, usage string) *bool {
|
||||||
p := new(bool)
|
p := new(bool)
|
||||||
f.BoolVar(p, names, value, usage)
|
fs.BoolVar(p, names, value, usage)
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -655,8 +655,8 @@ func Bool(names []string, value bool, usage string) *bool {
|
||||||
|
|
||||||
// IntVar defines an int flag with specified name, default value, and usage string.
|
// 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.
|
// The argument p points to an int variable in which to store the value of the flag.
|
||||||
func (f *FlagSet) IntVar(p *int, names []string, value int, usage string) {
|
func (fs *FlagSet) IntVar(p *int, names []string, value int, usage string) {
|
||||||
f.Var(newIntValue(value, p), names, usage)
|
fs.Var(newIntValue(value, p), names, usage)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IntVar defines an int flag with specified name, default value, and usage string.
|
// IntVar defines an int flag with specified name, default value, and usage string.
|
||||||
|
@ -667,9 +667,9 @@ func IntVar(p *int, names []string, value int, usage string) {
|
||||||
|
|
||||||
// Int defines an int flag with specified name, default value, and usage string.
|
// 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.
|
// The return value is the address of an int variable that stores the value of the flag.
|
||||||
func (f *FlagSet) Int(names []string, value int, usage string) *int {
|
func (fs *FlagSet) Int(names []string, value int, usage string) *int {
|
||||||
p := new(int)
|
p := new(int)
|
||||||
f.IntVar(p, names, value, usage)
|
fs.IntVar(p, names, value, usage)
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -681,8 +681,8 @@ func Int(names []string, value int, usage string) *int {
|
||||||
|
|
||||||
// Int64Var defines an int64 flag with specified name, default value, and usage string.
|
// 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.
|
// The argument p points to an int64 variable in which to store the value of the flag.
|
||||||
func (f *FlagSet) Int64Var(p *int64, names []string, value int64, usage string) {
|
func (fs *FlagSet) Int64Var(p *int64, names []string, value int64, usage string) {
|
||||||
f.Var(newInt64Value(value, p), names, usage)
|
fs.Var(newInt64Value(value, p), names, usage)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Int64Var defines an int64 flag with specified name, default value, and usage string.
|
// Int64Var defines an int64 flag with specified name, default value, and usage string.
|
||||||
|
@ -693,9 +693,9 @@ func Int64Var(p *int64, names []string, value int64, usage string) {
|
||||||
|
|
||||||
// Int64 defines an int64 flag with specified name, default value, and usage string.
|
// 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.
|
// The return value is the address of an int64 variable that stores the value of the flag.
|
||||||
func (f *FlagSet) Int64(names []string, value int64, usage string) *int64 {
|
func (fs *FlagSet) Int64(names []string, value int64, usage string) *int64 {
|
||||||
p := new(int64)
|
p := new(int64)
|
||||||
f.Int64Var(p, names, value, usage)
|
fs.Int64Var(p, names, value, usage)
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -707,8 +707,8 @@ func Int64(names []string, value int64, usage string) *int64 {
|
||||||
|
|
||||||
// UintVar defines a uint flag with specified name, default value, and usage string.
|
// 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.
|
// The argument p points to a uint variable in which to store the value of the flag.
|
||||||
func (f *FlagSet) UintVar(p *uint, names []string, value uint, usage string) {
|
func (fs *FlagSet) UintVar(p *uint, names []string, value uint, usage string) {
|
||||||
f.Var(newUintValue(value, p), names, usage)
|
fs.Var(newUintValue(value, p), names, usage)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UintVar defines a uint flag with specified name, default value, and usage string.
|
// UintVar defines a uint flag with specified name, default value, and usage string.
|
||||||
|
@ -719,9 +719,9 @@ func UintVar(p *uint, names []string, value uint, usage string) {
|
||||||
|
|
||||||
// Uint defines a uint flag with specified name, default value, and usage string.
|
// 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.
|
// The return value is the address of a uint variable that stores the value of the flag.
|
||||||
func (f *FlagSet) Uint(names []string, value uint, usage string) *uint {
|
func (fs *FlagSet) Uint(names []string, value uint, usage string) *uint {
|
||||||
p := new(uint)
|
p := new(uint)
|
||||||
f.UintVar(p, names, value, usage)
|
fs.UintVar(p, names, value, usage)
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -733,8 +733,8 @@ func Uint(names []string, value uint, usage string) *uint {
|
||||||
|
|
||||||
// Uint64Var defines a uint64 flag with specified name, default value, and usage string.
|
// 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.
|
// The argument p points to a uint64 variable in which to store the value of the flag.
|
||||||
func (f *FlagSet) Uint64Var(p *uint64, names []string, value uint64, usage string) {
|
func (fs *FlagSet) Uint64Var(p *uint64, names []string, value uint64, usage string) {
|
||||||
f.Var(newUint64Value(value, p), names, usage)
|
fs.Var(newUint64Value(value, p), names, usage)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Uint64Var defines a uint64 flag with specified name, default value, and usage string.
|
// Uint64Var defines a uint64 flag with specified name, default value, and usage string.
|
||||||
|
@ -745,9 +745,9 @@ func Uint64Var(p *uint64, names []string, value uint64, usage string) {
|
||||||
|
|
||||||
// Uint64 defines a uint64 flag with specified name, default value, and usage string.
|
// 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.
|
// The return value is the address of a uint64 variable that stores the value of the flag.
|
||||||
func (f *FlagSet) Uint64(names []string, value uint64, usage string) *uint64 {
|
func (fs *FlagSet) Uint64(names []string, value uint64, usage string) *uint64 {
|
||||||
p := new(uint64)
|
p := new(uint64)
|
||||||
f.Uint64Var(p, names, value, usage)
|
fs.Uint64Var(p, names, value, usage)
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -759,8 +759,8 @@ func Uint64(names []string, value uint64, usage string) *uint64 {
|
||||||
|
|
||||||
// StringVar defines a string flag with specified name, default value, and usage string.
|
// 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.
|
// The argument p points to a string variable in which to store the value of the flag.
|
||||||
func (f *FlagSet) StringVar(p *string, names []string, value string, usage string) {
|
func (fs *FlagSet) StringVar(p *string, names []string, value string, usage string) {
|
||||||
f.Var(newStringValue(value, p), names, usage)
|
fs.Var(newStringValue(value, p), names, usage)
|
||||||
}
|
}
|
||||||
|
|
||||||
// StringVar defines a string flag with specified name, default value, and usage string.
|
// StringVar defines a string flag with specified name, default value, and usage string.
|
||||||
|
@ -771,9 +771,9 @@ func StringVar(p *string, names []string, value string, usage string) {
|
||||||
|
|
||||||
// String defines a string flag with specified name, default value, and usage string.
|
// 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.
|
// The return value is the address of a string variable that stores the value of the flag.
|
||||||
func (f *FlagSet) String(names []string, value string, usage string) *string {
|
func (fs *FlagSet) String(names []string, value string, usage string) *string {
|
||||||
p := new(string)
|
p := new(string)
|
||||||
f.StringVar(p, names, value, usage)
|
fs.StringVar(p, names, value, usage)
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -785,8 +785,8 @@ func String(names []string, value string, usage string) *string {
|
||||||
|
|
||||||
// Float64Var defines a float64 flag with specified name, default value, and usage string.
|
// 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.
|
// The argument p points to a float64 variable in which to store the value of the flag.
|
||||||
func (f *FlagSet) Float64Var(p *float64, names []string, value float64, usage string) {
|
func (fs *FlagSet) Float64Var(p *float64, names []string, value float64, usage string) {
|
||||||
f.Var(newFloat64Value(value, p), names, usage)
|
fs.Var(newFloat64Value(value, p), names, usage)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Float64Var defines a float64 flag with specified name, default value, and usage string.
|
// Float64Var defines a float64 flag with specified name, default value, and usage string.
|
||||||
|
@ -797,9 +797,9 @@ func Float64Var(p *float64, names []string, value float64, usage string) {
|
||||||
|
|
||||||
// Float64 defines a float64 flag with specified name, default value, and usage string.
|
// 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.
|
// The return value is the address of a float64 variable that stores the value of the flag.
|
||||||
func (f *FlagSet) Float64(names []string, value float64, usage string) *float64 {
|
func (fs *FlagSet) Float64(names []string, value float64, usage string) *float64 {
|
||||||
p := new(float64)
|
p := new(float64)
|
||||||
f.Float64Var(p, names, value, usage)
|
fs.Float64Var(p, names, value, usage)
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -811,8 +811,8 @@ func Float64(names []string, value float64, usage string) *float64 {
|
||||||
|
|
||||||
// DurationVar defines a time.Duration flag with specified name, default value, and usage string.
|
// 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.
|
// The argument p points to a time.Duration variable in which to store the value of the flag.
|
||||||
func (f *FlagSet) DurationVar(p *time.Duration, names []string, value time.Duration, usage string) {
|
func (fs *FlagSet) DurationVar(p *time.Duration, names []string, value time.Duration, usage string) {
|
||||||
f.Var(newDurationValue(value, p), names, usage)
|
fs.Var(newDurationValue(value, p), names, usage)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DurationVar defines a time.Duration flag with specified name, default value, and usage string.
|
// DurationVar defines a time.Duration flag with specified name, default value, and usage string.
|
||||||
|
@ -823,9 +823,9 @@ func DurationVar(p *time.Duration, names []string, value time.Duration, usage st
|
||||||
|
|
||||||
// Duration defines a time.Duration flag with specified name, default value, and usage string.
|
// 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.
|
// The return value is the address of a time.Duration variable that stores the value of the flag.
|
||||||
func (f *FlagSet) Duration(names []string, value time.Duration, usage string) *time.Duration {
|
func (fs *FlagSet) Duration(names []string, value time.Duration, usage string) *time.Duration {
|
||||||
p := new(time.Duration)
|
p := new(time.Duration)
|
||||||
f.DurationVar(p, names, value, usage)
|
fs.DurationVar(p, names, value, usage)
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -841,26 +841,26 @@ func Duration(names []string, value time.Duration, usage string) *time.Duration
|
||||||
// caller could create a flag that turns a comma-separated string into a slice
|
// 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
|
// of strings by giving the slice the methods of Value; in particular, Set would
|
||||||
// decompose the comma-separated string into the slice.
|
// decompose the comma-separated string into the slice.
|
||||||
func (f *FlagSet) Var(value Value, names []string, usage string) {
|
func (fs *FlagSet) Var(value Value, names []string, usage string) {
|
||||||
// Remember the default value as a string; it won't change.
|
// Remember the default value as a string; it won't change.
|
||||||
flag := &Flag{names, usage, value, value.String()}
|
flag := &Flag{names, usage, value, value.String()}
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
name = strings.TrimPrefix(name, "#")
|
name = strings.TrimPrefix(name, "#")
|
||||||
_, alreadythere := f.formal[name]
|
_, alreadythere := fs.formal[name]
|
||||||
if alreadythere {
|
if alreadythere {
|
||||||
var msg string
|
var msg string
|
||||||
if f.name == "" {
|
if fs.name == "" {
|
||||||
msg = fmt.Sprintf("flag redefined: %s", name)
|
msg = fmt.Sprintf("flag redefined: %s", name)
|
||||||
} else {
|
} else {
|
||||||
msg = fmt.Sprintf("%s flag redefined: %s", f.name, name)
|
msg = fmt.Sprintf("%s flag redefined: %s", fs.name, name)
|
||||||
}
|
}
|
||||||
fmt.Fprintln(f.Out(), msg)
|
fmt.Fprintln(fs.Out(), msg)
|
||||||
panic(msg) // Happens only if flags are declared with identical names
|
panic(msg) // Happens only if flags are declared with identical names
|
||||||
}
|
}
|
||||||
if f.formal == nil {
|
if fs.formal == nil {
|
||||||
f.formal = make(map[string]*Flag)
|
fs.formal = make(map[string]*Flag)
|
||||||
}
|
}
|
||||||
f.formal[name] = flag
|
fs.formal[name] = flag
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -876,26 +876,26 @@ func Var(value Value, names []string, usage string) {
|
||||||
|
|
||||||
// failf prints to standard error a formatted error and usage message and
|
// failf prints to standard error a formatted error and usage message and
|
||||||
// returns the error.
|
// returns the error.
|
||||||
func (f *FlagSet) failf(format string, a ...interface{}) error {
|
func (fs *FlagSet) failf(format string, a ...interface{}) error {
|
||||||
err := fmt.Errorf(format, a...)
|
err := fmt.Errorf(format, a...)
|
||||||
fmt.Fprintln(f.Out(), err)
|
fmt.Fprintln(fs.Out(), err)
|
||||||
if os.Args[0] == f.name {
|
if os.Args[0] == fs.name {
|
||||||
fmt.Fprintf(f.Out(), "See '%s --help'.\n", os.Args[0])
|
fmt.Fprintf(fs.Out(), "See '%s --help'.\n", os.Args[0])
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(f.Out(), "See '%s %s --help'.\n", os.Args[0], f.name)
|
fmt.Fprintf(fs.Out(), "See '%s %s --help'.\n", os.Args[0], fs.name)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// usage calls the Usage method for the flag set, or the usage function if
|
// usage calls the Usage method for the flag set, or the usage function if
|
||||||
// the flag set is CommandLine.
|
// the flag set is CommandLine.
|
||||||
func (f *FlagSet) usage() {
|
func (fs *FlagSet) usage() {
|
||||||
if f == CommandLine {
|
if fs == CommandLine {
|
||||||
Usage()
|
Usage()
|
||||||
} else if f.Usage == nil {
|
} else if fs.Usage == nil {
|
||||||
defaultUsage(f)
|
defaultUsage(fs)
|
||||||
} else {
|
} else {
|
||||||
f.Usage()
|
fs.Usage()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -934,25 +934,25 @@ func trimQuotes(str string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseOne parses one flag. It reports whether a flag was seen.
|
// parseOne parses one flag. It reports whether a flag was seen.
|
||||||
func (f *FlagSet) parseOne() (bool, string, error) {
|
func (fs *FlagSet) parseOne() (bool, string, error) {
|
||||||
if len(f.args) == 0 {
|
if len(fs.args) == 0 {
|
||||||
return false, "", nil
|
return false, "", nil
|
||||||
}
|
}
|
||||||
s := f.args[0]
|
s := fs.args[0]
|
||||||
if len(s) == 0 || s[0] != '-' || len(s) == 1 {
|
if len(s) == 0 || s[0] != '-' || len(s) == 1 {
|
||||||
return false, "", nil
|
return false, "", nil
|
||||||
}
|
}
|
||||||
if s[1] == '-' && len(s) == 2 { // "--" terminates the flags
|
if s[1] == '-' && len(s) == 2 { // "--" terminates the flags
|
||||||
f.args = f.args[1:]
|
fs.args = fs.args[1:]
|
||||||
return false, "", nil
|
return false, "", nil
|
||||||
}
|
}
|
||||||
name := s[1:]
|
name := s[1:]
|
||||||
if len(name) == 0 || name[0] == '=' {
|
if len(name) == 0 || name[0] == '=' {
|
||||||
return false, "", f.failf("bad flag syntax: %s", s)
|
return false, "", fs.failf("bad flag syntax: %s", s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// it's a flag. does it have an argument?
|
// it's a flag. does it have an argument?
|
||||||
f.args = f.args[1:]
|
fs.args = fs.args[1:]
|
||||||
hasValue := false
|
hasValue := false
|
||||||
value := ""
|
value := ""
|
||||||
if i := strings.Index(name, "="); i != -1 {
|
if i := strings.Index(name, "="); i != -1 {
|
||||||
|
@ -961,44 +961,44 @@ func (f *FlagSet) parseOne() (bool, string, error) {
|
||||||
name = name[:i]
|
name = name[:i]
|
||||||
}
|
}
|
||||||
|
|
||||||
m := f.formal
|
m := fs.formal
|
||||||
flag, alreadythere := m[name] // BUG
|
flag, alreadythere := m[name] // BUG
|
||||||
if !alreadythere {
|
if !alreadythere {
|
||||||
if name == "-help" || name == "help" || name == "h" { // special case for nice help message.
|
if name == "-help" || name == "help" || name == "h" { // special case for nice help message.
|
||||||
f.usage()
|
fs.usage()
|
||||||
return false, "", ErrHelp
|
return false, "", ErrHelp
|
||||||
}
|
}
|
||||||
if len(name) > 0 && name[0] == '-' {
|
if len(name) > 0 && name[0] == '-' {
|
||||||
return false, "", f.failf("flag provided but not defined: -%s", name)
|
return false, "", fs.failf("flag provided but not defined: -%s", name)
|
||||||
}
|
}
|
||||||
return false, name, ErrRetry
|
return false, name, ErrRetry
|
||||||
}
|
}
|
||||||
if fv, ok := flag.Value.(boolFlag); ok && fv.IsBoolFlag() { // special case: doesn't need an arg
|
if fv, ok := flag.Value.(boolFlag); ok && fv.IsBoolFlag() { // special case: doesn't need an arg
|
||||||
if hasValue {
|
if hasValue {
|
||||||
if err := fv.Set(value); err != nil {
|
if err := fv.Set(value); err != nil {
|
||||||
return false, "", f.failf("invalid boolean value %q for -%s: %v", value, name, err)
|
return false, "", fs.failf("invalid boolean value %q for -%s: %v", value, name, err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fv.Set("true")
|
fv.Set("true")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// It must have a value, which might be the next argument.
|
// It must have a value, which might be the next argument.
|
||||||
if !hasValue && len(f.args) > 0 {
|
if !hasValue && len(fs.args) > 0 {
|
||||||
// value is the next arg
|
// value is the next arg
|
||||||
hasValue = true
|
hasValue = true
|
||||||
value, f.args = f.args[0], f.args[1:]
|
value, fs.args = fs.args[0], fs.args[1:]
|
||||||
}
|
}
|
||||||
if !hasValue {
|
if !hasValue {
|
||||||
return false, "", f.failf("flag needs an argument: -%s", name)
|
return false, "", fs.failf("flag needs an argument: -%s", name)
|
||||||
}
|
}
|
||||||
if err := flag.Value.Set(value); err != nil {
|
if err := flag.Value.Set(value); err != nil {
|
||||||
return false, "", f.failf("invalid value %q for flag -%s: %v", value, name, err)
|
return false, "", fs.failf("invalid value %q for flag -%s: %v", value, name, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if f.actual == nil {
|
if fs.actual == nil {
|
||||||
f.actual = make(map[string]*Flag)
|
fs.actual = make(map[string]*Flag)
|
||||||
}
|
}
|
||||||
f.actual[name] = flag
|
fs.actual[name] = flag
|
||||||
for i, n := range flag.Names {
|
for i, n := range flag.Names {
|
||||||
if n == fmt.Sprintf("#%s", name) {
|
if n == fmt.Sprintf("#%s", name) {
|
||||||
replacement := ""
|
replacement := ""
|
||||||
|
@ -1009,9 +1009,9 @@ func (f *FlagSet) parseOne() (bool, string, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if replacement != "" {
|
if replacement != "" {
|
||||||
fmt.Fprintf(f.Out(), "Warning: '-%s' is deprecated, it will be replaced by '-%s' soon. See usage.\n", name, replacement)
|
fmt.Fprintf(fs.Out(), "Warning: '-%s' is deprecated, it will be replaced by '-%s' soon. See usage.\n", name, replacement)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(f.Out(), "Warning: '-%s' is deprecated, it will be removed soon. See usage.\n", name)
|
fmt.Fprintf(fs.Out(), "Warning: '-%s' is deprecated, it will be removed soon. See usage.\n", name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1022,11 +1022,11 @@ func (f *FlagSet) parseOne() (bool, string, error) {
|
||||||
// include the command name. Must be called after all flags in the FlagSet
|
// include the command name. Must be called after all flags in the FlagSet
|
||||||
// are defined and before flags are accessed by the program.
|
// are defined and before flags are accessed by the program.
|
||||||
// The return value will be ErrHelp if -help was set but not defined.
|
// The return value will be ErrHelp if -help was set but not defined.
|
||||||
func (f *FlagSet) Parse(arguments []string) error {
|
func (fs *FlagSet) Parse(arguments []string) error {
|
||||||
f.parsed = true
|
fs.parsed = true
|
||||||
f.args = arguments
|
fs.args = arguments
|
||||||
for {
|
for {
|
||||||
seen, name, err := f.parseOne()
|
seen, name, err := fs.parseOne()
|
||||||
if seen {
|
if seen {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -1037,13 +1037,13 @@ func (f *FlagSet) Parse(arguments []string) error {
|
||||||
if len(name) > 1 {
|
if len(name) > 1 {
|
||||||
err = nil
|
err = nil
|
||||||
for _, letter := range strings.Split(name, "") {
|
for _, letter := range strings.Split(name, "") {
|
||||||
f.args = append([]string{"-" + letter}, f.args...)
|
fs.args = append([]string{"-" + letter}, fs.args...)
|
||||||
seen2, _, err2 := f.parseOne()
|
seen2, _, err2 := fs.parseOne()
|
||||||
if seen2 {
|
if seen2 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
err = f.failf("flag provided but not defined: -%s", name)
|
err = fs.failf("flag provided but not defined: -%s", name)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1051,10 +1051,10 @@ func (f *FlagSet) Parse(arguments []string) error {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
err = f.failf("flag provided but not defined: -%s", name)
|
err = fs.failf("flag provided but not defined: -%s", name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
switch f.errorHandling {
|
switch fs.errorHandling {
|
||||||
case ContinueOnError:
|
case ContinueOnError:
|
||||||
return err
|
return err
|
||||||
case ExitOnError:
|
case ExitOnError:
|
||||||
|
@ -1067,46 +1067,48 @@ func (f *FlagSet) Parse(arguments []string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseFlags is a utility function that adds a help flag if withHelp is true,
|
// ParseFlags is a utility function that adds a help flag if withHelp is true,
|
||||||
// calls cmd.Parse(args) and prints a relevant error message if there are
|
// calls fs.Parse(args) and prints a relevant error message if there are
|
||||||
// incorrect number of arguments. It returns error only if error handling is
|
// incorrect number of arguments. It returns error only if error handling is
|
||||||
// set to ContinueOnError and parsing fails. If error handling is set to
|
// set to ContinueOnError and parsing fails. If error handling is set to
|
||||||
// ExitOnError, it's safe to ignore the return value.
|
// ExitOnError, it's safe to ignore the return value.
|
||||||
func (cmd *FlagSet) ParseFlags(args []string, withHelp bool) error {
|
func (fs *FlagSet) ParseFlags(args []string, withHelp bool) error {
|
||||||
var help *bool
|
var help *bool
|
||||||
if withHelp {
|
if withHelp {
|
||||||
help = cmd.Bool([]string{"#help", "-help"}, false, "Print usage")
|
help = fs.Bool([]string{"#help", "-help"}, false, "Print usage")
|
||||||
}
|
}
|
||||||
if err := cmd.Parse(args); err != nil {
|
if err := fs.Parse(args); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if help != nil && *help {
|
if help != nil && *help {
|
||||||
cmd.SetOutput(os.Stdout)
|
fs.SetOutput(os.Stdout)
|
||||||
cmd.Usage()
|
fs.Usage()
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
if str := cmd.CheckArgs(); str != "" {
|
if str := fs.CheckArgs(); str != "" {
|
||||||
cmd.SetOutput(os.Stderr)
|
fs.SetOutput(os.Stderr)
|
||||||
cmd.ReportError(str, withHelp)
|
fs.ReportError(str, withHelp)
|
||||||
cmd.ShortUsage()
|
fs.ShortUsage()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *FlagSet) ReportError(str string, withHelp bool) {
|
// ReportError is a utility method that prints a user-friendly message
|
||||||
|
// containing the error that occured during parsing and a suggestion to get help
|
||||||
|
func (fs *FlagSet) ReportError(str string, withHelp bool) {
|
||||||
if withHelp {
|
if withHelp {
|
||||||
if os.Args[0] == cmd.Name() {
|
if os.Args[0] == fs.Name() {
|
||||||
str += ".\nSee '" + os.Args[0] + " --help'"
|
str += ".\nSee '" + os.Args[0] + " --help'"
|
||||||
} else {
|
} else {
|
||||||
str += ".\nSee '" + os.Args[0] + " " + cmd.Name() + " --help'"
|
str += ".\nSee '" + os.Args[0] + " " + fs.Name() + " --help'"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Fprintf(cmd.Out(), "docker: %s.\n", str)
|
fmt.Fprintf(fs.Out(), "docker: %s.\n", str)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parsed reports whether f.Parse has been called.
|
// Parsed reports whether fs.Parse has been called.
|
||||||
func (f *FlagSet) Parsed() bool {
|
func (fs *FlagSet) Parsed() bool {
|
||||||
return f.parsed
|
return fs.parsed
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse parses the command-line flags from os.Args[1:]. Must be called
|
// Parse parses the command-line flags from os.Args[1:]. Must be called
|
||||||
|
@ -1139,9 +1141,9 @@ func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet {
|
||||||
// Init sets the name and error handling property for a flag set.
|
// Init sets the name and error handling property for a flag set.
|
||||||
// By default, the zero FlagSet uses an empty name and the
|
// By default, the zero FlagSet uses an empty name and the
|
||||||
// ContinueOnError error handling policy.
|
// ContinueOnError error handling policy.
|
||||||
func (f *FlagSet) Init(name string, errorHandling ErrorHandling) {
|
func (fs *FlagSet) Init(name string, errorHandling ErrorHandling) {
|
||||||
f.name = name
|
fs.name = name
|
||||||
f.errorHandling = errorHandling
|
fs.errorHandling = errorHandling
|
||||||
}
|
}
|
||||||
|
|
||||||
type mergeVal struct {
|
type mergeVal struct {
|
||||||
|
@ -1161,6 +1163,9 @@ func (v mergeVal) IsBoolFlag() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
// the destination FlagSet's errorHandling behaviour.
|
||||||
func Merge(dest *FlagSet, flagsets ...*FlagSet) error {
|
func Merge(dest *FlagSet, flagsets ...*FlagSet) error {
|
||||||
for _, fset := range flagsets {
|
for _, fset := range flagsets {
|
||||||
for k, f := range fset.formal {
|
for k, f := range fset.formal {
|
||||||
|
@ -1190,6 +1195,7 @@ func Merge(dest *FlagSet, flagsets ...*FlagSet) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FlagSet) IsEmpty() bool {
|
// IsEmpty reports if the FlagSet is actually empty.
|
||||||
return len(f.actual) == 0
|
func (fs *FlagSet) IsEmpty() bool {
|
||||||
|
return len(fs.actual) == 0
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue