api/vendor/github.com/urfave/cli/v2/flag_bool.go

107 lines
2.2 KiB
Go
Raw Normal View History

2019-10-16 20:52:29 +00:00
package cli
import (
"flag"
"fmt"
"strconv"
)
// BoolFlag is a flag with type bool
type BoolFlag struct {
Name string
2020-06-13 19:08:47 +00:00
Aliases []string
2019-10-16 20:52:29 +00:00
Usage string
2020-06-13 19:08:47 +00:00
EnvVars []string
2019-10-16 20:52:29 +00:00
FilePath string
Required bool
Hidden bool
2020-06-13 19:08:47 +00:00
Value bool
DefaultText string
2019-10-16 20:52:29 +00:00
Destination *bool
2020-06-13 19:08:47 +00: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
2019-10-16 20:52:29 +00:00
}
// String returns a readable representation of this value
// (for usage defaults)
2020-06-13 19:08:47 +00:00
func (f *BoolFlag) String() string {
2019-10-16 20:52:29 +00:00
return FlagStringer(f)
}
2020-06-13 19:08:47 +00:00
// Names returns the names of the flag
func (f *BoolFlag) Names() []string {
return flagNames(f)
2019-10-16 20:52:29 +00:00
}
// IsRequired returns whether or not the flag is required
2020-06-13 19:08:47 +00:00
func (f *BoolFlag) IsRequired() bool {
2019-10-16 20:52:29 +00:00
return f.Required
}
// TakesValue returns true of the flag takes a value, otherwise false
2020-06-13 19:08:47 +00:00
func (f *BoolFlag) TakesValue() bool {
2019-10-16 20:52:29 +00:00
return false
}
// GetUsage returns the usage string for the flag
2020-06-13 19:08:47 +00:00
func (f *BoolFlag) GetUsage() string {
2019-10-16 20:52:29 +00: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-06-13 19:08:47 +00:00
func (f *BoolFlag) GetValue() string {
2019-10-16 20:52:29 +00:00
return ""
}
// Apply populates the flag given the flag set and environment
2020-06-13 19:08:47 +00: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)
2019-10-16 20:52:29 +00:00
if err != nil {
2020-06-13 19:08:47 +00:00
return fmt.Errorf("could not parse %q as bool value for flag %s: %s", val, f.Name, err)
2019-10-16 20:52:29 +00:00
}
2020-06-13 19:08:47 +00:00
f.Value = valBool
f.HasBeenSet = true
2019-10-16 20:52:29 +00:00
}
}
2020-06-13 19:08:47 +00:00
for _, name := range f.Names() {
2019-10-16 20:52:29 +00:00
if f.Destination != nil {
2020-06-13 19:08:47 +00:00
set.BoolVar(f.Destination, name, f.Value, f.Usage)
continue
2019-10-16 20:52:29 +00:00
}
2020-06-13 19:08:47 +00:00
set.Bool(name, f.Value, f.Usage)
}
2019-10-16 20:52:29 +00:00
return nil
}
2020-06-13 19:08:47 +00: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
}
2019-10-16 20:52:29 +00: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
}