summaryrefslogtreecommitdiff
path: root/cli_args.go
diff options
context:
space:
mode:
authorJakob Unterwurzacher2016-10-10 19:44:34 +0200
committerJakob Unterwurzacher2016-10-10 19:44:34 +0200
commit7b2049c7698325a6a0ef965b6fb7a2fa94e3329b (patch)
treec07aecad09a6f3b137f584428224db2548513f50 /cli_args.go
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.
Diffstat (limited to 'cli_args.go')
-rw-r--r--cli_args.go35
1 files changed, 26 insertions, 9 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
}