Skip to content

Commit

Permalink
main: accept "-o" at the front AND at the end
Browse files Browse the repository at this point in the history
Moving "-o" to the end broke a third-party app, SiriKali.
Breaking your users is bad, so let's accept "-o" anywhere.
  • Loading branch information
rfjakob committed Oct 10, 2016
1 parent 828f718 commit 7b2049c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
35 changes: 26 additions & 9 deletions cli_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
11 changes: 10 additions & 1 deletion cli_args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,24 @@ func TestPrefixOArgs(t *testing.T) {
i: []string{"gocryptfs", "foo", "bar", "-o", "a,b,xxxxx"},
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)
}
}
}

0 comments on commit 7b2049c

Please sign in to comment.