summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Unterwurzacher2016-10-10 19:44:34 +0200
committerJakob Unterwurzacher2016-10-10 19:44:34 +0200
commit7b2049c7698325a6a0ef965b6fb7a2fa94e3329b (patch)
treec07aecad09a6f3b137f584428224db2548513f50
parent828f7184838be17d04ff4d78d8b066f4947406f3 (diff)
main: accept "-o" at the front AND at the end
Moving "-o" to the end broke a third-party app, SiriKali. Breaking your users is bad, so let's accept "-o" anywhere.
-rw-r--r--cli_args.go35
-rw-r--r--cli_args_test.go11
2 files changed, 36 insertions, 10 deletions
diff --git a/cli_args.go b/cli_args.go
index 9ba5496..ef1ca09 100644
--- a/cli_args.go
+++ b/cli_args.go
@@ -32,22 +32,39 @@ var flagSet *flag.FlagSet
// prefixOArgs transform options passed via "-o foo,bar" into regular options
// like "-foo -bar" and prefixes them to the command line.
func prefixOArgs(osArgs []string) []string {
- l := len(osArgs)
// Need at least 3, example: gocryptfs -o foo,bar
- if l < 3 {
+ if len(osArgs) < 3 {
return osArgs
}
- if osArgs[l-2] != "-o" {
- return osArgs
+ // Find and extract "-o foo,bar"
+ var otherArgs, oOpts []string
+ for i := 1; i < len(osArgs); i++ {
+ if osArgs[i] == "-o" {
+ // Last argument?
+ if i+1 >= len(osArgs) {
+ tlog.Fatal.Printf("The \"-o\" option requires an argument")
+ os.Exit(ErrExitUsage)
+ }
+ oOpts = strings.Split(osArgs[i+1], ",")
+ // Skip over the arguments to "-o"
+ i++
+ } else if strings.HasPrefix(osArgs[i], "-o=") {
+ oOpts = strings.Split(osArgs[i][3:], ",")
+ } else {
+ otherArgs = append(otherArgs, osArgs[i])
+ }
}
- oOpts := strings.Split(osArgs[l-1], ",")
- osArgs = osArgs[:l-2]
+ // Start with program name
newArgs := []string{osArgs[0]}
// Add options from "-o"
- for _, a := range oOpts {
- newArgs = append(newArgs, "-"+a)
+ for _, o := range oOpts {
+ if o == "" {
+ continue
+ }
+ newArgs = append(newArgs, "-"+o)
}
- newArgs = append(newArgs, osArgs[1:]...)
+ // Add other arguments
+ newArgs = append(newArgs, otherArgs...)
return newArgs
}
diff --git a/cli_args_test.go b/cli_args_test.go
index 844a0b1..e1ed435 100644
--- a/cli_args_test.go
+++ b/cli_args_test.go
@@ -39,14 +39,23 @@ func TestPrefixOArgs(t *testing.T) {
o: []string{"gocryptfs", "-a", "-b", "-xxxxx", "foo", "bar"},
},
testcase{
+ i: []string{"gocryptfs", "foo", "bar", "-d", "-o=a,b,xxxxx"},
+ o: []string{"gocryptfs", "-a", "-b", "-xxxxx", "foo", "bar", "-d"},
+ },
+ testcase{
i: []string{"gocryptfs", "foo", "bar", "-oooo", "a,b,xxxxx"},
o: []string{"gocryptfs", "foo", "bar", "-oooo", "a,b,xxxxx"},
},
+ // https://github.com/mhogomchungu/sirikali/blob/a36d91d3e39f0c1eb9a79680ed6c28ddb6568fa8/src/siritask.cpp#L192
+ testcase{
+ i: []string{"gocryptfs", "-o", "rw", "--config", "fff", "ccc", "mmm"},
+ o: []string{"gocryptfs", "-rw", "--config", "fff", "ccc", "mmm"},
+ },
}
for _, tc := range testcases {
o := prefixOArgs(tc.i)
if !reflect.DeepEqual(o, tc.o) {
- t.Errorf("\n i=%q\nwant=%q\n got=%q", tc.i, tc.o, o)
+ t.Errorf("\n in=%q\nwant=%q\n got=%q", tc.i, tc.o, o)
}
}
}