2019-04-25 17:06:53 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2020-01-04 04:45:36 +00:00
|
|
|
"io/ioutil"
|
2019-04-25 17:06:53 +00:00
|
|
|
"reflect"
|
2020-01-04 17:44:25 +00:00
|
|
|
"regexp"
|
2019-04-25 17:06:53 +00:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
2020-01-04 17:44:25 +00:00
|
|
|
"time"
|
2019-04-25 17:06:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultPlaceholder = "value"
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
var (
|
|
|
|
slPfx = fmt.Sprintf("sl:::%d:::", time.Now().UTC().UnixNano())
|
|
|
|
|
|
|
|
commaWhitespace = regexp.MustCompile("[, ]+.*")
|
|
|
|
)
|
|
|
|
|
2019-04-25 17:06:53 +00:00
|
|
|
// BashCompletionFlag enables bash-completion for all commands and subcommands
|
2020-01-04 17:44:25 +00:00
|
|
|
var BashCompletionFlag Flag = &BoolFlag{
|
2019-04-25 17:06:53 +00:00
|
|
|
Name: "generate-bash-completion",
|
|
|
|
Hidden: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// VersionFlag prints the version for the application
|
2020-01-04 17:44:25 +00:00
|
|
|
var VersionFlag Flag = &BoolFlag{
|
|
|
|
Name: "version",
|
|
|
|
Aliases: []string{"v"},
|
|
|
|
Usage: "print the version",
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
// HelpFlag prints the help for all commands and subcommands.
|
|
|
|
// Set to nil to disable the flag. The subcommand
|
2020-04-30 10:54:11 +00:00
|
|
|
// will still be added unless HideHelp or HideHelpCommand is set to true.
|
2020-01-04 17:44:25 +00:00
|
|
|
var HelpFlag Flag = &BoolFlag{
|
|
|
|
Name: "help",
|
|
|
|
Aliases: []string{"h"},
|
|
|
|
Usage: "show help",
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FlagStringer converts a flag definition to a string. This is used by help
|
|
|
|
// to display a flag.
|
|
|
|
var FlagStringer FlagStringFunc = stringifyFlag
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
// Serializer is used to circumvent the limitations of flag.FlagSet.Set
|
|
|
|
type Serializer interface {
|
|
|
|
Serialize() string
|
|
|
|
}
|
|
|
|
|
2020-01-04 04:45:36 +00:00
|
|
|
// FlagNamePrefixer converts a full flag name and its placeholder into the help
|
|
|
|
// message flag prefix. This is used by the default FlagStringer.
|
|
|
|
var FlagNamePrefixer FlagNamePrefixFunc = prefixedNames
|
|
|
|
|
|
|
|
// FlagEnvHinter annotates flag help message with the environment variable
|
|
|
|
// details. This is used by the default FlagStringer.
|
|
|
|
var FlagEnvHinter FlagEnvHintFunc = withEnvHint
|
|
|
|
|
|
|
|
// FlagFileHinter annotates flag help message with the environment variable
|
|
|
|
// details. This is used by the default FlagStringer.
|
|
|
|
var FlagFileHinter FlagFileHintFunc = withFileHint
|
|
|
|
|
2019-04-25 17:06:53 +00:00
|
|
|
// FlagsByName is a slice of Flag.
|
|
|
|
type FlagsByName []Flag
|
|
|
|
|
|
|
|
func (f FlagsByName) Len() int {
|
|
|
|
return len(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FlagsByName) Less(i, j int) bool {
|
2020-01-04 17:44:25 +00:00
|
|
|
if len(f[j].Names()) == 0 {
|
|
|
|
return false
|
|
|
|
} else if len(f[i].Names()) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return lexicographicLess(f[i].Names()[0], f[j].Names()[0])
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f FlagsByName) Swap(i, j int) {
|
|
|
|
f[i], f[j] = f[j], f[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flag is a common interface related to parsing flags in cli.
|
|
|
|
// For more advanced flag parsing techniques, it is recommended that
|
|
|
|
// this interface be implemented.
|
|
|
|
type Flag interface {
|
|
|
|
fmt.Stringer
|
|
|
|
// Apply Flag settings to the given flag set
|
2020-01-04 17:44:25 +00:00
|
|
|
Apply(*flag.FlagSet) error
|
|
|
|
Names() []string
|
|
|
|
IsSet() bool
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 04:45:36 +00:00
|
|
|
// RequiredFlag is an interface that allows us to mark flags as required
|
|
|
|
// it allows flags required flags to be backwards compatible with the Flag interface
|
|
|
|
type RequiredFlag interface {
|
|
|
|
Flag
|
|
|
|
|
|
|
|
IsRequired() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// DocGenerationFlag is an interface that allows documentation generation for the flag
|
|
|
|
type DocGenerationFlag interface {
|
|
|
|
Flag
|
|
|
|
|
|
|
|
// TakesValue returns true if the flag takes a value, otherwise false
|
|
|
|
TakesValue() bool
|
|
|
|
|
|
|
|
// GetUsage returns the usage string for the flag
|
|
|
|
GetUsage() string
|
|
|
|
|
|
|
|
// GetValue returns the flags value as string representation and an empty
|
|
|
|
// string if the flag takes no value at all.
|
|
|
|
GetValue() string
|
|
|
|
}
|
|
|
|
|
2019-04-25 17:06:53 +00:00
|
|
|
func flagSet(name string, flags []Flag) (*flag.FlagSet, error) {
|
|
|
|
set := flag.NewFlagSet(name, flag.ContinueOnError)
|
|
|
|
|
|
|
|
for _, f := range flags {
|
2020-01-04 17:44:25 +00:00
|
|
|
if err := f.Apply(set); err != nil {
|
|
|
|
return nil, err
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-04 04:45:36 +00:00
|
|
|
set.SetOutput(ioutil.Discard)
|
2019-04-25 17:06:53 +00:00
|
|
|
return set, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func visibleFlags(fl []Flag) []Flag {
|
2020-01-04 04:45:36 +00:00
|
|
|
var visible []Flag
|
|
|
|
for _, f := range fl {
|
|
|
|
field := flagValue(f).FieldByName("Hidden")
|
2019-04-25 17:06:53 +00:00
|
|
|
if !field.IsValid() || !field.Bool() {
|
2020-01-04 04:45:36 +00:00
|
|
|
visible = append(visible, f)
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return visible
|
|
|
|
}
|
|
|
|
|
|
|
|
func prefixFor(name string) (prefix string) {
|
|
|
|
if len(name) == 1 {
|
|
|
|
prefix = "-"
|
|
|
|
} else {
|
|
|
|
prefix = "--"
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the placeholder, if any, and the unquoted usage string.
|
|
|
|
func unquoteUsage(usage string) (string, string) {
|
|
|
|
for i := 0; i < len(usage); i++ {
|
|
|
|
if usage[i] == '`' {
|
|
|
|
for j := i + 1; j < len(usage); j++ {
|
|
|
|
if usage[j] == '`' {
|
|
|
|
name := usage[i+1 : j]
|
|
|
|
usage = usage[:i] + name + usage[j+1:]
|
|
|
|
return name, usage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", usage
|
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
func prefixedNames(names []string, placeholder string) string {
|
2019-04-25 17:06:53 +00:00
|
|
|
var prefixed string
|
2020-01-04 17:44:25 +00:00
|
|
|
for i, name := range names {
|
|
|
|
if name == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-04-25 17:06:53 +00:00
|
|
|
prefixed += prefixFor(name) + name
|
|
|
|
if placeholder != "" {
|
|
|
|
prefixed += " " + placeholder
|
|
|
|
}
|
2020-01-04 17:44:25 +00:00
|
|
|
if i < len(names)-1 {
|
2019-04-25 17:06:53 +00:00
|
|
|
prefixed += ", "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return prefixed
|
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
func withEnvHint(envVars []string, str string) string {
|
2019-04-25 17:06:53 +00:00
|
|
|
envText := ""
|
2020-01-04 17:44:25 +00:00
|
|
|
if envVars != nil && len(envVars) > 0 {
|
2019-04-25 17:06:53 +00:00
|
|
|
prefix := "$"
|
|
|
|
suffix := ""
|
|
|
|
sep := ", $"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
prefix = "%"
|
|
|
|
suffix = "%"
|
|
|
|
sep = "%, %"
|
|
|
|
}
|
2020-01-04 17:44:25 +00:00
|
|
|
|
|
|
|
envText = fmt.Sprintf(" [%s%s%s]", prefix, strings.Join(envVars, sep), suffix)
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
return str + envText
|
|
|
|
}
|
|
|
|
|
2020-04-30 10:54:11 +00:00
|
|
|
func flagNames(name string, aliases []string) []string {
|
2020-01-04 17:44:25 +00:00
|
|
|
var ret []string
|
|
|
|
|
|
|
|
for _, part := range append([]string{name}, aliases...) {
|
|
|
|
// v1 -> v2 migration warning zone:
|
|
|
|
// Strip off anything after the first found comma or space, which
|
|
|
|
// *hopefully* makes it a tiny bit more obvious that unexpected behavior is
|
|
|
|
// caused by using the v1 form of stringly typed "Name".
|
|
|
|
ret = append(ret, commaWhitespace.ReplaceAllString(part, ""))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func flagStringSliceField(f Flag, name string) []string {
|
|
|
|
fv := flagValue(f)
|
|
|
|
field := fv.FieldByName(name)
|
|
|
|
|
|
|
|
if field.IsValid() {
|
|
|
|
return field.Interface().([]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
2020-01-04 04:45:36 +00:00
|
|
|
func withFileHint(filePath, str string) string {
|
|
|
|
fileText := ""
|
|
|
|
if filePath != "" {
|
|
|
|
fileText = fmt.Sprintf(" [%s]", filePath)
|
|
|
|
}
|
|
|
|
return str + fileText
|
|
|
|
}
|
|
|
|
|
2019-04-25 17:06:53 +00:00
|
|
|
func flagValue(f Flag) reflect.Value {
|
|
|
|
fv := reflect.ValueOf(f)
|
|
|
|
for fv.Kind() == reflect.Ptr {
|
|
|
|
fv = reflect.Indirect(fv)
|
|
|
|
}
|
|
|
|
return fv
|
|
|
|
}
|
|
|
|
|
2020-04-30 10:54:11 +00:00
|
|
|
func formatDefault(format string) string {
|
|
|
|
return " (default: " + format + ")"
|
|
|
|
}
|
|
|
|
|
2019-04-25 17:06:53 +00:00
|
|
|
func stringifyFlag(f Flag) string {
|
|
|
|
fv := flagValue(f)
|
|
|
|
|
2020-04-30 10:54:11 +00:00
|
|
|
switch f := f.(type) {
|
2020-01-04 17:44:25 +00:00
|
|
|
case *IntSliceFlag:
|
|
|
|
return withEnvHint(flagStringSliceField(f, "EnvVars"),
|
2020-04-30 10:54:11 +00:00
|
|
|
stringifyIntSliceFlag(f))
|
2020-01-04 17:44:25 +00:00
|
|
|
case *Int64SliceFlag:
|
|
|
|
return withEnvHint(flagStringSliceField(f, "EnvVars"),
|
2020-04-30 10:54:11 +00:00
|
|
|
stringifyInt64SliceFlag(f))
|
2020-01-04 17:44:25 +00:00
|
|
|
case *Float64SliceFlag:
|
|
|
|
return withEnvHint(flagStringSliceField(f, "EnvVars"),
|
2020-04-30 10:54:11 +00:00
|
|
|
stringifyFloat64SliceFlag(f))
|
2020-01-04 17:44:25 +00:00
|
|
|
case *StringSliceFlag:
|
|
|
|
return withEnvHint(flagStringSliceField(f, "EnvVars"),
|
2020-04-30 10:54:11 +00:00
|
|
|
stringifyStringSliceFlag(f))
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
placeholder, usage := unquoteUsage(fv.FieldByName("Usage").String())
|
|
|
|
|
|
|
|
needsPlaceholder := false
|
|
|
|
defaultValueString := ""
|
2020-01-04 17:44:25 +00:00
|
|
|
val := fv.FieldByName("Value")
|
|
|
|
if val.IsValid() {
|
|
|
|
needsPlaceholder = val.Kind() != reflect.Bool
|
2020-04-30 10:54:11 +00:00
|
|
|
defaultValueString = fmt.Sprintf(formatDefault("%v"), val.Interface())
|
2019-04-25 17:06:53 +00:00
|
|
|
|
|
|
|
if val.Kind() == reflect.String && val.String() != "" {
|
2020-04-30 10:54:11 +00:00
|
|
|
defaultValueString = fmt.Sprintf(formatDefault("%q"), val.String())
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
helpText := fv.FieldByName("DefaultText")
|
|
|
|
if helpText.IsValid() && helpText.String() != "" {
|
|
|
|
needsPlaceholder = val.Kind() != reflect.Bool
|
2020-04-30 10:54:11 +00:00
|
|
|
defaultValueString = fmt.Sprintf(formatDefault("%s"), helpText.String())
|
2020-01-04 17:44:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 10:54:11 +00:00
|
|
|
if defaultValueString == formatDefault("") {
|
2019-04-25 17:06:53 +00:00
|
|
|
defaultValueString = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if needsPlaceholder && placeholder == "" {
|
|
|
|
placeholder = defaultPlaceholder
|
|
|
|
}
|
|
|
|
|
2020-01-04 04:45:36 +00:00
|
|
|
usageWithDefault := strings.TrimSpace(usage + defaultValueString)
|
2019-04-25 17:06:53 +00:00
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
return withEnvHint(flagStringSliceField(f, "EnvVars"),
|
|
|
|
fmt.Sprintf("%s\t%s", prefixedNames(f.Names(), placeholder), usageWithDefault))
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
func stringifyIntSliceFlag(f *IntSliceFlag) string {
|
2020-01-04 04:45:36 +00:00
|
|
|
var defaultVals []string
|
2019-04-25 17:06:53 +00:00
|
|
|
if f.Value != nil && len(f.Value.Value()) > 0 {
|
|
|
|
for _, i := range f.Value.Value() {
|
2020-01-04 04:45:36 +00:00
|
|
|
defaultVals = append(defaultVals, strconv.Itoa(i))
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
return stringifySliceFlag(f.Usage, f.Names(), defaultVals)
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
func stringifyInt64SliceFlag(f *Int64SliceFlag) string {
|
2020-01-04 04:45:36 +00:00
|
|
|
var defaultVals []string
|
2019-04-25 17:06:53 +00:00
|
|
|
if f.Value != nil && len(f.Value.Value()) > 0 {
|
|
|
|
for _, i := range f.Value.Value() {
|
2020-01-04 04:45:36 +00:00
|
|
|
defaultVals = append(defaultVals, strconv.FormatInt(i, 10))
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
return stringifySliceFlag(f.Usage, f.Names(), defaultVals)
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringifyFloat64SliceFlag(f *Float64SliceFlag) string {
|
|
|
|
var defaultVals []string
|
|
|
|
|
|
|
|
if f.Value != nil && len(f.Value.Value()) > 0 {
|
|
|
|
for _, i := range f.Value.Value() {
|
|
|
|
defaultVals = append(defaultVals, strings.TrimRight(strings.TrimRight(fmt.Sprintf("%f", i), "0"), "."))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringifySliceFlag(f.Usage, f.Names(), defaultVals)
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
func stringifyStringSliceFlag(f *StringSliceFlag) string {
|
2020-01-04 04:45:36 +00:00
|
|
|
var defaultVals []string
|
2019-04-25 17:06:53 +00:00
|
|
|
if f.Value != nil && len(f.Value.Value()) > 0 {
|
|
|
|
for _, s := range f.Value.Value() {
|
|
|
|
if len(s) > 0 {
|
2020-01-04 04:45:36 +00:00
|
|
|
defaultVals = append(defaultVals, strconv.Quote(s))
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
return stringifySliceFlag(f.Usage, f.Names(), defaultVals)
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
func stringifySliceFlag(usage string, names, defaultVals []string) string {
|
2019-04-25 17:06:53 +00:00
|
|
|
placeholder, usage := unquoteUsage(usage)
|
|
|
|
if placeholder == "" {
|
|
|
|
placeholder = defaultPlaceholder
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultVal := ""
|
|
|
|
if len(defaultVals) > 0 {
|
2020-04-30 10:54:11 +00:00
|
|
|
defaultVal = fmt.Sprintf(formatDefault("%s"), strings.Join(defaultVals, ", "))
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
usageWithDefault := strings.TrimSpace(fmt.Sprintf("%s%s", usage, defaultVal))
|
|
|
|
return fmt.Sprintf("%s\t%s", prefixedNames(names, placeholder), usageWithDefault)
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasFlag(flags []Flag, fl Flag) bool {
|
|
|
|
for _, existing := range flags {
|
|
|
|
if fl == existing {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2020-01-04 04:45:36 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
func flagFromEnvOrFile(envVars []string, filePath string) (val string, ok bool) {
|
|
|
|
for _, envVar := range envVars {
|
2020-01-04 04:45:36 +00:00
|
|
|
envVar = strings.TrimSpace(envVar)
|
2020-01-04 17:44:25 +00:00
|
|
|
if val, ok := syscall.Getenv(envVar); ok {
|
|
|
|
return val, true
|
2020-01-04 04:45:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, fileVar := range strings.Split(filePath, ",") {
|
|
|
|
if data, err := ioutil.ReadFile(fileVar); err == nil {
|
|
|
|
return string(data), true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|