From f53f52b0464f747a370618bcdf152fad585f1eb5 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Tue, 10 Aug 2021 18:24:35 +0200 Subject: main: switch from flag to pflag Need support for flags at any position for https://github.com/rfjakob/gocryptfs/issues/590 --- cli_args.go | 29 ++++++++++++++++++++++++++--- go.mod | 1 + go.sum | 2 ++ 3 files changed, 29 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) diff --git a/go.mod b/go.mod index f23fad4..2c91017 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/pkg/xattr v0.4.1 github.com/rfjakob/eme v1.1.1 github.com/sabhiram/go-gitignore v0.0.0-20180611051255-d3107576ba94 + github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.5.1 // indirect golang.org/x/crypto v0.0.0-20200429183012-4b2356b1ed79 golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect diff --git a/go.sum b/go.sum index ef4fe81..ac824b7 100644 --- a/go.sum +++ b/go.sum @@ -22,6 +22,8 @@ github.com/rfjakob/eme v1.1.1 h1:t+CgvcOn+eDvj2xdglxsSnkgg8LM8jwdxnV7OnsrTn0= github.com/rfjakob/eme v1.1.1/go.mod h1:U2bmx0hDj8EyDdcxmD5t3XHDnBFnyNNc22n1R4008eM= github.com/sabhiram/go-gitignore v0.0.0-20180611051255-d3107576ba94 h1:G04eS0JkAIVZfaJLjla9dNxkJCPiKIGZlw9AfOhzOD0= github.com/sabhiram/go-gitignore v0.0.0-20180611051255-d3107576ba94/go.mod h1:b18R55ulyQ/h3RaWyloPyER7fWQVZvimKKhnI5OfrJQ= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -- cgit v1.2.3