rc: add -o/--opt and -a/--arg for more structured input

This commit is contained in:
Nick Craig-Wood 2020-04-28 12:36:39 +01:00
parent 37a53570d4
commit 1f61027f51

View File

@ -27,6 +27,8 @@ var (
authUser = "" authUser = ""
authPass = "" authPass = ""
loopback = false loopback = false
options []string
arguments []string
) )
func init() { func init() {
@ -38,6 +40,8 @@ func init() {
flags.StringVarP(cmdFlags, &authUser, "user", "", "", "Username to use to rclone remote control.") flags.StringVarP(cmdFlags, &authUser, "user", "", "", "Username to use to rclone remote control.")
flags.StringVarP(cmdFlags, &authPass, "pass", "", "", "Password to use to connect to rclone remote control.") flags.StringVarP(cmdFlags, &authPass, "pass", "", "", "Password to use to connect to rclone remote control.")
flags.BoolVarP(cmdFlags, &loopback, "loopback", "", false, "If set connect to this rclone instance not via HTTP.") flags.BoolVarP(cmdFlags, &loopback, "loopback", "", false, "If set connect to this rclone instance not via HTTP.")
flags.StringArrayVarP(cmdFlags, &options, "opt", "o", options, "Option in the form name=value or name placed in the \"opt\" array.")
flags.StringArrayVarP(cmdFlags, &arguments, "arg", "a", arguments, "Argument placed in the \"arg\" array.")
} }
var commandDefinition = &cobra.Command{ var commandDefinition = &cobra.Command{
@ -63,6 +67,29 @@ The --json parameter can be used to pass in a JSON blob as an input
instead of key=value arguments. This is the only way of passing in instead of key=value arguments. This is the only way of passing in
more complicated values. more complicated values.
The -o/--opt option can be used to set a key "opt" with key, value
options in the form "-o key=value" or "-o key". It can be repeated as
many times as required. This is useful for rc commands which take the
"opt" parameter which by convention is a dictionary of strings.
-o key=value -o key2
Will place this in the "opt" value
{"key":"value", "key2","")
The -a/--arg option can be used to set strings in the "arg" value. It
can be repeated as many times as required. This is useful for rc
commands which take the "arg" parameter which by convention is a list
of strings.
-a value -a value2
Will place this in the "arg" value
["value", "value2"]
Use --loopback to connect to the rclone instance running "rclone rc". Use --loopback to connect to the rclone instance running "rclone rc".
This is very useful for testing commands without having to run an This is very useful for testing commands without having to run an
rclone rc server, eg: rclone rc server, eg:
@ -103,6 +130,23 @@ func parseFlags() {
} }
} }
// ParseOptions parses a slice of options in the form key=value or key
// into a map
func ParseOptions(options []string) (opt map[string]string) {
opt = make(map[string]string, len(options))
for _, option := range options {
equals := strings.IndexRune(option, '=')
key := option
value := ""
if equals >= 0 {
key = option[:equals]
value = option[equals+1:]
}
opt[key] = value
}
return opt
}
// If the user set flagName set the output to its value // If the user set flagName set the output to its value
func setAlternateFlag(flagName string, output *string) { func setAlternateFlag(flagName string, output *string) {
if rcFlag := pflag.Lookup(flagName); rcFlag != nil && rcFlag.Changed { if rcFlag := pflag.Lookup(flagName); rcFlag != nil && rcFlag.Changed {
@ -210,6 +254,12 @@ func run(ctx context.Context, args []string) (err error) {
return errors.Wrap(err, "bad --json input") return errors.Wrap(err, "bad --json input")
} }
} }
if len(options) > 0 {
in["opt"] = ParseOptions(options)
}
if len(arguments) > 0 {
in["arg"] = arguments
}
// Do the call // Do the call
out, callErr := doCall(ctx, path, in) out, callErr := doCall(ctx, path, in)