2013-07-15 02:16:30 +00:00
|
|
|
package cli
|
|
|
|
|
2013-12-07 15:10:04 +00:00
|
|
|
import (
|
2016-04-29 06:30:49 +00:00
|
|
|
"encoding/json"
|
2013-12-07 15:10:04 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2014-07-11 17:29:56 +00:00
|
|
|
"os"
|
2016-05-01 12:36:17 +00:00
|
|
|
"reflect"
|
2016-05-24 00:55:03 +00:00
|
|
|
"regexp"
|
2016-02-09 16:36:13 +00:00
|
|
|
"runtime"
|
2013-12-07 15:10:04 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-08-02 21:32:32 +00:00
|
|
|
"time"
|
2013-12-07 15:10:04 +00:00
|
|
|
)
|
2013-07-15 02:16:30 +00:00
|
|
|
|
2016-05-02 23:42:08 +00:00
|
|
|
const defaultPlaceholder = "value"
|
|
|
|
|
2016-05-24 00:55:03 +00:00
|
|
|
var (
|
|
|
|
slPfx = fmt.Sprintf("sl:::%d:::", time.Now().UTC().UnixNano())
|
|
|
|
|
|
|
|
commaWhitespace = regexp.MustCompile("[, ]+.*")
|
|
|
|
)
|
2016-04-29 06:30:49 +00:00
|
|
|
|
2016-07-22 08:19:29 +00:00
|
|
|
// GenerateCompletionFlag enables completion for all commands and subcommands
|
|
|
|
var GenerateCompletionFlag = &BoolFlag{
|
|
|
|
Name: "generate-completion",
|
2016-05-01 12:36:17 +00:00
|
|
|
Hidden: true,
|
2014-07-11 22:13:10 +00:00
|
|
|
}
|
2014-04-12 13:32:53 +00:00
|
|
|
|
2016-07-21 19:01:59 +00:00
|
|
|
// InitCompletionFlag generates completion code
|
|
|
|
var InitCompletionFlag = &StringFlag{
|
|
|
|
Name: "init-completion",
|
|
|
|
Usage: "generate completion code. Value must be 'bash' or 'zsh'",
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// VersionFlag prints the version for the application
|
2016-05-22 19:20:52 +00:00
|
|
|
var VersionFlag = &BoolFlag{
|
2016-05-22 01:29:45 +00:00
|
|
|
Name: "version",
|
|
|
|
Aliases: []string{"v"},
|
|
|
|
Usage: "print the version",
|
2014-07-11 22:13:10 +00:00
|
|
|
}
|
2014-04-29 14:21:19 +00:00
|
|
|
|
2016-05-22 01:29:45 +00:00
|
|
|
// HelpFlag prints the help for all commands and subcommands.
|
2016-05-22 19:20:52 +00:00
|
|
|
// Set to nil to disable the flag. The subcommand
|
2016-05-22 01:29:45 +00:00
|
|
|
// will still be added unless HideHelp is set to true.
|
2016-05-22 19:20:52 +00:00
|
|
|
var HelpFlag = &BoolFlag{
|
2016-05-22 01:29:45 +00:00
|
|
|
Name: "help",
|
|
|
|
Aliases: []string{"h"},
|
|
|
|
Usage: "show help",
|
2014-07-11 22:13:10 +00:00
|
|
|
}
|
2014-04-29 14:21:19 +00:00
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// FlagStringer converts a flag definition to a string. This is used by help
|
|
|
|
// to display a flag.
|
2016-05-02 17:07:57 +00:00
|
|
|
var FlagStringer FlagStringFunc = stringifyFlag
|
2016-05-02 17:05:21 +00:00
|
|
|
|
2016-04-29 06:30:49 +00:00
|
|
|
// Serializeder is used to circumvent the limitations of flag.FlagSet.Set
|
|
|
|
type Serializeder interface {
|
|
|
|
Serialized() string
|
|
|
|
}
|
|
|
|
|
2013-11-01 14:31:37 +00:00
|
|
|
// Flag is a common interface related to parsing flags in cli.
|
2016-02-09 16:36:13 +00:00
|
|
|
// For more advanced flag parsing techniques, it is recommended that
|
2013-11-01 14:31:37 +00:00
|
|
|
// this interface be implemented.
|
2013-07-15 02:16:30 +00:00
|
|
|
type Flag interface {
|
2013-07-19 00:39:42 +00:00
|
|
|
fmt.Stringer
|
2013-11-01 14:31:37 +00:00
|
|
|
// Apply Flag settings to the given flag set
|
2013-07-19 00:39:42 +00:00
|
|
|
Apply(*flag.FlagSet)
|
2016-05-22 01:29:45 +00:00
|
|
|
Names() []string
|
2013-07-19 00:39:42 +00:00
|
|
|
}
|
|
|
|
|
2013-07-20 15:21:20 +00:00
|
|
|
func flagSet(name string, flags []Flag) *flag.FlagSet {
|
2013-07-24 14:35:45 +00:00
|
|
|
set := flag.NewFlagSet(name, flag.ContinueOnError)
|
2013-09-24 01:41:31 +00:00
|
|
|
|
2013-07-19 00:39:42 +00:00
|
|
|
for _, f := range flags {
|
|
|
|
f.Apply(set)
|
|
|
|
}
|
|
|
|
return set
|
2013-07-15 02:16:30 +00:00
|
|
|
}
|
|
|
|
|
2014-04-15 14:16:47 +00:00
|
|
|
// Generic is a generic parseable type identified by a specific flag
|
|
|
|
type Generic interface {
|
|
|
|
Set(value string) error
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
2015-01-08 18:58:34 +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
|
2016-05-22 19:20:52 +00:00
|
|
|
func (f *GenericFlag) Apply(set *flag.FlagSet) {
|
2014-07-11 17:29:56 +00:00
|
|
|
val := f.Value
|
2016-05-22 01:29:45 +00:00
|
|
|
if f.EnvVars != nil {
|
|
|
|
for _, envVar := range f.EnvVars {
|
2014-09-23 03:24:08 +00:00
|
|
|
if envVal := os.Getenv(envVar); envVal != "" {
|
|
|
|
val.Set(envVal)
|
|
|
|
break
|
|
|
|
}
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-22 01:29:45 +00:00
|
|
|
for _, name := range f.Names() {
|
2016-05-24 02:00:59 +00:00
|
|
|
set.Var(val, name, f.Usage)
|
2016-05-22 01:29:45 +00:00
|
|
|
}
|
2014-04-15 14:16:47 +00:00
|
|
|
}
|
|
|
|
|
2016-04-05 16:35:30 +00:00
|
|
|
// StringSlice wraps a []string to satisfy flag.Value
|
|
|
|
type StringSlice struct {
|
|
|
|
slice []string
|
|
|
|
hasBeenSet bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStringSlice creates a *StringSlice with default values
|
|
|
|
func NewStringSlice(defaults ...string) *StringSlice {
|
|
|
|
return &StringSlice{slice: append([]string{}, defaults...)}
|
|
|
|
}
|
2013-09-24 01:41:31 +00:00
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Set appends the string value to the list of values
|
2013-09-24 19:36:01 +00:00
|
|
|
func (f *StringSlice) Set(value string) error {
|
2016-04-05 16:35:30 +00:00
|
|
|
if !f.hasBeenSet {
|
|
|
|
f.slice = []string{}
|
|
|
|
f.hasBeenSet = true
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:30:49 +00:00
|
|
|
if strings.HasPrefix(value, slPfx) {
|
2016-04-29 06:53:58 +00:00
|
|
|
// Deserializing assumes overwrite
|
|
|
|
_ = json.Unmarshal([]byte(strings.Replace(value, slPfx, "", 1)), &f.slice)
|
|
|
|
f.hasBeenSet = true
|
2016-04-29 06:30:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-05 16:35:30 +00:00
|
|
|
f.slice = append(f.slice, value)
|
2013-09-24 01:41:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// String returns a readable representation of this value (for usage defaults)
|
2013-09-24 19:36:01 +00:00
|
|
|
func (f *StringSlice) String() string {
|
2016-04-05 16:35:30 +00:00
|
|
|
return fmt.Sprintf("%s", f.slice)
|
2013-09-24 01:41:31 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 06:30:49 +00:00
|
|
|
// Serialized allows StringSlice to fulfill Serializeder
|
|
|
|
func (f *StringSlice) Serialized() string {
|
|
|
|
jsonBytes, _ := json.Marshal(f.slice)
|
|
|
|
return fmt.Sprintf("%s%s", slPfx, string(jsonBytes))
|
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Value returns the slice of strings set by this flag
|
2013-09-24 19:36:01 +00:00
|
|
|
func (f *StringSlice) Value() []string {
|
2016-04-05 16:35:30 +00:00
|
|
|
return f.slice
|
2013-09-24 01:41:31 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2016-05-22 19:20:52 +00:00
|
|
|
func (f *StringSliceFlag) Apply(set *flag.FlagSet) {
|
2016-05-22 01:29:45 +00:00
|
|
|
if f.EnvVars != nil {
|
|
|
|
for _, envVar := range f.EnvVars {
|
2014-09-23 03:24:08 +00:00
|
|
|
if envVal := os.Getenv(envVar); envVal != "" {
|
2016-04-06 02:38:31 +00:00
|
|
|
newVal := NewStringSlice()
|
2014-09-23 03:24:08 +00:00
|
|
|
for _, s := range strings.Split(envVal, ",") {
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
newVal.Set(s)
|
|
|
|
}
|
|
|
|
f.Value = newVal
|
|
|
|
break
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-05 16:35:30 +00:00
|
|
|
if f.Value == nil {
|
2016-04-06 02:38:31 +00:00
|
|
|
f.Value = NewStringSlice()
|
2016-04-05 16:35:30 +00:00
|
|
|
}
|
|
|
|
|
2016-05-22 01:29:45 +00:00
|
|
|
for _, name := range f.Names() {
|
2013-11-18 23:35:23 +00:00
|
|
|
set.Var(f.Value, name, f.Usage)
|
2016-05-22 01:29:45 +00:00
|
|
|
}
|
2013-11-18 23:35:23 +00:00
|
|
|
}
|
|
|
|
|
2016-04-05 16:35:30 +00:00
|
|
|
// IntSlice wraps an []int to satisfy flag.Value
|
|
|
|
type IntSlice struct {
|
|
|
|
slice []int
|
|
|
|
hasBeenSet bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewIntSlice makes an *IntSlice with default values
|
|
|
|
func NewIntSlice(defaults ...int) *IntSlice {
|
|
|
|
return &IntSlice{slice: append([]int{}, defaults...)}
|
|
|
|
}
|
|
|
|
|
2016-06-17 13:54:54 +00:00
|
|
|
// NewInt64Slice makes an *Int64Slice with default values
|
|
|
|
func NewInt64Slice(defaults ...int64) *Int64Slice {
|
|
|
|
return &Int64Slice{slice: append([]int64{}, defaults...)}
|
|
|
|
}
|
|
|
|
|
2016-04-05 16:35:30 +00:00
|
|
|
// SetInt directly adds an integer to the list of values
|
|
|
|
func (i *IntSlice) SetInt(value int) {
|
|
|
|
if !i.hasBeenSet {
|
|
|
|
i.slice = []int{}
|
|
|
|
i.hasBeenSet = true
|
|
|
|
}
|
|
|
|
|
|
|
|
i.slice = append(i.slice, value)
|
|
|
|
}
|
2013-09-24 19:36:01 +00:00
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Set parses the value into an integer and appends it to the list of values
|
2016-04-05 16:35:30 +00:00
|
|
|
func (i *IntSlice) Set(value string) error {
|
|
|
|
if !i.hasBeenSet {
|
|
|
|
i.slice = []int{}
|
|
|
|
i.hasBeenSet = true
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:53:58 +00:00
|
|
|
if strings.HasPrefix(value, slPfx) {
|
|
|
|
// Deserializing assumes overwrite
|
|
|
|
_ = json.Unmarshal([]byte(strings.Replace(value, slPfx, "", 1)), &i.slice)
|
|
|
|
i.hasBeenSet = true
|
2016-04-29 06:30:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-17 13:54:54 +00:00
|
|
|
tmp, err := strconv.ParseInt(value, 0, 64)
|
2013-09-24 19:36:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-09 15:18:21 +00:00
|
|
|
|
2016-06-17 13:54:54 +00:00
|
|
|
i.slice = append(i.slice, int(tmp))
|
2013-09-24 19:36:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// String returns a readable representation of this value (for usage defaults)
|
2013-09-24 19:36:01 +00:00
|
|
|
func (f *IntSlice) String() string {
|
2016-06-17 13:11:36 +00:00
|
|
|
return fmt.Sprintf("%#v", f.slice)
|
2013-09-24 19:36:01 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 06:53:58 +00:00
|
|
|
// Serialized allows IntSlice to fulfill Serializeder
|
|
|
|
func (i *IntSlice) Serialized() string {
|
|
|
|
jsonBytes, _ := json.Marshal(i.slice)
|
|
|
|
return fmt.Sprintf("%s%s", slPfx, string(jsonBytes))
|
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Value returns the slice of ints set by this flag
|
2016-04-05 16:35:30 +00:00
|
|
|
func (i *IntSlice) Value() []int {
|
|
|
|
return i.slice
|
2013-09-24 19:36:01 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2016-05-22 19:20:52 +00:00
|
|
|
func (f *IntSliceFlag) Apply(set *flag.FlagSet) {
|
2016-05-22 01:29:45 +00:00
|
|
|
if f.EnvVars != nil {
|
|
|
|
for _, envVar := range f.EnvVars {
|
2014-09-23 03:24:08 +00:00
|
|
|
if envVal := os.Getenv(envVar); envVal != "" {
|
2016-04-06 02:40:12 +00:00
|
|
|
newVal := NewIntSlice()
|
2014-09-23 03:24:08 +00:00
|
|
|
for _, s := range strings.Split(envVal, ",") {
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
err := newVal.Set(s)
|
|
|
|
if err != nil {
|
2016-05-06 16:14:26 +00:00
|
|
|
fmt.Fprintf(ErrWriter, err.Error())
|
2014-09-23 03:24:08 +00:00
|
|
|
}
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
2014-09-23 03:24:08 +00:00
|
|
|
f.Value = newVal
|
|
|
|
break
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 02:38:31 +00:00
|
|
|
if f.Value == nil {
|
|
|
|
f.Value = NewIntSlice()
|
|
|
|
}
|
|
|
|
|
2016-05-22 01:29:45 +00:00
|
|
|
for _, name := range f.Names() {
|
2013-11-18 23:35:23 +00:00
|
|
|
set.Var(f.Value, name, f.Usage)
|
2016-05-22 01:29:45 +00:00
|
|
|
}
|
2013-11-18 23:35:23 +00:00
|
|
|
}
|
|
|
|
|
2016-06-12 04:54:33 +00:00
|
|
|
// Int64Slice is an opaque type for []int to satisfy flag.Value
|
2016-06-17 13:54:54 +00:00
|
|
|
type Int64Slice struct {
|
|
|
|
slice []int64
|
|
|
|
hasBeenSet bool
|
|
|
|
}
|
2016-06-12 04:54:33 +00:00
|
|
|
|
|
|
|
// Set parses the value into an integer and appends it to the list of values
|
|
|
|
func (f *Int64Slice) Set(value string) error {
|
2016-06-17 13:54:54 +00:00
|
|
|
if !f.hasBeenSet {
|
|
|
|
f.slice = []int64{}
|
|
|
|
f.hasBeenSet = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(value, slPfx) {
|
|
|
|
// Deserializing assumes overwrite
|
|
|
|
_ = json.Unmarshal([]byte(strings.Replace(value, slPfx, "", 1)), &f.slice)
|
|
|
|
f.hasBeenSet = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp, err := strconv.ParseInt(value, 0, 64)
|
2016-06-12 04:54:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-17 13:54:54 +00:00
|
|
|
|
|
|
|
f.slice = append(f.slice, tmp)
|
2016-06-12 04:54:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a readable representation of this value (for usage defaults)
|
|
|
|
func (f *Int64Slice) String() string {
|
2016-06-17 13:54:54 +00:00
|
|
|
return fmt.Sprintf("%#v", f.slice)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serialized allows Int64Slice to fulfill Serializeder
|
|
|
|
func (f *Int64Slice) Serialized() string {
|
|
|
|
jsonBytes, _ := json.Marshal(f.slice)
|
|
|
|
return fmt.Sprintf("%s%s", slPfx, string(jsonBytes))
|
2016-06-12 04:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the slice of ints set by this flag
|
|
|
|
func (f *Int64Slice) Value() []int64 {
|
2016-06-17 13:54:54 +00:00
|
|
|
return f.slice
|
2016-06-12 04:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply populates the flag given the flag set and environment
|
2016-06-17 13:54:54 +00:00
|
|
|
func (f *Int64SliceFlag) Apply(set *flag.FlagSet) {
|
|
|
|
if f.EnvVars != nil {
|
|
|
|
for _, envVar := range f.EnvVars {
|
2016-06-12 04:54:33 +00:00
|
|
|
if envVal := os.Getenv(envVar); envVal != "" {
|
2016-06-17 13:54:54 +00:00
|
|
|
newVal := NewInt64Slice()
|
2016-06-12 04:54:33 +00:00
|
|
|
for _, s := range strings.Split(envVal, ",") {
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
err := newVal.Set(s)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(ErrWriter, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.Value = newVal
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 13:54:54 +00:00
|
|
|
if f.Value == nil {
|
|
|
|
f.Value = NewInt64Slice()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range f.Names() {
|
2016-06-12 04:54:33 +00:00
|
|
|
set.Var(f.Value, name, f.Usage)
|
2016-06-17 13:54:54 +00:00
|
|
|
}
|
2016-06-12 04:54:33 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2016-05-22 19:20:52 +00:00
|
|
|
func (f *BoolFlag) Apply(set *flag.FlagSet) {
|
2016-05-22 01:29:45 +00:00
|
|
|
if f.EnvVars != nil {
|
|
|
|
for _, envVar := range f.EnvVars {
|
2014-09-23 03:24:08 +00:00
|
|
|
if envVal := os.Getenv(envVar); envVal != "" {
|
|
|
|
envValBool, err := strconv.ParseBool(envVal)
|
|
|
|
if err == nil {
|
2016-05-18 12:20:15 +00:00
|
|
|
f.Value = envValBool
|
2014-09-23 03:24:08 +00:00
|
|
|
}
|
|
|
|
break
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-22 01:29:45 +00:00
|
|
|
for _, name := range f.Names() {
|
2015-11-14 19:01:15 +00:00
|
|
|
if f.Destination != nil {
|
2016-05-18 12:20:15 +00:00
|
|
|
set.BoolVar(f.Destination, name, f.Value, f.Usage)
|
2016-05-24 02:00:59 +00:00
|
|
|
continue
|
2015-11-14 19:01:15 +00:00
|
|
|
}
|
2016-05-18 12:20:15 +00:00
|
|
|
set.Bool(name, f.Value, f.Usage)
|
2016-05-22 01:29:45 +00:00
|
|
|
}
|
2013-11-18 23:35:23 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2016-05-22 19:20:52 +00:00
|
|
|
func (f *StringFlag) Apply(set *flag.FlagSet) {
|
2016-05-22 01:29:45 +00:00
|
|
|
if f.EnvVars != nil {
|
|
|
|
for _, envVar := range f.EnvVars {
|
2014-09-23 03:24:08 +00:00
|
|
|
if envVal := os.Getenv(envVar); envVal != "" {
|
|
|
|
f.Value = envVal
|
|
|
|
break
|
|
|
|
}
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-22 01:29:45 +00:00
|
|
|
for _, name := range f.Names() {
|
2015-11-14 19:01:15 +00:00
|
|
|
if f.Destination != nil {
|
|
|
|
set.StringVar(f.Destination, name, f.Value, f.Usage)
|
2016-05-24 02:00:59 +00:00
|
|
|
continue
|
2015-11-14 19:01:15 +00:00
|
|
|
}
|
2013-11-18 23:35:23 +00:00
|
|
|
set.String(name, f.Value, f.Usage)
|
2016-05-22 01:29:45 +00:00
|
|
|
}
|
2013-11-18 23:35:23 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2016-05-22 19:20:52 +00:00
|
|
|
func (f *IntFlag) Apply(set *flag.FlagSet) {
|
2016-05-22 01:29:45 +00:00
|
|
|
if f.EnvVars != nil {
|
|
|
|
for _, envVar := range f.EnvVars {
|
2014-09-23 03:24:08 +00:00
|
|
|
if envVal := os.Getenv(envVar); envVal != "" {
|
2015-01-09 18:10:42 +00:00
|
|
|
envValInt, err := strconv.ParseInt(envVal, 0, 64)
|
2014-09-23 03:24:08 +00:00
|
|
|
if err == nil {
|
|
|
|
f.Value = int(envValInt)
|
|
|
|
break
|
|
|
|
}
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-22 01:29:45 +00:00
|
|
|
for _, name := range f.Names() {
|
2015-11-14 19:01:15 +00:00
|
|
|
if f.Destination != nil {
|
|
|
|
set.IntVar(f.Destination, name, f.Value, f.Usage)
|
2016-05-24 02:00:59 +00:00
|
|
|
continue
|
2015-11-14 19:01:15 +00:00
|
|
|
}
|
2013-11-18 23:35:23 +00:00
|
|
|
set.Int(name, f.Value, f.Usage)
|
2016-05-22 01:29:45 +00:00
|
|
|
}
|
2013-11-18 23:35:23 +00:00
|
|
|
}
|
|
|
|
|
2016-06-11 22:22:07 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2016-06-17 13:54:54 +00:00
|
|
|
func (f *Int64Flag) Apply(set *flag.FlagSet) {
|
|
|
|
if f.EnvVars != nil {
|
|
|
|
for _, envVar := range f.EnvVars {
|
2016-06-11 22:22:07 +00:00
|
|
|
if envVal := os.Getenv(envVar); envVal != "" {
|
|
|
|
envValInt, err := strconv.ParseInt(envVal, 0, 64)
|
|
|
|
if err == nil {
|
|
|
|
f.Value = envValInt
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 13:54:54 +00:00
|
|
|
for _, name := range f.Names() {
|
2016-06-11 22:22:07 +00:00
|
|
|
if f.Destination != nil {
|
|
|
|
set.Int64Var(f.Destination, name, f.Value, f.Usage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
set.Int64(name, f.Value, f.Usage)
|
2016-06-17 13:54:54 +00:00
|
|
|
}
|
2016-06-11 22:22:07 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 15:13:32 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2016-06-17 13:54:54 +00:00
|
|
|
func (f *UintFlag) Apply(set *flag.FlagSet) {
|
|
|
|
if f.EnvVars != nil {
|
|
|
|
for _, envVar := range f.EnvVars {
|
2016-06-16 15:13:32 +00:00
|
|
|
if envVal := os.Getenv(envVar); envVal != "" {
|
|
|
|
envValInt, err := strconv.ParseUint(envVal, 0, 64)
|
|
|
|
if err == nil {
|
|
|
|
f.Value = uint(envValInt)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 13:54:54 +00:00
|
|
|
for _, name := range f.Names() {
|
2016-06-16 15:13:32 +00:00
|
|
|
if f.Destination != nil {
|
|
|
|
set.UintVar(f.Destination, name, f.Value, f.Usage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
set.Uint(name, f.Value, f.Usage)
|
2016-06-17 13:54:54 +00:00
|
|
|
}
|
2016-06-16 15:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply populates the flag given the flag set and environment
|
2016-06-17 13:54:54 +00:00
|
|
|
func (f *Uint64Flag) Apply(set *flag.FlagSet) {
|
|
|
|
if f.EnvVars != nil {
|
|
|
|
for _, envVar := range f.EnvVars {
|
2016-06-16 15:13:32 +00:00
|
|
|
if envVal := os.Getenv(envVar); envVal != "" {
|
|
|
|
envValInt, err := strconv.ParseUint(envVal, 0, 64)
|
|
|
|
if err == nil {
|
|
|
|
f.Value = uint64(envValInt)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 13:54:54 +00:00
|
|
|
for _, name := range f.Names() {
|
2016-06-16 15:13:32 +00:00
|
|
|
if f.Destination != nil {
|
|
|
|
set.Uint64Var(f.Destination, name, f.Value, f.Usage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
set.Uint64(name, f.Value, f.Usage)
|
2016-06-17 13:54:54 +00:00
|
|
|
}
|
2016-06-16 15:13:32 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2016-05-22 19:20:52 +00:00
|
|
|
func (f *DurationFlag) Apply(set *flag.FlagSet) {
|
2016-05-22 01:29:45 +00:00
|
|
|
if f.EnvVars != nil {
|
|
|
|
for _, envVar := range f.EnvVars {
|
2014-09-23 03:24:08 +00:00
|
|
|
if envVal := os.Getenv(envVar); envVal != "" {
|
|
|
|
envValDuration, err := time.ParseDuration(envVal)
|
|
|
|
if err == nil {
|
|
|
|
f.Value = envValDuration
|
|
|
|
break
|
|
|
|
}
|
2014-08-02 21:32:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-22 01:29:45 +00:00
|
|
|
for _, name := range f.Names() {
|
2015-11-14 19:01:15 +00:00
|
|
|
if f.Destination != nil {
|
|
|
|
set.DurationVar(f.Destination, name, f.Value, f.Usage)
|
2016-05-24 02:00:59 +00:00
|
|
|
continue
|
2015-11-14 19:01:15 +00:00
|
|
|
}
|
2014-08-02 21:32:32 +00:00
|
|
|
set.Duration(name, f.Value, f.Usage)
|
2016-05-22 01:29:45 +00:00
|
|
|
}
|
2014-08-02 21:32:32 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2016-05-22 19:20:52 +00:00
|
|
|
func (f *Float64Flag) Apply(set *flag.FlagSet) {
|
2016-05-22 01:29:45 +00:00
|
|
|
if f.EnvVars != nil {
|
|
|
|
for _, envVar := range f.EnvVars {
|
2014-09-23 03:24:08 +00:00
|
|
|
if envVal := os.Getenv(envVar); envVal != "" {
|
|
|
|
envValFloat, err := strconv.ParseFloat(envVal, 10)
|
|
|
|
if err == nil {
|
|
|
|
f.Value = float64(envValFloat)
|
|
|
|
}
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-22 01:29:45 +00:00
|
|
|
for _, name := range f.Names() {
|
2015-11-14 19:01:15 +00:00
|
|
|
if f.Destination != nil {
|
|
|
|
set.Float64Var(f.Destination, name, f.Value, f.Usage)
|
2016-05-24 02:00:59 +00:00
|
|
|
continue
|
2015-11-14 19:01:15 +00:00
|
|
|
}
|
2013-12-03 13:42:09 +00:00
|
|
|
set.Float64(name, f.Value, f.Usage)
|
2016-05-22 01:29:45 +00:00
|
|
|
}
|
2013-12-03 13:42:09 +00:00
|
|
|
}
|
|
|
|
|
2016-06-29 02:52:25 +00:00
|
|
|
// NewFloat64Slice makes a *Float64Slice with default values
|
|
|
|
func NewFloat64Slice(defaults ...float64) *Float64Slice {
|
|
|
|
return &Float64Slice{slice: append([]float64{}, defaults...)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Float64Slice is an opaque type for []float64 to satisfy flag.Value
|
|
|
|
type Float64Slice struct {
|
|
|
|
slice []float64
|
|
|
|
hasBeenSet bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set parses the value into a float64 and appends it to the list of values
|
|
|
|
func (f *Float64Slice) Set(value string) error {
|
|
|
|
if !f.hasBeenSet {
|
|
|
|
f.slice = []float64{}
|
|
|
|
f.hasBeenSet = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(value, slPfx) {
|
|
|
|
// Deserializing assumes overwrite
|
|
|
|
_ = json.Unmarshal([]byte(strings.Replace(value, slPfx, "", 1)), &f.slice)
|
|
|
|
f.hasBeenSet = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp, err := strconv.ParseFloat(value, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f.slice = append(f.slice, tmp)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a readable representation of this value (for usage defaults)
|
|
|
|
func (f *Float64Slice) String() string {
|
|
|
|
return fmt.Sprintf("%#v", f.slice)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serialized allows Float64Slice to fulfill Serializeder
|
|
|
|
func (f *Float64Slice) Serialized() string {
|
|
|
|
jsonBytes, _ := json.Marshal(f.slice)
|
|
|
|
return fmt.Sprintf("%s%s", slPfx, string(jsonBytes))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the slice of float64s set by this flag
|
|
|
|
func (f *Float64Slice) Value() []float64 {
|
|
|
|
return f.slice
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
func (f *Float64SliceFlag) Apply(set *flag.FlagSet) {
|
|
|
|
if f.EnvVars != nil {
|
|
|
|
for _, envVar := range f.EnvVars {
|
|
|
|
if envVal := os.Getenv(envVar); envVal != "" {
|
|
|
|
newVal := NewFloat64Slice()
|
|
|
|
for _, s := range strings.Split(envVal, ",") {
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
err := newVal.Set(s)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(ErrWriter, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.Value = newVal
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.Value == nil {
|
|
|
|
f.Value = NewFloat64Slice()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range f.Names() {
|
|
|
|
set.Var(f.Value, name, f.Usage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 12:36:17 +00:00
|
|
|
func visibleFlags(fl []Flag) []Flag {
|
|
|
|
visible := []Flag{}
|
|
|
|
for _, flag := range fl {
|
2016-05-22 19:20:52 +00:00
|
|
|
if !flagValue(flag).FieldByName("Hidden").Bool() {
|
2016-05-01 12:36:17 +00:00
|
|
|
visible = append(visible, flag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return visible
|
2015-03-15 10:11:52 +00:00
|
|
|
}
|
|
|
|
|
2013-09-09 18:51:46 +00:00
|
|
|
func prefixFor(name string) (prefix string) {
|
|
|
|
if len(name) == 1 {
|
|
|
|
prefix = "-"
|
|
|
|
} else {
|
|
|
|
prefix = "--"
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2013-11-20 08:05:18 +00:00
|
|
|
|
2016-04-22 03:02:06 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2016-05-22 01:29:45 +00:00
|
|
|
func prefixedNames(names []string, placeholder string) string {
|
2016-04-22 03:02:06 +00:00
|
|
|
var prefixed string
|
2016-05-22 01:29:45 +00:00
|
|
|
for i, name := range names {
|
|
|
|
if name == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-11-20 08:05:18 +00:00
|
|
|
prefixed += prefixFor(name) + name
|
2016-04-22 03:02:06 +00:00
|
|
|
if placeholder != "" {
|
|
|
|
prefixed += " " + placeholder
|
|
|
|
}
|
2016-05-22 01:29:45 +00:00
|
|
|
if i < len(names)-1 {
|
2013-11-20 08:05:18 +00:00
|
|
|
prefixed += ", "
|
|
|
|
}
|
|
|
|
}
|
2016-04-22 03:02:06 +00:00
|
|
|
return prefixed
|
2013-11-20 08:05:18 +00:00
|
|
|
}
|
2014-07-11 17:29:56 +00:00
|
|
|
|
2016-05-22 01:29:45 +00:00
|
|
|
func withEnvHint(envVars []string, str string) string {
|
2014-07-11 17:29:56 +00:00
|
|
|
envText := ""
|
2016-05-22 01:29:45 +00:00
|
|
|
if envVars != nil && len(envVars) > 0 {
|
2016-01-22 14:08:27 +00:00
|
|
|
prefix := "$"
|
|
|
|
suffix := ""
|
|
|
|
sep := ", $"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
prefix = "%"
|
|
|
|
suffix = "%"
|
|
|
|
sep = "%, %"
|
|
|
|
}
|
2016-05-22 01:29:45 +00:00
|
|
|
envText = fmt.Sprintf(" [%s%s%s]", prefix, strings.Join(envVars, sep), suffix)
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
return str + envText
|
|
|
|
}
|
2016-05-02 17:05:21 +00:00
|
|
|
|
2016-05-24 00:55:03 +00:00
|
|
|
func flagNames(f Flag) []string {
|
|
|
|
ret := []string{}
|
|
|
|
|
|
|
|
name := flagStringField(f, "Name")
|
|
|
|
aliases := flagStringSliceField(f, "Aliases")
|
|
|
|
|
|
|
|
for _, part := range append([]string{name}, aliases...) {
|
2016-05-24 08:00:37 +00:00
|
|
|
// 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".
|
2016-05-24 00:55:03 +00:00
|
|
|
ret = append(ret, commaWhitespace.ReplaceAllString(part, ""))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2016-05-22 01:29:45 +00:00
|
|
|
func flagStringSliceField(f Flag, name string) []string {
|
|
|
|
fv := flagValue(f)
|
|
|
|
field := fv.FieldByName(name)
|
|
|
|
|
|
|
|
if field.IsValid() {
|
|
|
|
return field.Interface().([]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func flagStringField(f Flag, name string) string {
|
|
|
|
fv := flagValue(f)
|
|
|
|
field := fv.FieldByName(name)
|
|
|
|
|
|
|
|
if field.IsValid() {
|
|
|
|
return field.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func flagValue(f Flag) reflect.Value {
|
2016-05-02 17:05:21 +00:00
|
|
|
fv := reflect.ValueOf(f)
|
2016-05-22 01:29:45 +00:00
|
|
|
for fv.Kind() == reflect.Ptr {
|
|
|
|
fv = reflect.Indirect(fv)
|
|
|
|
}
|
|
|
|
return fv
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringifyFlag(f Flag) string {
|
|
|
|
fv := flagValue(f)
|
2016-05-02 23:42:08 +00:00
|
|
|
|
|
|
|
switch f.(type) {
|
2016-05-22 19:20:52 +00:00
|
|
|
case *IntSliceFlag:
|
2016-06-17 13:54:54 +00:00
|
|
|
return withEnvHint(flagStringSliceField(f, "EnvVars"),
|
2016-06-17 13:11:36 +00:00
|
|
|
stringifyIntSliceFlag(f.(*IntSliceFlag)))
|
|
|
|
case *Int64SliceFlag:
|
2016-06-17 13:54:54 +00:00
|
|
|
return withEnvHint(flagStringSliceField(f, "EnvVars"),
|
2016-06-17 13:11:36 +00:00
|
|
|
stringifyInt64SliceFlag(f.(*Int64SliceFlag)))
|
2016-06-29 02:52:25 +00:00
|
|
|
case *Float64SliceFlag:
|
|
|
|
return withEnvHint(flagStringSliceField(f, "EnvVars"),
|
|
|
|
stringifyFloat64SliceFlag(f.(*Float64SliceFlag)))
|
2016-05-22 19:20:52 +00:00
|
|
|
case *StringSliceFlag:
|
2016-06-17 13:54:54 +00:00
|
|
|
return withEnvHint(flagStringSliceField(f, "EnvVars"),
|
2016-06-17 13:11:36 +00:00
|
|
|
stringifyStringSliceFlag(f.(*StringSliceFlag)))
|
2016-05-02 23:42:08 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 17:05:21 +00:00
|
|
|
placeholder, usage := unquoteUsage(fv.FieldByName("Usage").String())
|
|
|
|
|
2016-05-02 23:58:16 +00:00
|
|
|
needsPlaceholder := false
|
2016-05-02 17:05:21 +00:00
|
|
|
defaultValueString := ""
|
2016-05-02 23:42:08 +00:00
|
|
|
val := fv.FieldByName("Value")
|
|
|
|
|
2016-05-22 00:02:08 +00:00
|
|
|
if val.IsValid() {
|
|
|
|
needsPlaceholder = val.Kind() != reflect.Bool
|
2016-05-02 23:58:16 +00:00
|
|
|
defaultValueString = fmt.Sprintf(" (default: %v)", val.Interface())
|
|
|
|
|
2016-05-02 23:42:08 +00:00
|
|
|
if val.Kind() == reflect.String && val.String() != "" {
|
2016-05-02 23:58:16 +00:00
|
|
|
defaultValueString = fmt.Sprintf(" (default: %q)", val.String())
|
2016-05-02 23:42:08 +00:00
|
|
|
}
|
2016-05-02 17:05:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 23:58:16 +00:00
|
|
|
if defaultValueString == " (default: )" {
|
|
|
|
defaultValueString = ""
|
2016-05-02 17:05:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 23:42:08 +00:00
|
|
|
if needsPlaceholder && placeholder == "" {
|
|
|
|
placeholder = defaultPlaceholder
|
2016-05-02 17:05:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 23:58:16 +00:00
|
|
|
usageWithDefault := strings.TrimSpace(fmt.Sprintf("%s%s", usage, defaultValueString))
|
2016-05-02 23:42:08 +00:00
|
|
|
|
2016-05-22 01:29:45 +00:00
|
|
|
return withEnvHint(flagStringSliceField(f, "EnvVars"),
|
|
|
|
fmt.Sprintf("%s\t%s", prefixedNames(f.Names(), placeholder), usageWithDefault))
|
2016-05-02 23:42:08 +00:00
|
|
|
}
|
|
|
|
|
2016-05-22 19:20:52 +00:00
|
|
|
func stringifyIntSliceFlag(f *IntSliceFlag) string {
|
2016-05-02 23:42:08 +00:00
|
|
|
defaultVals := []string{}
|
|
|
|
if f.Value != nil && len(f.Value.Value()) > 0 {
|
|
|
|
for _, i := range f.Value.Value() {
|
|
|
|
defaultVals = append(defaultVals, fmt.Sprintf("%d", i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 13:54:54 +00:00
|
|
|
return stringifySliceFlag(f.Usage, f.Names(), defaultVals)
|
2016-05-02 23:42:08 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 13:54:54 +00:00
|
|
|
func stringifyInt64SliceFlag(f *Int64SliceFlag) string {
|
2016-06-12 04:54:33 +00:00
|
|
|
defaultVals := []string{}
|
2016-05-02 23:42:08 +00:00
|
|
|
if f.Value != nil && len(f.Value.Value()) > 0 {
|
|
|
|
for _, i := range f.Value.Value() {
|
|
|
|
defaultVals = append(defaultVals, fmt.Sprintf("%d", i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 13:54:54 +00:00
|
|
|
return stringifySliceFlag(f.Usage, f.Names(), defaultVals)
|
2016-05-02 23:42:08 +00:00
|
|
|
}
|
|
|
|
|
2016-06-29 02:52:25 +00:00
|
|
|
func stringifyFloat64SliceFlag(f *Float64SliceFlag) string {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-05-22 19:20:52 +00:00
|
|
|
func stringifyStringSliceFlag(f *StringSliceFlag) string {
|
2016-05-02 23:42:08 +00:00
|
|
|
defaultVals := []string{}
|
|
|
|
if f.Value != nil && len(f.Value.Value()) > 0 {
|
|
|
|
for _, s := range f.Value.Value() {
|
|
|
|
if len(s) > 0 {
|
|
|
|
defaultVals = append(defaultVals, fmt.Sprintf("%q", s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 13:54:54 +00:00
|
|
|
return stringifySliceFlag(f.Usage, f.Names(), defaultVals)
|
2016-05-02 23:42:08 +00:00
|
|
|
}
|
|
|
|
|
2016-05-22 01:29:45 +00:00
|
|
|
func stringifySliceFlag(usage string, names, defaultVals []string) string {
|
2016-05-02 23:42:08 +00:00
|
|
|
placeholder, usage := unquoteUsage(usage)
|
|
|
|
if placeholder == "" {
|
|
|
|
placeholder = defaultPlaceholder
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultVal := ""
|
|
|
|
if len(defaultVals) > 0 {
|
|
|
|
defaultVal = fmt.Sprintf(" (default: %s)", strings.Join(defaultVals, ", "))
|
|
|
|
}
|
|
|
|
|
|
|
|
usageWithDefault := strings.TrimSpace(fmt.Sprintf("%s%s", usage, defaultVal))
|
2016-05-22 01:29:45 +00:00
|
|
|
return fmt.Sprintf("%s\t%s", prefixedNames(names, placeholder), usageWithDefault)
|
|
|
|
}
|
2016-05-22 19:20:52 +00:00
|
|
|
|
|
|
|
func hasFlag(flags []Flag, fl Flag) bool {
|
|
|
|
for _, existing := range flags {
|
|
|
|
if fl == existing {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2016-05-02 17:05:21 +00:00
|
|
|
}
|