cmd: make backend env vars show in help as the defaults for backend flags

Before this change

    RCLONE_DRIVE_CHUNK_SIZE=111M rclone help flags | grep drive-chunk-size

Would show the default value, not the setting of RCLONE_DRIVE_CHUNK_SIZE
as the non backend flags do.

This change makes it work as expected by setting the default of the
option to the environment variable.

Fixes #4659
This commit is contained in:
Nick Craig-Wood 2020-10-07 12:29:03 +01:00
parent 70f92fd6b3
commit 72083c65ad
2 changed files with 10 additions and 1 deletions

View File

@ -513,7 +513,7 @@ func AddBackendFlags() {
if opt.IsPassword { if opt.IsPassword {
help += " (obscured)" help += " (obscured)"
} }
flag := pflag.CommandLine.VarPF(opt, name, opt.ShortOpt, help) flag := flags.VarPF(pflag.CommandLine, opt, name, opt.ShortOpt, help)
if _, isBool := opt.Default.(bool); isBool { if _, isBool := opt.Default.(bool); isBool {
flag.NoOptDefVal = "true" flag.NoOptDefVal = "true"
} }

View File

@ -156,6 +156,15 @@ func FVarP(flags *pflag.FlagSet, value pflag.Value, name, shorthand, usage strin
setDefaultFromEnv(flags, name) setDefaultFromEnv(flags, name)
} }
// VarPF defines a flag which can be overridden by an environment variable
//
// It is a thin wrapper around pflag.VarPF
func VarPF(flags *pflag.FlagSet, value pflag.Value, name, shorthand, usage string) *pflag.Flag {
flag := flags.VarPF(value, name, shorthand, usage)
setDefaultFromEnv(flags, name)
return flag
}
// StringArrayP defines a flag which can be overridden by an environment variable // StringArrayP defines a flag which can be overridden by an environment variable
// //
// It sets one value only - command line flags can be used to set more. // It sets one value only - command line flags can be used to set more.