2019-04-30 14:06:24 +02:00
|
|
|
// Package flags contains enhanced versions of spf13/pflag flag
|
2018-01-12 17:30:54 +01:00
|
|
|
// routines which will read from the environment also.
|
|
|
|
package flags
|
2016-12-20 16:50:46 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
2023-07-10 19:34:10 +02:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2016-12-20 16:50:46 +01:00
|
|
|
"time"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
2016-12-20 16:50:46 +01:00
|
|
|
"github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
2023-07-10 19:34:10 +02:00
|
|
|
// Groups of Flags
|
|
|
|
type Groups struct {
|
|
|
|
// Groups of flags
|
|
|
|
Groups []*Group
|
|
|
|
|
|
|
|
// GroupsMaps maps a group name to a Group
|
|
|
|
ByName map[string]*Group
|
|
|
|
}
|
|
|
|
|
|
|
|
// Group related flags together for documentation purposes
|
|
|
|
type Group struct {
|
|
|
|
Groups *Groups
|
|
|
|
Name string
|
|
|
|
Help string
|
|
|
|
Flags *pflag.FlagSet
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGroups constructs a collection of Groups
|
|
|
|
func NewGroups() *Groups {
|
|
|
|
return &Groups{
|
|
|
|
ByName: make(map[string]*Group),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGroup to create Group
|
|
|
|
func (gs *Groups) NewGroup(name, help string) *Group {
|
|
|
|
group := &Group{
|
|
|
|
Name: name,
|
|
|
|
Help: help,
|
|
|
|
Flags: pflag.NewFlagSet(name, pflag.ExitOnError),
|
|
|
|
}
|
|
|
|
gs.ByName[group.Name] = group
|
|
|
|
gs.Groups = append(gs.Groups, group)
|
|
|
|
return group
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter makes a copy of groups filtered by flagsRe
|
2023-11-03 23:35:48 +01:00
|
|
|
func (gs *Groups) Filter(group string, filterRe *regexp.Regexp, filterNamesOnly bool) *Groups {
|
2023-07-10 19:34:10 +02:00
|
|
|
newGs := NewGroups()
|
|
|
|
for _, g := range gs.Groups {
|
2023-11-03 23:35:48 +01:00
|
|
|
if group == "" || strings.EqualFold(g.Name, group) {
|
|
|
|
newG := newGs.NewGroup(g.Name, g.Help)
|
|
|
|
g.Flags.VisitAll(func(f *pflag.Flag) {
|
|
|
|
if filterRe == nil || filterRe.MatchString(f.Name) || (!filterNamesOnly && filterRe.MatchString(f.Usage)) {
|
|
|
|
newG.Flags.AddFlag(f)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2023-07-10 19:34:10 +02:00
|
|
|
}
|
|
|
|
return newGs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Include makes a copy of groups only including the ones in the filter string
|
|
|
|
func (gs *Groups) Include(groupsString string) *Groups {
|
|
|
|
if groupsString == "" {
|
|
|
|
return gs
|
|
|
|
}
|
|
|
|
want := map[string]bool{}
|
|
|
|
for _, groupName := range strings.Split(groupsString, ",") {
|
|
|
|
_, ok := All.ByName[groupName]
|
|
|
|
if !ok {
|
|
|
|
log.Fatalf("Couldn't find group %q in command annotation", groupName)
|
|
|
|
}
|
|
|
|
want[groupName] = true
|
|
|
|
}
|
|
|
|
newGs := NewGroups()
|
|
|
|
for _, g := range gs.Groups {
|
|
|
|
if !want[g.Name] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newG := newGs.NewGroup(g.Name, g.Help)
|
|
|
|
newG.Flags = g.Flags
|
|
|
|
}
|
|
|
|
return newGs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a new flag to the Group
|
|
|
|
func (g *Group) Add(flag *pflag.Flag) {
|
|
|
|
g.Flags.AddFlag(flag)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllRegistered returns all flags in a group
|
|
|
|
func (gs *Groups) AllRegistered() map[*pflag.Flag]struct{} {
|
|
|
|
out := make(map[*pflag.Flag]struct{})
|
|
|
|
for _, g := range gs.Groups {
|
|
|
|
g.Flags.VisitAll(func(f *pflag.Flag) {
|
|
|
|
out[f] = struct{}{}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2023-08-29 14:02:34 +02:00
|
|
|
// All is the global stats Groups
|
2023-07-10 19:34:10 +02:00
|
|
|
var All *Groups
|
|
|
|
|
|
|
|
// Groups of flags for documentation purposes
|
|
|
|
func init() {
|
|
|
|
All = NewGroups()
|
2024-08-10 16:50:20 +02:00
|
|
|
All.NewGroup("Copy", "Flags for anything which can copy a file")
|
|
|
|
All.NewGroup("Sync", "Flags used for sync commands")
|
|
|
|
All.NewGroup("Important", "Important flags useful for most commands")
|
|
|
|
All.NewGroup("Check", "Flags used for check commands")
|
|
|
|
All.NewGroup("Networking", "Flags for general networking and HTTP stuff")
|
|
|
|
All.NewGroup("Performance", "Flags helpful for increasing performance")
|
|
|
|
All.NewGroup("Config", "Flags for general configuration of rclone")
|
|
|
|
All.NewGroup("Debugging", "Flags for developers")
|
|
|
|
All.NewGroup("Filter", "Flags for filtering directory listings")
|
|
|
|
All.NewGroup("Listing", "Flags for listing directories")
|
|
|
|
All.NewGroup("Logging", "Flags for logging and statistics")
|
|
|
|
All.NewGroup("Metadata", "Flags to control metadata")
|
|
|
|
All.NewGroup("RC", "Flags to control the Remote Control API")
|
2024-09-06 16:00:36 +02:00
|
|
|
All.NewGroup("Metrics", "Flags to control the Metrics HTTP endpoint.")
|
2023-07-10 19:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// installFlag constructs a name from the flag passed in and
|
2021-05-20 14:08:01 +02:00
|
|
|
// sets the value and default from the environment if possible
|
|
|
|
// the value may be overridden when the command line is parsed
|
|
|
|
//
|
2023-07-10 19:34:10 +02:00
|
|
|
// Used to create non-backend flags like --stats.
|
|
|
|
//
|
|
|
|
// It also adds the flag to the groups passed in.
|
|
|
|
func installFlag(flags *pflag.FlagSet, name string, groupsString string) {
|
|
|
|
// Find flag
|
|
|
|
flag := flags.Lookup(name)
|
|
|
|
if flag == nil {
|
|
|
|
log.Fatalf("Couldn't find flag --%q", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read default from environment if possible
|
2021-05-20 14:08:01 +02:00
|
|
|
envKey := fs.OptionToEnv(name)
|
2023-07-10 19:34:10 +02:00
|
|
|
if envValue, envFound := os.LookupEnv(envKey); envFound {
|
2021-05-20 14:08:01 +02:00
|
|
|
err := flags.Set(name, envValue)
|
2016-12-20 16:50:46 +01:00
|
|
|
if err != nil {
|
2021-05-20 14:08:01 +02:00
|
|
|
log.Fatalf("Invalid value when setting --%s from environment variable %s=%q: %v", name, envKey, envValue, err)
|
2016-12-20 16:50:46 +01:00
|
|
|
}
|
2021-05-20 14:08:01 +02:00
|
|
|
fs.Debugf(nil, "Setting --%s %q from environment variable %s=%q", name, flag.Value, envKey, envValue)
|
|
|
|
flag.DefValue = envValue
|
2016-12-20 16:50:46 +01:00
|
|
|
}
|
2023-07-10 19:34:10 +02:00
|
|
|
|
|
|
|
// Add flag to Group if it is a global flag
|
2024-07-01 19:09:58 +02:00
|
|
|
if groupsString != "" && flags == pflag.CommandLine {
|
2023-07-10 19:34:10 +02:00
|
|
|
for _, groupName := range strings.Split(groupsString, ",") {
|
|
|
|
if groupName == "rc-" {
|
|
|
|
groupName = "RC"
|
|
|
|
}
|
|
|
|
group, ok := All.ByName[groupName]
|
|
|
|
if !ok {
|
|
|
|
log.Fatalf("Couldn't find group %q for flag --%s", groupName, name)
|
|
|
|
}
|
|
|
|
group.Add(flag)
|
|
|
|
}
|
|
|
|
}
|
2016-12-20 16:50:46 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// StringP defines a flag which can be set by an environment variable
|
2016-12-20 16:50:46 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.StringP
|
2023-07-10 19:34:10 +02:00
|
|
|
func StringP(name, shorthand string, value string, usage string, groups string) (out *string) {
|
2016-12-20 16:50:46 +01:00
|
|
|
out = pflag.StringP(name, shorthand, value, usage)
|
2023-07-10 19:34:10 +02:00
|
|
|
installFlag(pflag.CommandLine, name, groups)
|
2016-12-20 16:50:46 +01:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// StringVarP defines a flag which can be set by an environment variable
|
2017-11-07 18:09:08 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.StringVarP
|
2023-07-10 19:34:10 +02:00
|
|
|
func StringVarP(flags *pflag.FlagSet, p *string, name, shorthand string, value string, usage string, groups string) {
|
2017-11-07 18:09:08 +01:00
|
|
|
flags.StringVarP(p, name, shorthand, value, usage)
|
2023-07-10 19:34:10 +02:00
|
|
|
installFlag(flags, name, groups)
|
2017-11-07 18:09:08 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// BoolP defines a flag which can be set by an environment variable
|
2016-12-20 16:50:46 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.BoolP
|
2023-07-10 19:34:10 +02:00
|
|
|
func BoolP(name, shorthand string, value bool, usage string, groups string) (out *bool) {
|
2016-12-20 16:50:46 +01:00
|
|
|
out = pflag.BoolP(name, shorthand, value, usage)
|
2023-07-10 19:34:10 +02:00
|
|
|
installFlag(pflag.CommandLine, name, groups)
|
2016-12-20 16:50:46 +01:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// BoolVarP defines a flag which can be set by an environment variable
|
2017-11-07 18:09:08 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.BoolVarP
|
2023-07-10 19:34:10 +02:00
|
|
|
func BoolVarP(flags *pflag.FlagSet, p *bool, name, shorthand string, value bool, usage string, groups string) {
|
2017-11-07 18:09:08 +01:00
|
|
|
flags.BoolVarP(p, name, shorthand, value, usage)
|
2023-07-10 19:34:10 +02:00
|
|
|
installFlag(flags, name, groups)
|
2017-11-07 18:09:08 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// IntP defines a flag which can be set by an environment variable
|
2016-12-20 16:50:46 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.IntP
|
2023-07-10 19:34:10 +02:00
|
|
|
func IntP(name, shorthand string, value int, usage string, groups string) (out *int) {
|
2016-12-20 16:50:46 +01:00
|
|
|
out = pflag.IntP(name, shorthand, value, usage)
|
2023-07-10 19:34:10 +02:00
|
|
|
installFlag(pflag.CommandLine, name, groups)
|
2016-12-20 16:50:46 +01:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// Int64P defines a flag which can be set by an environment variable
|
2018-01-12 17:30:54 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.IntP
|
2023-07-10 19:34:10 +02:00
|
|
|
func Int64P(name, shorthand string, value int64, usage string, groups string) (out *int64) {
|
2018-01-12 17:30:54 +01:00
|
|
|
out = pflag.Int64P(name, shorthand, value, usage)
|
2023-07-10 19:34:10 +02:00
|
|
|
installFlag(pflag.CommandLine, name, groups)
|
2018-01-12 17:30:54 +01:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// Int64VarP defines a flag which can be set by an environment variable
|
2018-01-22 19:53:18 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.Int64VarP
|
2023-07-10 19:34:10 +02:00
|
|
|
func Int64VarP(flags *pflag.FlagSet, p *int64, name, shorthand string, value int64, usage string, groups string) {
|
2018-01-22 19:53:18 +01:00
|
|
|
flags.Int64VarP(p, name, shorthand, value, usage)
|
2023-07-10 19:34:10 +02:00
|
|
|
installFlag(flags, name, groups)
|
2018-01-22 19:53:18 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// IntVarP defines a flag which can be set by an environment variable
|
2017-11-07 18:09:08 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.IntVarP
|
2023-07-10 19:34:10 +02:00
|
|
|
func IntVarP(flags *pflag.FlagSet, p *int, name, shorthand string, value int, usage string, groups string) {
|
2017-11-07 18:09:08 +01:00
|
|
|
flags.IntVarP(p, name, shorthand, value, usage)
|
2023-07-10 19:34:10 +02:00
|
|
|
installFlag(flags, name, groups)
|
2017-11-07 18:09:08 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// Uint32VarP defines a flag which can be set by an environment variable
|
2017-11-07 18:09:08 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.Uint32VarP
|
2023-07-10 19:34:10 +02:00
|
|
|
func Uint32VarP(flags *pflag.FlagSet, p *uint32, name, shorthand string, value uint32, usage string, groups string) {
|
2017-11-07 18:09:08 +01:00
|
|
|
flags.Uint32VarP(p, name, shorthand, value, usage)
|
2023-07-10 19:34:10 +02:00
|
|
|
installFlag(flags, name, groups)
|
2017-11-07 18:09:08 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// Float64P defines a flag which can be set by an environment variable
|
2017-07-13 06:11:24 +02:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.Float64P
|
2023-07-10 19:34:10 +02:00
|
|
|
func Float64P(name, shorthand string, value float64, usage string, groups string) (out *float64) {
|
2017-07-13 06:11:24 +02:00
|
|
|
out = pflag.Float64P(name, shorthand, value, usage)
|
2023-07-10 19:34:10 +02:00
|
|
|
installFlag(pflag.CommandLine, name, groups)
|
2017-07-13 06:11:24 +02:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// Float64VarP defines a flag which can be set by an environment variable
|
2017-11-07 18:09:08 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.Float64VarP
|
2023-07-10 19:34:10 +02:00
|
|
|
func Float64VarP(flags *pflag.FlagSet, p *float64, name, shorthand string, value float64, usage string, groups string) {
|
2017-11-07 18:09:08 +01:00
|
|
|
flags.Float64VarP(p, name, shorthand, value, usage)
|
2023-07-10 19:34:10 +02:00
|
|
|
installFlag(flags, name, groups)
|
2017-11-07 18:09:08 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// DurationP defines a flag which can be set by an environment variable
|
2016-12-20 16:50:46 +01:00
|
|
|
//
|
2022-11-10 18:28:12 +01:00
|
|
|
// It wraps the duration in an fs.Duration for extra suffixes and
|
|
|
|
// passes it to pflag.VarP
|
2023-07-10 19:34:10 +02:00
|
|
|
func DurationP(name, shorthand string, value time.Duration, usage string, groups string) (out *time.Duration) {
|
2022-11-10 18:28:12 +01:00
|
|
|
out = new(time.Duration)
|
|
|
|
*out = value
|
|
|
|
pflag.VarP((*fs.Duration)(out), name, shorthand, usage)
|
2023-07-10 19:34:10 +02:00
|
|
|
installFlag(pflag.CommandLine, name, groups)
|
2016-12-20 16:50:46 +01:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// DurationVarP defines a flag which can be set by an environment variable
|
2017-11-07 18:09:08 +01:00
|
|
|
//
|
2022-11-10 18:28:12 +01:00
|
|
|
// It wraps the duration in an fs.Duration for extra suffixes and
|
|
|
|
// passes it to pflag.VarP
|
2023-07-10 19:34:10 +02:00
|
|
|
func DurationVarP(flags *pflag.FlagSet, p *time.Duration, name, shorthand string, value time.Duration, usage string, groups string) {
|
2022-11-10 18:28:12 +01:00
|
|
|
flags.VarP((*fs.Duration)(p), name, shorthand, usage)
|
2023-07-10 19:34:10 +02:00
|
|
|
installFlag(flags, name, groups)
|
2017-11-07 18:09:08 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// VarP defines a flag which can be set by an environment variable
|
2016-12-20 16:50:46 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.VarP
|
2023-07-10 19:34:10 +02:00
|
|
|
func VarP(value pflag.Value, name, shorthand, usage string, groups string) {
|
2016-12-20 16:50:46 +01:00
|
|
|
pflag.VarP(value, name, shorthand, usage)
|
2023-07-10 19:34:10 +02:00
|
|
|
installFlag(pflag.CommandLine, name, groups)
|
2016-12-20 16:50:46 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// FVarP defines a flag which can be set by an environment variable
|
2017-11-07 18:09:08 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.VarP
|
2023-07-10 19:34:10 +02:00
|
|
|
func FVarP(flags *pflag.FlagSet, value pflag.Value, name, shorthand, usage string, groups string) {
|
2017-11-07 18:09:08 +01:00
|
|
|
flags.VarP(value, name, shorthand, usage)
|
2023-07-10 19:34:10 +02:00
|
|
|
installFlag(flags, name, groups)
|
2020-10-07 13:29:03 +02:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// StringArrayP defines a flag which can be set by an environment variable
|
2016-12-20 16:50:46 +01:00
|
|
|
//
|
|
|
|
// It sets one value only - command line flags can be used to set more.
|
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.StringArrayP
|
2023-07-10 19:34:10 +02:00
|
|
|
func StringArrayP(name, shorthand string, value []string, usage string, groups string) (out *[]string) {
|
2016-12-20 16:50:46 +01:00
|
|
|
out = pflag.StringArrayP(name, shorthand, value, usage)
|
2023-07-10 19:34:10 +02:00
|
|
|
installFlag(pflag.CommandLine, name, groups)
|
2016-12-20 16:50:46 +01:00
|
|
|
return out
|
|
|
|
}
|
2017-02-09 22:22:46 +01:00
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// StringArrayVarP defines a flag which can be set by an environment variable
|
2017-11-07 18:09:08 +01:00
|
|
|
//
|
|
|
|
// It sets one value only - command line flags can be used to set more.
|
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.StringArrayVarP
|
2023-07-10 19:34:10 +02:00
|
|
|
func StringArrayVarP(flags *pflag.FlagSet, p *[]string, name, shorthand string, value []string, usage string, groups string) {
|
2017-11-07 18:09:08 +01:00
|
|
|
flags.StringArrayVarP(p, name, shorthand, value, usage)
|
2023-07-10 19:34:10 +02:00
|
|
|
installFlag(flags, name, groups)
|
2017-11-07 18:09:08 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// CountP defines a flag which can be set by an environment variable
|
2017-02-09 22:22:46 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.CountP
|
2023-07-10 19:34:10 +02:00
|
|
|
func CountP(name, shorthand string, usage string, groups string) (out *int) {
|
2017-02-09 22:22:46 +01:00
|
|
|
out = pflag.CountP(name, shorthand, usage)
|
2023-07-10 19:34:10 +02:00
|
|
|
installFlag(pflag.CommandLine, name, groups)
|
2017-02-09 22:22:46 +01:00
|
|
|
return out
|
|
|
|
}
|
2017-11-07 18:09:08 +01:00
|
|
|
|
2021-05-20 14:08:01 +02:00
|
|
|
// CountVarP defines a flag which can be set by an environment variable
|
2017-11-07 18:09:08 +01:00
|
|
|
//
|
|
|
|
// It is a thin wrapper around pflag.CountVarP
|
2023-07-10 19:34:10 +02:00
|
|
|
func CountVarP(flags *pflag.FlagSet, p *int, name, shorthand string, usage string, groups string) {
|
2017-11-07 18:09:08 +01:00
|
|
|
flags.CountVarP(p, name, shorthand, usage)
|
2023-07-10 19:34:10 +02:00
|
|
|
installFlag(flags, name, groups)
|
2017-11-07 18:09:08 +01:00
|
|
|
}
|
2024-07-01 19:09:58 +02:00
|
|
|
|
|
|
|
// AddFlagsFromOptions takes a slice of fs.Option and adds flags for all of them
|
|
|
|
func AddFlagsFromOptions(flags *pflag.FlagSet, prefix string, options fs.Options) {
|
|
|
|
done := map[string]struct{}{}
|
|
|
|
for i := range options {
|
|
|
|
opt := &options[i]
|
|
|
|
// Skip if done already (e.g. with Provider options)
|
|
|
|
if _, doneAlready := done[opt.Name]; doneAlready {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
done[opt.Name] = struct{}{}
|
|
|
|
// Make a flag from each option
|
|
|
|
if prefix == "" {
|
|
|
|
opt.NoPrefix = true
|
|
|
|
}
|
|
|
|
name := opt.FlagName(prefix)
|
|
|
|
found := flags.Lookup(name) != nil
|
|
|
|
if !found {
|
|
|
|
// Take first line of help only
|
|
|
|
help := strings.TrimSpace(opt.Help)
|
|
|
|
if nl := strings.IndexRune(help, '\n'); nl >= 0 {
|
|
|
|
help = help[:nl]
|
|
|
|
}
|
|
|
|
help = strings.TrimRight(strings.TrimSpace(help), ".!?")
|
|
|
|
if opt.IsPassword {
|
|
|
|
help += " (obscured)"
|
|
|
|
}
|
|
|
|
flag := flags.VarPF(opt, name, opt.ShortOpt, help)
|
|
|
|
installFlag(flags, name, opt.Groups)
|
|
|
|
if _, isBool := opt.Default.(bool); isBool {
|
|
|
|
flag.NoOptDefVal = "true"
|
|
|
|
}
|
|
|
|
// Hide on the command line if requested
|
|
|
|
if opt.Hide&fs.OptionHideCommandLine != 0 {
|
|
|
|
flag.Hidden = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fs.Errorf(nil, "Not adding duplicate flag --%s", name)
|
|
|
|
}
|
|
|
|
// flag.Hidden = true
|
|
|
|
}
|
|
|
|
}
|