2022-04-04 20:06:48 +00:00
|
|
|
|
package cli
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Generic is a generic parseable type identified by a specific flag
|
|
|
|
|
type Generic interface {
|
|
|
|
|
Set(value string) error
|
|
|
|
|
String() string
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 14:42:20 +00:00
|
|
|
|
type stringGeneric struct {
|
|
|
|
|
value string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *stringGeneric) Set(value string) error {
|
|
|
|
|
s.value = value
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *stringGeneric) String() string {
|
|
|
|
|
return s.value
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-04 20:06:48 +00:00
|
|
|
|
// TakesValue returns true of the flag takes a value, otherwise false
|
|
|
|
|
func (f *GenericFlag) TakesValue() bool {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetUsage returns the usage string for the flag
|
|
|
|
|
func (f *GenericFlag) GetUsage() string {
|
|
|
|
|
return f.Usage
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-05 13:02:09 +00:00
|
|
|
|
// GetCategory returns the category for the flag
|
|
|
|
|
func (f *GenericFlag) GetCategory() string {
|
|
|
|
|
return f.Category
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-04 20:06:48 +00:00
|
|
|
|
// GetValue returns the flags value as string representation and an empty
|
|
|
|
|
// string if the flag takes no value at all.
|
|
|
|
|
func (f *GenericFlag) GetValue() string {
|
|
|
|
|
if f.Value != nil {
|
|
|
|
|
return f.Value.String()
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-05 13:02:09 +00:00
|
|
|
|
// GetDefaultText returns the default text for this flag
|
|
|
|
|
func (f *GenericFlag) GetDefaultText() string {
|
|
|
|
|
if f.DefaultText != "" {
|
|
|
|
|
return f.DefaultText
|
|
|
|
|
}
|
2024-06-11 16:52:57 +00:00
|
|
|
|
val := f.Value
|
|
|
|
|
if f.defaultValueSet {
|
|
|
|
|
val = f.defaultValue
|
2023-03-22 14:42:20 +00:00
|
|
|
|
}
|
2024-06-11 16:52:57 +00:00
|
|
|
|
|
|
|
|
|
if val != nil {
|
|
|
|
|
return val.String()
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 14:42:20 +00:00
|
|
|
|
return ""
|
2022-07-05 13:02:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetEnvVars returns the env vars for this flag
|
|
|
|
|
func (f *GenericFlag) GetEnvVars() []string {
|
|
|
|
|
return f.EnvVars
|
2022-04-04 20:06:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Apply takes the flagset and calls Set on the generic flag with the value
|
|
|
|
|
// provided by the user for parsing by the flag
|
2023-03-22 14:42:20 +00:00
|
|
|
|
func (f *GenericFlag) Apply(set *flag.FlagSet) error {
|
|
|
|
|
// set default value so that environment wont be able to overwrite it
|
|
|
|
|
if f.Value != nil {
|
|
|
|
|
f.defaultValue = &stringGeneric{value: f.Value.String()}
|
2024-06-11 16:52:57 +00:00
|
|
|
|
f.defaultValueSet = true
|
2023-03-22 14:42:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-05 13:02:09 +00:00
|
|
|
|
if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found {
|
2022-04-04 20:06:48 +00:00
|
|
|
|
if val != "" {
|
|
|
|
|
if err := f.Value.Set(val); err != nil {
|
2022-07-05 13:02:09 +00:00
|
|
|
|
return fmt.Errorf("could not parse %q from %s as value for flag %s: %s", val, source, f.Name, err)
|
2022-04-04 20:06:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
f.HasBeenSet = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, name := range f.Names() {
|
2023-03-22 14:42:20 +00:00
|
|
|
|
if f.Destination != nil {
|
|
|
|
|
set.Var(f.Destination, name, f.Usage)
|
|
|
|
|
continue
|
|
|
|
|
}
|
2022-04-04 20:06:48 +00:00
|
|
|
|
set.Var(f.Value, name, f.Usage)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-05 13:02:09 +00:00
|
|
|
|
// Get returns the flag’s value in the given Context.
|
|
|
|
|
func (f *GenericFlag) Get(ctx *Context) interface{} {
|
|
|
|
|
return ctx.Generic(f.Name)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 14:42:20 +00:00
|
|
|
|
// RunAction executes flag action if set
|
|
|
|
|
func (f *GenericFlag) RunAction(c *Context) error {
|
|
|
|
|
if f.Action != nil {
|
|
|
|
|
return f.Action(c, c.Generic(f.Name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-04 20:06:48 +00:00
|
|
|
|
// Generic looks up the value of a local GenericFlag, returns
|
|
|
|
|
// nil if not found
|
2022-07-05 13:02:09 +00:00
|
|
|
|
func (cCtx *Context) Generic(name string) interface{} {
|
|
|
|
|
if fs := cCtx.lookupFlagSet(name); fs != nil {
|
2022-04-04 20:06:48 +00:00
|
|
|
|
return lookupGeneric(name, fs)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func lookupGeneric(name string, set *flag.FlagSet) interface{} {
|
2023-10-07 13:59:06 +00:00
|
|
|
|
if f := set.Lookup(name); f != nil {
|
|
|
|
|
return f.Value
|
2022-04-04 20:06:48 +00:00
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|