|
|
@ -3,24 +3,24 @@ package cli |
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"flag" |
|
|
|
"flag" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"os" |
|
|
|
|
|
|
|
"reflect" |
|
|
|
"reflect" |
|
|
|
"runtime" |
|
|
|
"runtime" |
|
|
|
"strconv" |
|
|
|
"strconv" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
|
|
|
|
"syscall" |
|
|
|
"time" |
|
|
|
"time" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
const defaultPlaceholder = "value" |
|
|
|
const defaultPlaceholder = "value" |
|
|
|
|
|
|
|
|
|
|
|
// BashCompletionFlag enables bash-completion for all commands and subcommands
|
|
|
|
// BashCompletionFlag enables bash-completion for all commands and subcommands
|
|
|
|
var BashCompletionFlag = BoolFlag{ |
|
|
|
var BashCompletionFlag Flag = BoolFlag{ |
|
|
|
Name: "generate-bash-completion", |
|
|
|
Name: "generate-bash-completion", |
|
|
|
Hidden: true, |
|
|
|
Hidden: true, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// VersionFlag prints the version for the application
|
|
|
|
// VersionFlag prints the version for the application
|
|
|
|
var VersionFlag = BoolFlag{ |
|
|
|
var VersionFlag Flag = BoolFlag{ |
|
|
|
Name: "version, v", |
|
|
|
Name: "version, v", |
|
|
|
Usage: "print the version", |
|
|
|
Usage: "print the version", |
|
|
|
} |
|
|
|
} |
|
|
@ -28,7 +28,7 @@ var VersionFlag = BoolFlag{ |
|
|
|
// HelpFlag prints the help for all commands and subcommands
|
|
|
|
// HelpFlag prints the help for all commands and subcommands
|
|
|
|
// Set to the zero value (BoolFlag{}) to disable flag -- keeps subcommand
|
|
|
|
// Set to the zero value (BoolFlag{}) to disable flag -- keeps subcommand
|
|
|
|
// unless HideHelp is set to true)
|
|
|
|
// unless HideHelp is set to true)
|
|
|
|
var HelpFlag = BoolFlag{ |
|
|
|
var HelpFlag Flag = BoolFlag{ |
|
|
|
Name: "help, h", |
|
|
|
Name: "help, h", |
|
|
|
Usage: "show help", |
|
|
|
Usage: "show help", |
|
|
|
} |
|
|
|
} |
|
|
@ -62,13 +62,29 @@ type Flag interface { |
|
|
|
GetName() string |
|
|
|
GetName() string |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func flagSet(name string, flags []Flag) *flag.FlagSet { |
|
|
|
// errorableFlag is an interface that allows us to return errors during apply
|
|
|
|
|
|
|
|
// it allows flags defined in this library to return errors in a fashion backwards compatible
|
|
|
|
|
|
|
|
// TODO remove in v2 and modify the existing Flag interface to return errors
|
|
|
|
|
|
|
|
type errorableFlag interface { |
|
|
|
|
|
|
|
Flag |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ApplyWithError(*flag.FlagSet) error |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func flagSet(name string, flags []Flag) (*flag.FlagSet, error) { |
|
|
|
set := flag.NewFlagSet(name, flag.ContinueOnError) |
|
|
|
set := flag.NewFlagSet(name, flag.ContinueOnError) |
|
|
|
|
|
|
|
|
|
|
|
for _, f := range flags { |
|
|
|
for _, f := range flags { |
|
|
|
|
|
|
|
//TODO remove in v2 when errorableFlag is removed
|
|
|
|
|
|
|
|
if ef, ok := f.(errorableFlag); ok { |
|
|
|
|
|
|
|
if err := ef.ApplyWithError(set); err != nil { |
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
f.Apply(set) |
|
|
|
f.Apply(set) |
|
|
|
} |
|
|
|
} |
|
|
|
return set |
|
|
|
} |
|
|
|
|
|
|
|
return set, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func eachName(longName string, fn func(string)) { |
|
|
|
func eachName(longName string, fn func(string)) { |
|
|
@ -87,13 +103,22 @@ type Generic interface { |
|
|
|
|
|
|
|
|
|
|
|
// Apply takes the flagset and calls Set on the generic flag with the value
|
|
|
|
// Apply takes the flagset and calls Set on the generic flag with the value
|
|
|
|
// provided by the user for parsing by the flag
|
|
|
|
// provided by the user for parsing by the flag
|
|
|
|
|
|
|
|
// Ignores parsing errors
|
|
|
|
func (f GenericFlag) Apply(set *flag.FlagSet) { |
|
|
|
func (f GenericFlag) Apply(set *flag.FlagSet) { |
|
|
|
|
|
|
|
f.ApplyWithError(set) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ApplyWithError takes the flagset and calls Set on the generic flag with the value
|
|
|
|
|
|
|
|
// provided by the user for parsing by the flag
|
|
|
|
|
|
|
|
func (f GenericFlag) ApplyWithError(set *flag.FlagSet) error { |
|
|
|
val := f.Value |
|
|
|
val := f.Value |
|
|
|
if f.EnvVar != "" { |
|
|
|
if f.EnvVar != "" { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
if envVal := os.Getenv(envVar); envVal != "" { |
|
|
|
if envVal, ok := syscall.Getenv(envVar); ok { |
|
|
|
val.Set(envVal) |
|
|
|
if err := val.Set(envVal); err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("could not parse %s as value for flag %s: %s", envVal, f.Name, err) |
|
|
|
|
|
|
|
} |
|
|
|
break |
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -102,9 +127,11 @@ func (f GenericFlag) Apply(set *flag.FlagSet) { |
|
|
|
eachName(f.Name, func(name string) { |
|
|
|
eachName(f.Name, func(name string) { |
|
|
|
set.Var(f.Value, name, f.Usage) |
|
|
|
set.Var(f.Value, name, f.Usage) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// StringSlice is an opaque type for []string to satisfy flag.Value
|
|
|
|
// StringSlice is an opaque type for []string to satisfy flag.Value and flag.Getter
|
|
|
|
type StringSlice []string |
|
|
|
type StringSlice []string |
|
|
|
|
|
|
|
|
|
|
|
// Set appends the string value to the list of values
|
|
|
|
// Set appends the string value to the list of values
|
|
|
@ -123,16 +150,29 @@ func (f *StringSlice) Value() []string { |
|
|
|
return *f |
|
|
|
return *f |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Get returns the slice of strings set by this flag
|
|
|
|
|
|
|
|
func (f *StringSlice) Get() interface{} { |
|
|
|
|
|
|
|
return *f |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
// Ignores errors
|
|
|
|
func (f StringSliceFlag) Apply(set *flag.FlagSet) { |
|
|
|
func (f StringSliceFlag) Apply(set *flag.FlagSet) { |
|
|
|
|
|
|
|
f.ApplyWithError(set) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
func (f StringSliceFlag) ApplyWithError(set *flag.FlagSet) error { |
|
|
|
if f.EnvVar != "" { |
|
|
|
if f.EnvVar != "" { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
if envVal := os.Getenv(envVar); envVal != "" { |
|
|
|
if envVal, ok := syscall.Getenv(envVar); ok { |
|
|
|
newVal := &StringSlice{} |
|
|
|
newVal := &StringSlice{} |
|
|
|
for _, s := range strings.Split(envVal, ",") { |
|
|
|
for _, s := range strings.Split(envVal, ",") { |
|
|
|
s = strings.TrimSpace(s) |
|
|
|
s = strings.TrimSpace(s) |
|
|
|
newVal.Set(s) |
|
|
|
if err := newVal.Set(s); err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("could not parse %s as string value for flag %s: %s", envVal, f.Name, err) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
f.Value = newVal |
|
|
|
f.Value = newVal |
|
|
|
break |
|
|
|
break |
|
|
@ -146,9 +186,11 @@ func (f StringSliceFlag) Apply(set *flag.FlagSet) { |
|
|
|
} |
|
|
|
} |
|
|
|
set.Var(f.Value, name, f.Usage) |
|
|
|
set.Var(f.Value, name, f.Usage) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// IntSlice is an opaque type for []int to satisfy flag.Value
|
|
|
|
// IntSlice is an opaque type for []int to satisfy flag.Value and flag.Getter
|
|
|
|
type IntSlice []int |
|
|
|
type IntSlice []int |
|
|
|
|
|
|
|
|
|
|
|
// Set parses the value into an integer and appends it to the list of values
|
|
|
|
// Set parses the value into an integer and appends it to the list of values
|
|
|
@ -171,18 +213,28 @@ func (f *IntSlice) Value() []int { |
|
|
|
return *f |
|
|
|
return *f |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Get returns the slice of ints set by this flag
|
|
|
|
|
|
|
|
func (f *IntSlice) Get() interface{} { |
|
|
|
|
|
|
|
return *f |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
// Ignores errors
|
|
|
|
func (f IntSliceFlag) Apply(set *flag.FlagSet) { |
|
|
|
func (f IntSliceFlag) Apply(set *flag.FlagSet) { |
|
|
|
|
|
|
|
f.ApplyWithError(set) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
func (f IntSliceFlag) ApplyWithError(set *flag.FlagSet) error { |
|
|
|
if f.EnvVar != "" { |
|
|
|
if f.EnvVar != "" { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
if envVal := os.Getenv(envVar); envVal != "" { |
|
|
|
if envVal, ok := syscall.Getenv(envVar); ok { |
|
|
|
newVal := &IntSlice{} |
|
|
|
newVal := &IntSlice{} |
|
|
|
for _, s := range strings.Split(envVal, ",") { |
|
|
|
for _, s := range strings.Split(envVal, ",") { |
|
|
|
s = strings.TrimSpace(s) |
|
|
|
s = strings.TrimSpace(s) |
|
|
|
err := newVal.Set(s) |
|
|
|
if err := newVal.Set(s); err != nil { |
|
|
|
if err != nil { |
|
|
|
return fmt.Errorf("could not parse %s as int slice value for flag %s: %s", envVal, f.Name, err) |
|
|
|
fmt.Fprintf(ErrWriter, err.Error()) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
f.Value = newVal |
|
|
|
f.Value = newVal |
|
|
@ -197,9 +249,11 @@ func (f IntSliceFlag) Apply(set *flag.FlagSet) { |
|
|
|
} |
|
|
|
} |
|
|
|
set.Var(f.Value, name, f.Usage) |
|
|
|
set.Var(f.Value, name, f.Usage) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Int64Slice is an opaque type for []int to satisfy flag.Value
|
|
|
|
// Int64Slice is an opaque type for []int to satisfy flag.Value and flag.Getter
|
|
|
|
type Int64Slice []int64 |
|
|
|
type Int64Slice []int64 |
|
|
|
|
|
|
|
|
|
|
|
// Set parses the value into an integer and appends it to the list of values
|
|
|
|
// Set parses the value into an integer and appends it to the list of values
|
|
|
@ -222,18 +276,28 @@ func (f *Int64Slice) Value() []int64 { |
|
|
|
return *f |
|
|
|
return *f |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Get returns the slice of ints set by this flag
|
|
|
|
|
|
|
|
func (f *Int64Slice) Get() interface{} { |
|
|
|
|
|
|
|
return *f |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
// Ignores errors
|
|
|
|
func (f Int64SliceFlag) Apply(set *flag.FlagSet) { |
|
|
|
func (f Int64SliceFlag) Apply(set *flag.FlagSet) { |
|
|
|
|
|
|
|
f.ApplyWithError(set) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
func (f Int64SliceFlag) ApplyWithError(set *flag.FlagSet) error { |
|
|
|
if f.EnvVar != "" { |
|
|
|
if f.EnvVar != "" { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
if envVal := os.Getenv(envVar); envVal != "" { |
|
|
|
if envVal, ok := syscall.Getenv(envVar); ok { |
|
|
|
newVal := &Int64Slice{} |
|
|
|
newVal := &Int64Slice{} |
|
|
|
for _, s := range strings.Split(envVal, ",") { |
|
|
|
for _, s := range strings.Split(envVal, ",") { |
|
|
|
s = strings.TrimSpace(s) |
|
|
|
s = strings.TrimSpace(s) |
|
|
|
err := newVal.Set(s) |
|
|
|
if err := newVal.Set(s); err != nil { |
|
|
|
if err != nil { |
|
|
|
return fmt.Errorf("could not parse %s as int64 slice value for flag %s: %s", envVal, f.Name, err) |
|
|
|
fmt.Fprintf(ErrWriter, err.Error()) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
f.Value = newVal |
|
|
|
f.Value = newVal |
|
|
@ -248,19 +312,33 @@ func (f Int64SliceFlag) Apply(set *flag.FlagSet) { |
|
|
|
} |
|
|
|
} |
|
|
|
set.Var(f.Value, name, f.Usage) |
|
|
|
set.Var(f.Value, name, f.Usage) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
// Ignores errors
|
|
|
|
func (f BoolFlag) Apply(set *flag.FlagSet) { |
|
|
|
func (f BoolFlag) Apply(set *flag.FlagSet) { |
|
|
|
|
|
|
|
f.ApplyWithError(set) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
func (f BoolFlag) ApplyWithError(set *flag.FlagSet) error { |
|
|
|
val := false |
|
|
|
val := false |
|
|
|
if f.EnvVar != "" { |
|
|
|
if f.EnvVar != "" { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
if envVal := os.Getenv(envVar); envVal != "" { |
|
|
|
if envVal, ok := syscall.Getenv(envVar); ok { |
|
|
|
|
|
|
|
if envVal == "" { |
|
|
|
|
|
|
|
val = false |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
envValBool, err := strconv.ParseBool(envVal) |
|
|
|
envValBool, err := strconv.ParseBool(envVal) |
|
|
|
if err == nil { |
|
|
|
if err != nil { |
|
|
|
val = envValBool |
|
|
|
return fmt.Errorf("could not parse %s as bool value for flag %s: %s", envVal, f.Name, err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val = envValBool |
|
|
|
break |
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -273,23 +351,38 @@ func (f BoolFlag) Apply(set *flag.FlagSet) { |
|
|
|
} |
|
|
|
} |
|
|
|
set.Bool(name, val, f.Usage) |
|
|
|
set.Bool(name, val, f.Usage) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
// Ignores errors
|
|
|
|
func (f BoolTFlag) Apply(set *flag.FlagSet) { |
|
|
|
func (f BoolTFlag) Apply(set *flag.FlagSet) { |
|
|
|
|
|
|
|
f.ApplyWithError(set) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
func (f BoolTFlag) ApplyWithError(set *flag.FlagSet) error { |
|
|
|
val := true |
|
|
|
val := true |
|
|
|
if f.EnvVar != "" { |
|
|
|
if f.EnvVar != "" { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
if envVal := os.Getenv(envVar); envVal != "" { |
|
|
|
if envVal, ok := syscall.Getenv(envVar); ok { |
|
|
|
|
|
|
|
if envVal == "" { |
|
|
|
|
|
|
|
val = false |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
envValBool, err := strconv.ParseBool(envVal) |
|
|
|
envValBool, err := strconv.ParseBool(envVal) |
|
|
|
if err == nil { |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("could not parse %s as bool value for flag %s: %s", envVal, f.Name, err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
val = envValBool |
|
|
|
val = envValBool |
|
|
|
break |
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
eachName(f.Name, func(name string) { |
|
|
|
eachName(f.Name, func(name string) { |
|
|
|
if f.Destination != nil { |
|
|
|
if f.Destination != nil { |
|
|
@ -298,14 +391,22 @@ func (f BoolTFlag) Apply(set *flag.FlagSet) { |
|
|
|
} |
|
|
|
} |
|
|
|
set.Bool(name, val, f.Usage) |
|
|
|
set.Bool(name, val, f.Usage) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
// Ignores errors
|
|
|
|
func (f StringFlag) Apply(set *flag.FlagSet) { |
|
|
|
func (f StringFlag) Apply(set *flag.FlagSet) { |
|
|
|
|
|
|
|
f.ApplyWithError(set) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
func (f StringFlag) ApplyWithError(set *flag.FlagSet) error { |
|
|
|
if f.EnvVar != "" { |
|
|
|
if f.EnvVar != "" { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
if envVal := os.Getenv(envVar); envVal != "" { |
|
|
|
if envVal, ok := syscall.Getenv(envVar); ok { |
|
|
|
f.Value = envVal |
|
|
|
f.Value = envVal |
|
|
|
break |
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
@ -319,22 +420,31 @@ func (f StringFlag) Apply(set *flag.FlagSet) { |
|
|
|
} |
|
|
|
} |
|
|
|
set.String(name, f.Value, f.Usage) |
|
|
|
set.String(name, f.Value, f.Usage) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
// Ignores errors
|
|
|
|
func (f IntFlag) Apply(set *flag.FlagSet) { |
|
|
|
func (f IntFlag) Apply(set *flag.FlagSet) { |
|
|
|
|
|
|
|
f.ApplyWithError(set) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
func (f IntFlag) ApplyWithError(set *flag.FlagSet) error { |
|
|
|
if f.EnvVar != "" { |
|
|
|
if f.EnvVar != "" { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
if envVal := os.Getenv(envVar); envVal != "" { |
|
|
|
if envVal, ok := syscall.Getenv(envVar); ok { |
|
|
|
envValInt, err := strconv.ParseInt(envVal, 0, 64) |
|
|
|
envValInt, err := strconv.ParseInt(envVal, 0, 64) |
|
|
|
if err == nil { |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("could not parse %s as int value for flag %s: %s", envVal, f.Name, err) |
|
|
|
|
|
|
|
} |
|
|
|
f.Value = int(envValInt) |
|
|
|
f.Value = int(envValInt) |
|
|
|
break |
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
eachName(f.Name, func(name string) { |
|
|
|
eachName(f.Name, func(name string) { |
|
|
|
if f.Destination != nil { |
|
|
|
if f.Destination != nil { |
|
|
@ -343,22 +453,32 @@ func (f IntFlag) Apply(set *flag.FlagSet) { |
|
|
|
} |
|
|
|
} |
|
|
|
set.Int(name, f.Value, f.Usage) |
|
|
|
set.Int(name, f.Value, f.Usage) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
// Ignores errors
|
|
|
|
func (f Int64Flag) Apply(set *flag.FlagSet) { |
|
|
|
func (f Int64Flag) Apply(set *flag.FlagSet) { |
|
|
|
|
|
|
|
f.ApplyWithError(set) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
func (f Int64Flag) ApplyWithError(set *flag.FlagSet) error { |
|
|
|
if f.EnvVar != "" { |
|
|
|
if f.EnvVar != "" { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
if envVal := os.Getenv(envVar); envVal != "" { |
|
|
|
if envVal, ok := syscall.Getenv(envVar); ok { |
|
|
|
envValInt, err := strconv.ParseInt(envVal, 0, 64) |
|
|
|
envValInt, err := strconv.ParseInt(envVal, 0, 64) |
|
|
|
if err == nil { |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("could not parse %s as int value for flag %s: %s", envVal, f.Name, err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
f.Value = envValInt |
|
|
|
f.Value = envValInt |
|
|
|
break |
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
eachName(f.Name, func(name string) { |
|
|
|
eachName(f.Name, func(name string) { |
|
|
|
if f.Destination != nil { |
|
|
|
if f.Destination != nil { |
|
|
@ -367,22 +487,32 @@ func (f Int64Flag) Apply(set *flag.FlagSet) { |
|
|
|
} |
|
|
|
} |
|
|
|
set.Int64(name, f.Value, f.Usage) |
|
|
|
set.Int64(name, f.Value, f.Usage) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
// Ignores errors
|
|
|
|
func (f UintFlag) Apply(set *flag.FlagSet) { |
|
|
|
func (f UintFlag) Apply(set *flag.FlagSet) { |
|
|
|
|
|
|
|
f.ApplyWithError(set) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
func (f UintFlag) ApplyWithError(set *flag.FlagSet) error { |
|
|
|
if f.EnvVar != "" { |
|
|
|
if f.EnvVar != "" { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
if envVal := os.Getenv(envVar); envVal != "" { |
|
|
|
if envVal, ok := syscall.Getenv(envVar); ok { |
|
|
|
envValInt, err := strconv.ParseUint(envVal, 0, 64) |
|
|
|
envValInt, err := strconv.ParseUint(envVal, 0, 64) |
|
|
|
if err == nil { |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("could not parse %s as uint value for flag %s: %s", envVal, f.Name, err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
f.Value = uint(envValInt) |
|
|
|
f.Value = uint(envValInt) |
|
|
|
break |
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
eachName(f.Name, func(name string) { |
|
|
|
eachName(f.Name, func(name string) { |
|
|
|
if f.Destination != nil { |
|
|
|
if f.Destination != nil { |
|
|
@ -391,22 +521,32 @@ func (f UintFlag) Apply(set *flag.FlagSet) { |
|
|
|
} |
|
|
|
} |
|
|
|
set.Uint(name, f.Value, f.Usage) |
|
|
|
set.Uint(name, f.Value, f.Usage) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
// Ignores errors
|
|
|
|
func (f Uint64Flag) Apply(set *flag.FlagSet) { |
|
|
|
func (f Uint64Flag) Apply(set *flag.FlagSet) { |
|
|
|
|
|
|
|
f.ApplyWithError(set) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
func (f Uint64Flag) ApplyWithError(set *flag.FlagSet) error { |
|
|
|
if f.EnvVar != "" { |
|
|
|
if f.EnvVar != "" { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
if envVal := os.Getenv(envVar); envVal != "" { |
|
|
|
if envVal, ok := syscall.Getenv(envVar); ok { |
|
|
|
envValInt, err := strconv.ParseUint(envVal, 0, 64) |
|
|
|
envValInt, err := strconv.ParseUint(envVal, 0, 64) |
|
|
|
if err == nil { |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("could not parse %s as uint64 value for flag %s: %s", envVal, f.Name, err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
f.Value = uint64(envValInt) |
|
|
|
f.Value = uint64(envValInt) |
|
|
|
break |
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
eachName(f.Name, func(name string) { |
|
|
|
eachName(f.Name, func(name string) { |
|
|
|
if f.Destination != nil { |
|
|
|
if f.Destination != nil { |
|
|
@ -415,22 +555,32 @@ func (f Uint64Flag) Apply(set *flag.FlagSet) { |
|
|
|
} |
|
|
|
} |
|
|
|
set.Uint64(name, f.Value, f.Usage) |
|
|
|
set.Uint64(name, f.Value, f.Usage) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
// Ignores errors
|
|
|
|
func (f DurationFlag) Apply(set *flag.FlagSet) { |
|
|
|
func (f DurationFlag) Apply(set *flag.FlagSet) { |
|
|
|
|
|
|
|
f.ApplyWithError(set) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
func (f DurationFlag) ApplyWithError(set *flag.FlagSet) error { |
|
|
|
if f.EnvVar != "" { |
|
|
|
if f.EnvVar != "" { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
if envVal := os.Getenv(envVar); envVal != "" { |
|
|
|
if envVal, ok := syscall.Getenv(envVar); ok { |
|
|
|
envValDuration, err := time.ParseDuration(envVal) |
|
|
|
envValDuration, err := time.ParseDuration(envVal) |
|
|
|
if err == nil { |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("could not parse %s as duration for flag %s: %s", envVal, f.Name, err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
f.Value = envValDuration |
|
|
|
f.Value = envValDuration |
|
|
|
break |
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
eachName(f.Name, func(name string) { |
|
|
|
eachName(f.Name, func(name string) { |
|
|
|
if f.Destination != nil { |
|
|
|
if f.Destination != nil { |
|
|
@ -439,18 +589,29 @@ func (f DurationFlag) Apply(set *flag.FlagSet) { |
|
|
|
} |
|
|
|
} |
|
|
|
set.Duration(name, f.Value, f.Usage) |
|
|
|
set.Duration(name, f.Value, f.Usage) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
// Ignores errors
|
|
|
|
func (f Float64Flag) Apply(set *flag.FlagSet) { |
|
|
|
func (f Float64Flag) Apply(set *flag.FlagSet) { |
|
|
|
|
|
|
|
f.ApplyWithError(set) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ApplyWithError populates the flag given the flag set and environment
|
|
|
|
|
|
|
|
func (f Float64Flag) ApplyWithError(set *flag.FlagSet) error { |
|
|
|
if f.EnvVar != "" { |
|
|
|
if f.EnvVar != "" { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") { |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
envVar = strings.TrimSpace(envVar) |
|
|
|
if envVal := os.Getenv(envVar); envVal != "" { |
|
|
|
if envVal, ok := syscall.Getenv(envVar); ok { |
|
|
|
envValFloat, err := strconv.ParseFloat(envVal, 10) |
|
|
|
envValFloat, err := strconv.ParseFloat(envVal, 10) |
|
|
|
if err == nil { |
|
|
|
if err != nil { |
|
|
|
f.Value = float64(envValFloat) |
|
|
|
return fmt.Errorf("could not parse %s as float64 value for flag %s: %s", envVal, f.Name, err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
f.Value = float64(envValFloat) |
|
|
|
|
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -462,12 +623,15 @@ func (f Float64Flag) Apply(set *flag.FlagSet) { |
|
|
|
} |
|
|
|
} |
|
|
|
set.Float64(name, f.Value, f.Usage) |
|
|
|
set.Float64(name, f.Value, f.Usage) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func visibleFlags(fl []Flag) []Flag { |
|
|
|
func visibleFlags(fl []Flag) []Flag { |
|
|
|
visible := []Flag{} |
|
|
|
visible := []Flag{} |
|
|
|
for _, flag := range fl { |
|
|
|
for _, flag := range fl { |
|
|
|
if !flagValue(flag).FieldByName("Hidden").Bool() { |
|
|
|
field := flagValue(flag).FieldByName("Hidden") |
|
|
|
|
|
|
|
if !field.IsValid() || !field.Bool() { |
|
|
|
visible = append(visible, flag) |
|
|
|
visible = append(visible, flag) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -560,9 +724,8 @@ func stringifyFlag(f Flag) string { |
|
|
|
|
|
|
|
|
|
|
|
needsPlaceholder := false |
|
|
|
needsPlaceholder := false |
|
|
|
defaultValueString := "" |
|
|
|
defaultValueString := "" |
|
|
|
val := fv.FieldByName("Value") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if val.IsValid() { |
|
|
|
if val := fv.FieldByName("Value"); val.IsValid() { |
|
|
|
needsPlaceholder = true |
|
|
|
needsPlaceholder = true |
|
|
|
defaultValueString = fmt.Sprintf(" (default: %v)", val.Interface()) |
|
|
|
defaultValueString = fmt.Sprintf(" (default: %v)", val.Interface()) |
|
|
|
|
|
|
|
|
|
|
|