2020-01-04 12:45:36 +08:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BoolFlag is a flag with type bool
|
|
|
|
type BoolFlag struct {
|
|
|
|
Name string
|
2020-01-05 01:44:25 +08:00
|
|
|
Aliases []string
|
2020-01-04 12:45:36 +08:00
|
|
|
Usage string
|
2020-01-05 01:44:25 +08:00
|
|
|
EnvVars []string
|
2020-01-04 12:45:36 +08:00
|
|
|
FilePath string
|
|
|
|
Required bool
|
|
|
|
Hidden bool
|
2020-01-05 01:44:25 +08:00
|
|
|
Value bool
|
|
|
|
DefaultText string
|
2020-01-04 12:45:36 +08:00
|
|
|
Destination *bool
|
2020-01-05 01:44:25 +08:00
|
|
|
HasBeenSet bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsSet returns whether or not the flag has been set through env or file
|
|
|
|
func (f *BoolFlag) IsSet() bool {
|
|
|
|
return f.HasBeenSet
|
2020-01-04 12:45:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a readable representation of this value
|
|
|
|
// (for usage defaults)
|
2020-01-05 01:44:25 +08:00
|
|
|
func (f *BoolFlag) String() string {
|
2020-01-04 12:45:36 +08:00
|
|
|
return FlagStringer(f)
|
|
|
|
}
|
|
|
|
|
2020-01-05 01:44:25 +08:00
|
|
|
// Names returns the names of the flag
|
|
|
|
func (f *BoolFlag) Names() []string {
|
2020-04-30 18:54:11 +08:00
|
|
|
return flagNames(f.Name, f.Aliases)
|
2020-01-04 12:45:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsRequired returns whether or not the flag is required
|
2020-01-05 01:44:25 +08:00
|
|
|
func (f *BoolFlag) IsRequired() bool {
|
2020-01-04 12:45:36 +08:00
|
|
|
return f.Required
|
|
|
|
}
|
|
|
|
|
|
|
|
// TakesValue returns true of the flag takes a value, otherwise false
|
2020-01-05 01:44:25 +08:00
|
|
|
func (f *BoolFlag) TakesValue() bool {
|
2020-01-04 12:45:36 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUsage returns the usage string for the flag
|
2020-01-05 01:44:25 +08:00
|
|
|
func (f *BoolFlag) GetUsage() string {
|
2020-01-04 12:45:36 +08:00
|
|
|
return f.Usage
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetValue returns the flags value as string representation and an empty
|
|
|
|
// string if the flag takes no value at all.
|
2020-01-05 01:44:25 +08:00
|
|
|
func (f *BoolFlag) GetValue() string {
|
2020-01-04 12:45:36 +08:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply populates the flag given the flag set and environment
|
2020-01-05 01:44:25 +08:00
|
|
|
func (f *BoolFlag) Apply(set *flag.FlagSet) error {
|
|
|
|
if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {
|
|
|
|
if val != "" {
|
|
|
|
valBool, err := strconv.ParseBool(val)
|
2020-01-04 12:45:36 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2020-01-05 01:44:25 +08:00
|
|
|
return fmt.Errorf("could not parse %q as bool value for flag %s: %s", val, f.Name, err)
|
2020-01-04 12:45:36 +08:00
|
|
|
}
|
2020-01-05 01:44:25 +08:00
|
|
|
|
|
|
|
f.Value = valBool
|
|
|
|
f.HasBeenSet = true
|
2020-01-04 12:45:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 01:44:25 +08:00
|
|
|
for _, name := range f.Names() {
|
2020-01-04 12:45:36 +08:00
|
|
|
if f.Destination != nil {
|
2020-01-05 01:44:25 +08:00
|
|
|
set.BoolVar(f.Destination, name, f.Value, f.Usage)
|
|
|
|
continue
|
2020-01-04 12:45:36 +08:00
|
|
|
}
|
2020-01-05 01:44:25 +08:00
|
|
|
set.Bool(name, f.Value, f.Usage)
|
|
|
|
}
|
2020-01-04 12:45:36 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-05 01:44:25 +08:00
|
|
|
// Bool looks up the value of a local BoolFlag, returns
|
|
|
|
// false if not found
|
|
|
|
func (c *Context) Bool(name string) bool {
|
|
|
|
if fs := lookupFlagSet(name, c); fs != nil {
|
|
|
|
return lookupBool(name, fs)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-01-04 12:45:36 +08:00
|
|
|
func lookupBool(name string, set *flag.FlagSet) bool {
|
|
|
|
f := set.Lookup(name)
|
|
|
|
if f != nil {
|
|
|
|
parsed, err := strconv.ParseBool(f.Value.String())
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return parsed
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|