mirror of
https://github.com/rclone/rclone.git
synced 2024-11-25 09:41:44 +08:00
flags: factor AddFlagsFromOptions from cmd
This is in preparation for generalising the backend config
This commit is contained in:
parent
671a15f65f
commit
c6ab37a59f
35
cmd/cmd.go
35
cmd/cmd.go
|
@ -522,41 +522,12 @@ var backendFlags map[string]struct{}
|
||||||
func AddBackendFlags() {
|
func AddBackendFlags() {
|
||||||
backendFlags = map[string]struct{}{}
|
backendFlags = map[string]struct{}{}
|
||||||
for _, fsInfo := range fs.Registry {
|
for _, fsInfo := range fs.Registry {
|
||||||
done := map[string]struct{}{}
|
flags.AddFlagsFromOptions(pflag.CommandLine, fsInfo.Prefix, fsInfo.Options)
|
||||||
|
// Store the backend flag names for the help generator
|
||||||
for i := range fsInfo.Options {
|
for i := range fsInfo.Options {
|
||||||
opt := &fsInfo.Options[i]
|
opt := &fsInfo.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
|
|
||||||
name := opt.FlagName(fsInfo.Prefix)
|
name := opt.FlagName(fsInfo.Prefix)
|
||||||
found := pflag.CommandLine.Lookup(name) != nil
|
backendFlags[name] = struct{}{}
|
||||||
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 := pflag.CommandLine.VarPF(opt, name, opt.ShortOpt, help)
|
|
||||||
flags.SetDefaultFromEnv(pflag.CommandLine, name)
|
|
||||||
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
|
|
||||||
}
|
|
||||||
backendFlags[name] = struct{}{}
|
|
||||||
} else {
|
|
||||||
fs.Errorf(nil, "Not adding duplicate flag --%s", name)
|
|
||||||
}
|
|
||||||
// flag.Hidden = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,7 +151,7 @@ func installFlag(flags *pflag.FlagSet, name string, groupsString string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add flag to Group if it is a global flag
|
// Add flag to Group if it is a global flag
|
||||||
if flags == pflag.CommandLine {
|
if groupsString != "" && flags == pflag.CommandLine {
|
||||||
for _, groupName := range strings.Split(groupsString, ",") {
|
for _, groupName := range strings.Split(groupsString, ",") {
|
||||||
if groupName == "rc-" {
|
if groupName == "rc-" {
|
||||||
groupName = "RC"
|
groupName = "RC"
|
||||||
|
@ -165,24 +165,6 @@ func installFlag(flags *pflag.FlagSet, name string, groupsString string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetDefaultFromEnv constructs a name from the flag passed in and
|
|
||||||
// sets the default from the environment if possible
|
|
||||||
//
|
|
||||||
// Used to create backend flags like --skip-links
|
|
||||||
func SetDefaultFromEnv(flags *pflag.FlagSet, name string) {
|
|
||||||
envKey := fs.OptionToEnv(name)
|
|
||||||
envValue, found := os.LookupEnv(envKey)
|
|
||||||
if found {
|
|
||||||
flag := flags.Lookup(name)
|
|
||||||
if flag == nil {
|
|
||||||
log.Fatalf("Couldn't find flag --%q", name)
|
|
||||||
}
|
|
||||||
fs.Debugf(nil, "Setting default for %s=%q from environment variable %s", name, envValue, envKey)
|
|
||||||
//err = tempValue.Set()
|
|
||||||
flag.DefValue = envValue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// StringP defines a flag which can be set by an environment variable
|
// StringP defines a flag which can be set by an environment variable
|
||||||
//
|
//
|
||||||
// It is a thin wrapper around pflag.StringP
|
// It is a thin wrapper around pflag.StringP
|
||||||
|
@ -350,3 +332,45 @@ func CountVarP(flags *pflag.FlagSet, p *int, name, shorthand string, usage strin
|
||||||
flags.CountVarP(p, name, shorthand, usage)
|
flags.CountVarP(p, name, shorthand, usage)
|
||||||
installFlag(flags, name, groups)
|
installFlag(flags, name, groups)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user