2013-07-15 02:16:30 +00:00
|
|
|
package cli
|
|
|
|
|
2013-12-07 15:10:04 +00:00
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2014-07-11 17:29:56 +00:00
|
|
|
"os"
|
2016-05-01 12:36:17 +00:00
|
|
|
"reflect"
|
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"
|
|
|
|
|
2014-04-12 13:32:53 +00:00
|
|
|
// This flag enables bash-completion for all commands and subcommands
|
2014-07-11 22:13:10 +00:00
|
|
|
var BashCompletionFlag = BoolFlag{
|
2016-05-01 12:36:17 +00:00
|
|
|
Name: "generate-bash-completion",
|
|
|
|
Hidden: true,
|
2014-07-11 22:13:10 +00:00
|
|
|
}
|
2014-04-12 13:32:53 +00:00
|
|
|
|
2014-04-29 14:21:19 +00:00
|
|
|
// This flag prints the version for the application
|
2014-07-11 22:13:10 +00:00
|
|
|
var VersionFlag = BoolFlag{
|
|
|
|
Name: "version, v",
|
|
|
|
Usage: "print the version",
|
|
|
|
}
|
2014-04-29 14:21:19 +00:00
|
|
|
|
|
|
|
// This flag prints the help for all commands and subcommands
|
2014-12-02 04:20:21 +00:00
|
|
|
// Set to the zero value (BoolFlag{}) to disable flag -- keeps subcommand
|
|
|
|
// unless HideHelp is set to true)
|
2014-07-11 22:13:10 +00:00
|
|
|
var HelpFlag = BoolFlag{
|
|
|
|
Name: "help, h",
|
|
|
|
Usage: "show help",
|
|
|
|
}
|
2014-04-29 14:21:19 +00:00
|
|
|
|
2016-05-02 17:07:57 +00:00
|
|
|
var FlagStringer FlagStringFunc = stringifyFlag
|
2016-05-02 17:05:21 +00:00
|
|
|
|
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)
|
2015-12-15 17:29:03 +00:00
|
|
|
GetName() 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
|
|
|
}
|
|
|
|
|
2013-11-20 08:05:18 +00:00
|
|
|
func eachName(longName string, fn func(string)) {
|
|
|
|
parts := strings.Split(longName, ",")
|
|
|
|
for _, name := range parts {
|
|
|
|
name = strings.Trim(name, " ")
|
|
|
|
fn(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenericFlag is the flag type for types implementing Generic
|
|
|
|
type GenericFlag struct {
|
2014-07-11 17:29:56 +00:00
|
|
|
Name string
|
|
|
|
Value Generic
|
|
|
|
Usage string
|
|
|
|
EnvVar string
|
2016-05-01 12:36:17 +00:00
|
|
|
Hidden bool
|
2014-04-15 14:16:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 18:58:34 +00:00
|
|
|
// String returns the string representation of the generic flag to display the
|
|
|
|
// help text to the user (uses the String() method of the generic flag to show
|
|
|
|
// the value)
|
2014-04-15 14:16:47 +00:00
|
|
|
func (f GenericFlag) String() string {
|
2016-05-02 17:07:57 +00:00
|
|
|
return FlagStringer(f)
|
2016-01-22 14:08:27 +00:00
|
|
|
}
|
|
|
|
|
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
|
2014-04-15 14:16:47 +00:00
|
|
|
func (f GenericFlag) Apply(set *flag.FlagSet) {
|
2014-07-11 17:29:56 +00:00
|
|
|
val := f.Value
|
|
|
|
if f.EnvVar != "" {
|
2014-09-23 03:24:08 +00:00
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") {
|
|
|
|
envVar = strings.TrimSpace(envVar)
|
|
|
|
if envVal := os.Getenv(envVar); envVal != "" {
|
|
|
|
val.Set(envVal)
|
|
|
|
break
|
|
|
|
}
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-15 14:16:47 +00:00
|
|
|
eachName(f.Name, func(name string) {
|
|
|
|
set.Var(f.Value, name, f.Usage)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-12-15 17:29:03 +00:00
|
|
|
func (f GenericFlag) GetName() string {
|
2014-04-15 14:16:47 +00:00
|
|
|
return f.Name
|
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// StringSlice is an opaque type for []string to satisfy flag.Value
|
2013-09-24 01:41:31 +00:00
|
|
|
type StringSlice []string
|
|
|
|
|
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 {
|
|
|
|
*f = append(*f, 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 {
|
|
|
|
return fmt.Sprintf("%s", *f)
|
2013-09-24 01:41:31 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return *f
|
2013-09-24 01:41:31 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// StringSlice is a string flag that can be specified multiple times on the
|
|
|
|
// command-line
|
2013-09-24 01:41:31 +00:00
|
|
|
type StringSliceFlag struct {
|
2014-07-11 17:29:56 +00:00
|
|
|
Name string
|
|
|
|
Value *StringSlice
|
|
|
|
Usage string
|
|
|
|
EnvVar string
|
2016-05-01 12:36:17 +00:00
|
|
|
Hidden bool
|
2013-09-24 01:41:31 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// String returns the usage
|
2013-09-24 01:41:31 +00:00
|
|
|
func (f StringSliceFlag) String() string {
|
2016-05-02 17:07:57 +00:00
|
|
|
return FlagStringer(f)
|
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
|
2013-09-24 01:41:31 +00:00
|
|
|
func (f StringSliceFlag) Apply(set *flag.FlagSet) {
|
2014-07-11 17:29:56 +00:00
|
|
|
if f.EnvVar != "" {
|
2014-09-23 03:24:08 +00:00
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") {
|
|
|
|
envVar = strings.TrimSpace(envVar)
|
|
|
|
if envVal := os.Getenv(envVar); envVal != "" {
|
|
|
|
newVal := &StringSlice{}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-20 08:05:18 +00:00
|
|
|
eachName(f.Name, func(name string) {
|
2015-05-31 20:50:23 +00:00
|
|
|
if f.Value == nil {
|
|
|
|
f.Value = &StringSlice{}
|
|
|
|
}
|
2013-11-18 23:35:23 +00:00
|
|
|
set.Var(f.Value, name, f.Usage)
|
2013-11-20 08:05:18 +00:00
|
|
|
})
|
2013-11-18 23:35:23 +00:00
|
|
|
}
|
|
|
|
|
2015-12-15 17:29:03 +00:00
|
|
|
func (f StringSliceFlag) GetName() string {
|
2013-11-18 23:35:23 +00:00
|
|
|
return f.Name
|
2013-09-24 01:41:31 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// StringSlice is an opaque type for []int to satisfy flag.Value
|
2013-09-24 19:36:01 +00:00
|
|
|
type IntSlice []int
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Set parses the value into an integer and appends it to the list of values
|
2013-09-24 19:36:01 +00:00
|
|
|
func (f *IntSlice) Set(value string) error {
|
|
|
|
tmp, err := strconv.Atoi(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
*f = append(*f, tmp)
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
return fmt.Sprintf("%d", *f)
|
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Value returns the slice of ints set by this flag
|
2013-09-24 19:36:01 +00:00
|
|
|
func (f *IntSlice) Value() []int {
|
|
|
|
return *f
|
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// IntSliceFlag is an int flag that can be specified multiple times on the
|
|
|
|
// command-line
|
2013-09-24 19:36:01 +00:00
|
|
|
type IntSliceFlag struct {
|
2014-07-11 17:29:56 +00:00
|
|
|
Name string
|
|
|
|
Value *IntSlice
|
|
|
|
Usage string
|
|
|
|
EnvVar string
|
2016-05-01 12:36:17 +00:00
|
|
|
Hidden bool
|
2013-09-24 19:36:01 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// String returns the usage
|
2013-09-24 19:36:01 +00:00
|
|
|
func (f IntSliceFlag) String() string {
|
2016-05-02 17:07:57 +00:00
|
|
|
return FlagStringer(f)
|
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
|
2013-09-24 19:36:01 +00:00
|
|
|
func (f IntSliceFlag) Apply(set *flag.FlagSet) {
|
2014-07-11 17:29:56 +00:00
|
|
|
if f.EnvVar != "" {
|
2014-09-23 03:24:08 +00:00
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") {
|
|
|
|
envVar = strings.TrimSpace(envVar)
|
|
|
|
if envVal := os.Getenv(envVar); envVal != "" {
|
|
|
|
newVal := &IntSlice{}
|
|
|
|
for _, s := range strings.Split(envVal, ",") {
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
err := newVal.Set(s)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-20 08:05:18 +00:00
|
|
|
eachName(f.Name, func(name string) {
|
2015-05-31 20:50:23 +00:00
|
|
|
if f.Value == nil {
|
|
|
|
f.Value = &IntSlice{}
|
|
|
|
}
|
2013-11-18 23:35:23 +00:00
|
|
|
set.Var(f.Value, name, f.Usage)
|
2013-11-20 08:05:18 +00:00
|
|
|
})
|
2013-11-18 23:35:23 +00:00
|
|
|
}
|
|
|
|
|
2015-12-15 17:29:03 +00:00
|
|
|
func (f IntSliceFlag) GetName() string {
|
2013-11-18 23:35:23 +00:00
|
|
|
return f.Name
|
2013-09-24 19:36:01 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// BoolFlag is a switch that defaults to false
|
2013-07-15 02:16:30 +00:00
|
|
|
type BoolFlag struct {
|
2015-11-14 19:01:15 +00:00
|
|
|
Name string
|
|
|
|
Usage string
|
|
|
|
EnvVar string
|
|
|
|
Destination *bool
|
2016-05-01 12:36:17 +00:00
|
|
|
Hidden bool
|
2013-07-19 00:39:42 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// String returns a readable representation of this value (for usage defaults)
|
2013-07-19 00:39:42 +00:00
|
|
|
func (f BoolFlag) String() string {
|
2016-05-02 17:07:57 +00:00
|
|
|
return FlagStringer(f)
|
2013-07-19 00:39:42 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2013-07-19 00:39:42 +00:00
|
|
|
func (f BoolFlag) Apply(set *flag.FlagSet) {
|
2014-07-11 17:29:56 +00:00
|
|
|
val := false
|
|
|
|
if f.EnvVar != "" {
|
2014-09-23 03:24:08 +00:00
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") {
|
|
|
|
envVar = strings.TrimSpace(envVar)
|
|
|
|
if envVal := os.Getenv(envVar); envVal != "" {
|
|
|
|
envValBool, err := strconv.ParseBool(envVal)
|
|
|
|
if err == nil {
|
|
|
|
val = envValBool
|
|
|
|
}
|
|
|
|
break
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-20 08:05:18 +00:00
|
|
|
eachName(f.Name, func(name string) {
|
2015-11-14 19:01:15 +00:00
|
|
|
if f.Destination != nil {
|
|
|
|
set.BoolVar(f.Destination, name, val, f.Usage)
|
|
|
|
return
|
|
|
|
}
|
2014-07-11 17:29:56 +00:00
|
|
|
set.Bool(name, val, f.Usage)
|
2013-11-20 08:05:18 +00:00
|
|
|
})
|
2013-11-18 23:35:23 +00:00
|
|
|
}
|
|
|
|
|
2015-12-15 17:29:03 +00:00
|
|
|
func (f BoolFlag) GetName() string {
|
2013-11-18 23:35:23 +00:00
|
|
|
return f.Name
|
2013-07-15 02:16:30 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// BoolTFlag this represents a boolean flag that is true by default, but can
|
|
|
|
// still be set to false by --some-flag=false
|
2014-03-06 01:24:22 +00:00
|
|
|
type BoolTFlag struct {
|
2015-11-14 21:39:38 +00:00
|
|
|
Name string
|
|
|
|
Usage string
|
|
|
|
EnvVar string
|
|
|
|
Destination *bool
|
2016-05-01 12:36:17 +00:00
|
|
|
Hidden bool
|
2014-03-06 01:24:22 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// String returns a readable representation of this value (for usage defaults)
|
2014-03-06 01:24:22 +00:00
|
|
|
func (f BoolTFlag) String() string {
|
2016-05-02 17:07:57 +00:00
|
|
|
return FlagStringer(f)
|
2014-03-06 01:24:22 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2014-03-06 01:24:22 +00:00
|
|
|
func (f BoolTFlag) Apply(set *flag.FlagSet) {
|
2014-07-11 17:29:56 +00:00
|
|
|
val := true
|
|
|
|
if f.EnvVar != "" {
|
2014-09-23 03:24:08 +00:00
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") {
|
|
|
|
envVar = strings.TrimSpace(envVar)
|
|
|
|
if envVal := os.Getenv(envVar); envVal != "" {
|
|
|
|
envValBool, err := strconv.ParseBool(envVal)
|
|
|
|
if err == nil {
|
|
|
|
val = envValBool
|
|
|
|
break
|
|
|
|
}
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-06 01:24:22 +00:00
|
|
|
eachName(f.Name, func(name string) {
|
2015-11-14 21:39:38 +00:00
|
|
|
if f.Destination != nil {
|
|
|
|
set.BoolVar(f.Destination, name, val, f.Usage)
|
|
|
|
return
|
|
|
|
}
|
2014-07-11 17:29:56 +00:00
|
|
|
set.Bool(name, val, f.Usage)
|
2014-03-06 01:24:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-12-15 17:29:03 +00:00
|
|
|
func (f BoolTFlag) GetName() string {
|
2014-03-06 01:24:22 +00:00
|
|
|
return f.Name
|
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// StringFlag represents a flag that takes as string value
|
2013-07-15 02:16:30 +00:00
|
|
|
type StringFlag struct {
|
2015-11-14 19:01:15 +00:00
|
|
|
Name string
|
|
|
|
Value string
|
|
|
|
Usage string
|
|
|
|
EnvVar string
|
|
|
|
Destination *string
|
2016-05-01 12:36:17 +00:00
|
|
|
Hidden bool
|
2013-07-15 02:16:30 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// String returns the usage
|
2013-07-15 02:16:30 +00:00
|
|
|
func (f StringFlag) String() string {
|
2016-05-02 17:07:57 +00:00
|
|
|
return FlagStringer(f)
|
2016-01-22 14:08:27 +00:00
|
|
|
}
|
2014-04-11 19:09:59 +00:00
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2013-07-17 02:59:04 +00:00
|
|
|
func (f StringFlag) Apply(set *flag.FlagSet) {
|
2014-07-11 17:29:56 +00:00
|
|
|
if f.EnvVar != "" {
|
2014-09-23 03:24:08 +00:00
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") {
|
|
|
|
envVar = strings.TrimSpace(envVar)
|
|
|
|
if envVal := os.Getenv(envVar); envVal != "" {
|
|
|
|
f.Value = envVal
|
|
|
|
break
|
|
|
|
}
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-20 08:05:18 +00:00
|
|
|
eachName(f.Name, func(name string) {
|
2015-11-14 19:01:15 +00:00
|
|
|
if f.Destination != nil {
|
|
|
|
set.StringVar(f.Destination, name, f.Value, f.Usage)
|
|
|
|
return
|
|
|
|
}
|
2013-11-18 23:35:23 +00:00
|
|
|
set.String(name, f.Value, f.Usage)
|
2013-11-20 08:05:18 +00:00
|
|
|
})
|
2013-11-18 23:35:23 +00:00
|
|
|
}
|
|
|
|
|
2015-12-15 17:29:03 +00:00
|
|
|
func (f StringFlag) GetName() string {
|
2013-11-18 23:35:23 +00:00
|
|
|
return f.Name
|
2013-07-17 02:59:04 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// IntFlag is a flag that takes an integer
|
|
|
|
// Errors if the value provided cannot be parsed
|
2013-07-19 00:39:42 +00:00
|
|
|
type IntFlag struct {
|
2015-11-14 19:01:15 +00:00
|
|
|
Name string
|
|
|
|
Value int
|
|
|
|
Usage string
|
|
|
|
EnvVar string
|
|
|
|
Destination *int
|
2016-05-01 12:36:17 +00:00
|
|
|
Hidden bool
|
2013-07-15 02:16:30 +00:00
|
|
|
}
|
2013-07-17 02:59:04 +00:00
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// String returns the usage
|
2013-07-19 00:39:42 +00:00
|
|
|
func (f IntFlag) String() string {
|
2016-05-02 17:07:57 +00:00
|
|
|
return FlagStringer(f)
|
2013-07-17 02:59:04 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2013-07-19 00:39:42 +00:00
|
|
|
func (f IntFlag) Apply(set *flag.FlagSet) {
|
2014-07-11 17:29:56 +00:00
|
|
|
if f.EnvVar != "" {
|
2014-09-23 03:24:08 +00:00
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") {
|
|
|
|
envVar = strings.TrimSpace(envVar)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-20 08:05:18 +00:00
|
|
|
eachName(f.Name, func(name string) {
|
2015-11-14 19:01:15 +00:00
|
|
|
if f.Destination != nil {
|
|
|
|
set.IntVar(f.Destination, name, f.Value, f.Usage)
|
|
|
|
return
|
|
|
|
}
|
2013-11-18 23:35:23 +00:00
|
|
|
set.Int(name, f.Value, f.Usage)
|
2013-11-20 08:05:18 +00:00
|
|
|
})
|
2013-11-18 23:35:23 +00:00
|
|
|
}
|
|
|
|
|
2015-12-15 17:29:03 +00:00
|
|
|
func (f IntFlag) GetName() string {
|
2013-11-18 23:35:23 +00:00
|
|
|
return f.Name
|
2013-07-17 02:59:04 +00:00
|
|
|
}
|
2013-07-24 14:35:45 +00:00
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// DurationFlag is a flag that takes a duration specified in Go's duration
|
|
|
|
// format: https://golang.org/pkg/time/#ParseDuration
|
2014-08-02 21:32:32 +00:00
|
|
|
type DurationFlag struct {
|
2015-11-14 19:01:15 +00:00
|
|
|
Name string
|
|
|
|
Value time.Duration
|
|
|
|
Usage string
|
|
|
|
EnvVar string
|
|
|
|
Destination *time.Duration
|
2016-05-01 12:36:17 +00:00
|
|
|
Hidden bool
|
2014-08-02 21:32:32 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// String returns a readable representation of this value (for usage defaults)
|
2014-08-02 21:32:32 +00:00
|
|
|
func (f DurationFlag) String() string {
|
2016-05-02 17:07:57 +00:00
|
|
|
return FlagStringer(f)
|
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
|
2014-08-02 21:32:32 +00:00
|
|
|
func (f DurationFlag) Apply(set *flag.FlagSet) {
|
|
|
|
if f.EnvVar != "" {
|
2014-09-23 03:24:08 +00:00
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") {
|
|
|
|
envVar = strings.TrimSpace(envVar)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
eachName(f.Name, func(name string) {
|
2015-11-14 19:01:15 +00:00
|
|
|
if f.Destination != nil {
|
|
|
|
set.DurationVar(f.Destination, name, f.Value, f.Usage)
|
|
|
|
return
|
|
|
|
}
|
2014-08-02 21:32:32 +00:00
|
|
|
set.Duration(name, f.Value, f.Usage)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-12-15 17:29:03 +00:00
|
|
|
func (f DurationFlag) GetName() string {
|
2014-08-02 21:32:32 +00:00
|
|
|
return f.Name
|
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Float64Flag is a flag that takes an float value
|
|
|
|
// Errors if the value provided cannot be parsed
|
2013-12-03 13:42:09 +00:00
|
|
|
type Float64Flag struct {
|
2015-11-14 19:01:15 +00:00
|
|
|
Name string
|
|
|
|
Value float64
|
|
|
|
Usage string
|
|
|
|
EnvVar string
|
|
|
|
Destination *float64
|
2016-05-01 12:36:17 +00:00
|
|
|
Hidden bool
|
2013-12-03 13:42:09 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// String returns the usage
|
2013-12-03 13:42:09 +00:00
|
|
|
func (f Float64Flag) String() string {
|
2016-05-02 17:07:57 +00:00
|
|
|
return FlagStringer(f)
|
2013-12-03 13:42:09 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 03:51:09 +00:00
|
|
|
// Apply populates the flag given the flag set and environment
|
2013-12-03 13:42:09 +00:00
|
|
|
func (f Float64Flag) Apply(set *flag.FlagSet) {
|
2014-07-11 17:29:56 +00:00
|
|
|
if f.EnvVar != "" {
|
2014-09-23 03:24:08 +00:00
|
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") {
|
|
|
|
envVar = strings.TrimSpace(envVar)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-03 13:42:09 +00:00
|
|
|
eachName(f.Name, func(name string) {
|
2015-11-14 19:01:15 +00:00
|
|
|
if f.Destination != nil {
|
|
|
|
set.Float64Var(f.Destination, name, f.Value, f.Usage)
|
|
|
|
return
|
|
|
|
}
|
2013-12-03 13:42:09 +00:00
|
|
|
set.Float64(name, f.Value, f.Usage)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-12-15 17:29:03 +00:00
|
|
|
func (f Float64Flag) GetName() string {
|
2013-12-03 13:42:09 +00:00
|
|
|
return f.Name
|
|
|
|
}
|
|
|
|
|
2016-05-01 12:36:17 +00:00
|
|
|
func visibleFlags(fl []Flag) []Flag {
|
|
|
|
visible := []Flag{}
|
|
|
|
for _, flag := range fl {
|
|
|
|
if !reflect.ValueOf(flag).FieldByName("Hidden").Bool() {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func prefixedNames(fullName, placeholder string) string {
|
|
|
|
var prefixed string
|
2013-11-20 08:05:18 +00:00
|
|
|
parts := strings.Split(fullName, ",")
|
|
|
|
for i, name := range parts {
|
|
|
|
name = strings.Trim(name, " ")
|
|
|
|
prefixed += prefixFor(name) + name
|
2016-04-22 03:02:06 +00:00
|
|
|
if placeholder != "" {
|
|
|
|
prefixed += " " + placeholder
|
|
|
|
}
|
2013-11-21 01:25:13 +00:00
|
|
|
if i < len(parts)-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
|
|
|
|
|
|
|
func withEnvHint(envVar, str string) string {
|
|
|
|
envText := ""
|
|
|
|
if envVar != "" {
|
2016-01-22 14:08:27 +00:00
|
|
|
prefix := "$"
|
|
|
|
suffix := ""
|
|
|
|
sep := ", $"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
prefix = "%"
|
|
|
|
suffix = "%"
|
|
|
|
sep = "%, %"
|
|
|
|
}
|
|
|
|
envText = fmt.Sprintf(" [%s%s%s]", prefix, strings.Join(strings.Split(envVar, ","), sep), suffix)
|
2014-07-11 17:29:56 +00:00
|
|
|
}
|
|
|
|
return str + envText
|
|
|
|
}
|
2016-05-02 17:05:21 +00:00
|
|
|
|
2016-05-02 17:07:57 +00:00
|
|
|
func stringifyFlag(f Flag) string {
|
2016-05-02 17:05:21 +00:00
|
|
|
fv := reflect.ValueOf(f)
|
2016-05-02 23:42:08 +00:00
|
|
|
|
|
|
|
switch f.(type) {
|
|
|
|
case IntSliceFlag:
|
|
|
|
return withEnvHint(fv.FieldByName("EnvVar").String(),
|
|
|
|
stringifyIntSliceFlag(f.(IntSliceFlag)))
|
|
|
|
case StringSliceFlag:
|
|
|
|
return withEnvHint(fv.FieldByName("EnvVar").String(),
|
|
|
|
stringifyStringSliceFlag(f.(StringSliceFlag)))
|
|
|
|
}
|
|
|
|
|
2016-05-02 17:05:21 +00:00
|
|
|
placeholder, usage := unquoteUsage(fv.FieldByName("Usage").String())
|
2016-05-02 23:42:08 +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")
|
|
|
|
|
|
|
|
valString := ""
|
|
|
|
if val.IsValid() {
|
|
|
|
needsPlaceholder = true
|
|
|
|
if val.Kind() == reflect.String && val.String() != "" {
|
|
|
|
valString = fmt.Sprintf("%q", val.String())
|
|
|
|
} else {
|
|
|
|
valString = fmt.Sprintf("%v", val.Interface())
|
|
|
|
}
|
2016-05-02 17:05:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 23:42:08 +00:00
|
|
|
if valString != "" {
|
|
|
|
defaultValueString = valString
|
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:42:08 +00:00
|
|
|
formattedDefaultValueString := ""
|
|
|
|
if defaultValueString != "" {
|
|
|
|
formattedDefaultValueString = fmt.Sprintf(" (default: %v)", defaultValueString)
|
|
|
|
}
|
|
|
|
|
|
|
|
usageWithDefault := strings.TrimSpace(fmt.Sprintf("%s%s", usage, formattedDefaultValueString))
|
|
|
|
|
2016-05-02 17:05:21 +00:00
|
|
|
return withEnvHint(fv.FieldByName("EnvVar").String(),
|
2016-05-02 23:42:08 +00:00
|
|
|
fmt.Sprintf("%s\t%s", prefixedNames(fv.FieldByName("Name").String(), placeholder), usageWithDefault))
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringifyIntSliceFlag(f IntSliceFlag) string {
|
|
|
|
defaultVals := []string{}
|
|
|
|
if f.Value != nil && len(f.Value.Value()) > 0 {
|
|
|
|
for _, i := range f.Value.Value() {
|
|
|
|
defaultVals = append(defaultVals, fmt.Sprintf("%d", i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringifySliceFlag(f.Usage, f.Name, defaultVals)
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringifyStringSliceFlag(f StringSliceFlag) string {
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringifySliceFlag(f.Usage, f.Name, defaultVals)
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringifySliceFlag(usage, name string, defaultVals []string) string {
|
|
|
|
placeholder, usage := unquoteUsage(usage)
|
|
|
|
if placeholder == "" {
|
|
|
|
placeholder = defaultPlaceholder
|
|
|
|
}
|
|
|
|
|
|
|
|
nameParts := []string{}
|
|
|
|
for _, part := range strings.Split(name, ",") {
|
|
|
|
nameParts = append(nameParts, strings.TrimSpace(part))
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultVal := ""
|
|
|
|
if len(defaultVals) > 0 {
|
|
|
|
defaultVal = fmt.Sprintf(" (default: %s)", strings.Join(defaultVals, ", "))
|
|
|
|
}
|
|
|
|
|
|
|
|
usageWithDefault := strings.TrimSpace(fmt.Sprintf("%s%s", usage, defaultVal))
|
|
|
|
|
|
|
|
if len(nameParts) < 2 {
|
|
|
|
return fmt.Sprintf("%s%s %s\t%s", prefixFor(nameParts[0]), nameParts[0],
|
|
|
|
placeholder, usageWithDefault)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s%s %s, %s%s %s\t%s", prefixFor(nameParts[0]), nameParts[0],
|
|
|
|
placeholder, prefixFor(nameParts[1]), nameParts[1],
|
|
|
|
placeholder, usageWithDefault)
|
2016-05-02 17:05:21 +00:00
|
|
|
}
|