summaryrefslogtreecommitdiff
path: root/cli_args.go
diff options
context:
space:
mode:
authorJakob Unterwurzacher2021-08-10 18:24:35 +0200
committerJakob Unterwurzacher2021-08-10 18:24:35 +0200
commitf53f52b0464f747a370618bcdf152fad585f1eb5 (patch)
tree703120fe50b8527460c4b29da39f8105cf68a005 /cli_args.go
parent8c9a1c1121581a0d2a7e99d4ddd0fc22d8be20ed (diff)
main: switch from flag to pflag
Need support for flags at any position for https://github.com/rfjakob/gocryptfs/issues/590
Diffstat (limited to 'cli_args.go')
-rw-r--r--cli_args.go29
1 files changed, 26 insertions, 3 deletions
diff --git a/cli_args.go b/cli_args.go
index ce15448..8018f99 100644
--- a/cli_args.go
+++ b/cli_args.go
@@ -6,7 +6,6 @@ package main
import _ "github.com/rfjakob/gocryptfs/internal/ensurefds012"
import (
- "flag"
"fmt"
"net"
"os"
@@ -15,6 +14,8 @@ import (
"strings"
"time"
+ flag "github.com/spf13/pflag"
+
"github.com/hanwen/go-fuse/v2/fuse"
"github.com/rfjakob/gocryptfs/internal/configfile"
@@ -71,6 +72,10 @@ func (s *multipleStrings) Empty() bool {
return len(s2) == 0
}
+func (s *multipleStrings) Type() string {
+ return "multipleStrings"
+}
+
var flagSet *flag.FlagSet
// prefixOArgs transform options passed via "-o foo,bar" into regular options
@@ -123,16 +128,34 @@ func prefixOArgs(osArgs []string) ([]string, error) {
return newArgs, nil
}
+// convertToDoubleDash converts args like "-debug" (Go stdlib `flag` style)
+// into "--debug" (spf13/pflag style).
+// gocryptfs v2.1 switched from `flag` to `pflag`, but we obviously want to stay
+// cli-compatible, and this is the hack to do it.
+func convertToDoubleDash(osArgs []string) (out []string) {
+ out = append(out, osArgs...)
+ for i, v := range out {
+ if v == "-h" {
+ continue
+ }
+ if len(v) >= 2 && v[0] == '-' && v[1] != '-' {
+ out[i] = "-" + out[i]
+ }
+ }
+ return out
+}
+
// parseCliOpts - parse command line options (i.e. arguments that start with "-")
func parseCliOpts() (args argContainer) {
var err error
var opensslAuto string
- os.Args, err = prefixOArgs(os.Args)
+ osArgsPreprocessed, err := prefixOArgs(os.Args)
if err != nil {
tlog.Fatal.Println(err)
os.Exit(exitcodes.Usage)
}
+ osArgsPreprocessed = convertToDoubleDash(osArgsPreprocessed)
flagSet = flag.NewFlagSet(tlog.ProgramName, flag.ContinueOnError)
flagSet.Usage = func() {}
@@ -222,7 +245,7 @@ func parseCliOpts() (args argContainer) {
var dummyString string
flagSet.StringVar(&dummyString, "o", "", "For compatibility with mount(1), options can be also passed as a comma-separated list to -o on the end.")
// Actual parsing
- err = flagSet.Parse(os.Args[1:])
+ err = flagSet.Parse(osArgsPreprocessed[1:])
if err == flag.ErrHelp {
helpShort()
os.Exit(0)