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.
This commit is contained in:
Jakob Unterwurzacher 2016-10-10 19:44:34 +02:00
parent 828f718483
commit 7b2049c769
2 changed files with 36 additions and 10 deletions

View File

@ -32,22 +32,39 @@ var flagSet *flag.FlagSet
// prefixOArgs transform options passed via "-o foo,bar" into regular options // prefixOArgs transform options passed via "-o foo,bar" into regular options
// like "-foo -bar" and prefixes them to the command line. // like "-foo -bar" and prefixes them to the command line.
func prefixOArgs(osArgs []string) []string { func prefixOArgs(osArgs []string) []string {
l := len(osArgs)
// Need at least 3, example: gocryptfs -o foo,bar // Need at least 3, example: gocryptfs -o foo,bar
if l < 3 { if len(osArgs) < 3 {
return osArgs return osArgs
} }
if osArgs[l-2] != "-o" { // Find and extract "-o foo,bar"
return osArgs 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[l-1], ",") oOpts = strings.Split(osArgs[i+1], ",")
osArgs = osArgs[:l-2] // 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])
}
}
// Start with program name
newArgs := []string{osArgs[0]} newArgs := []string{osArgs[0]}
// Add options from "-o" // Add options from "-o"
for _, a := range oOpts { for _, o := range oOpts {
newArgs = append(newArgs, "-"+a) if o == "" {
continue
} }
newArgs = append(newArgs, osArgs[1:]...) newArgs = append(newArgs, "-"+o)
}
// Add other arguments
newArgs = append(newArgs, otherArgs...)
return newArgs return newArgs
} }

View File

@ -38,15 +38,24 @@ func TestPrefixOArgs(t *testing.T) {
i: []string{"gocryptfs", "foo", "bar", "-o", "a,b,xxxxx"}, i: []string{"gocryptfs", "foo", "bar", "-o", "a,b,xxxxx"},
o: []string{"gocryptfs", "-a", "-b", "-xxxxx", "foo", "bar"}, 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{ testcase{
i: []string{"gocryptfs", "foo", "bar", "-oooo", "a,b,xxxxx"}, i: []string{"gocryptfs", "foo", "bar", "-oooo", "a,b,xxxxx"},
o: []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 { for _, tc := range testcases {
o := prefixOArgs(tc.i) o := prefixOArgs(tc.i)
if !reflect.DeepEqual(o, tc.o) { 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)
} }
} }
} }