From 8e35a4f0d4616f11c322e63dc3ad373fe3d25e0d Mon Sep 17 00:00:00 2001 From: Alex Flint Date: Sun, 31 Mar 2024 10:30:12 -0400 Subject: handle explicit empty placeholders --- parse.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'parse.go') diff --git a/parse.go b/parse.go index 0bdddc7..9f3e687 100644 --- a/parse.go +++ b/parse.go @@ -56,7 +56,7 @@ type spec struct { env string // the name of the environment variable for this option, or empty for none defaultValue reflect.Value // default value for this option defaultString string // default value for this option, in string form to be displayed in help text - placeholder string // name of the data in help + placeholder string // placeholder string in help } // command represents a named subcommand, or the top-level command @@ -341,9 +341,8 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) { spec.help = help } - // Look at the tag - var isSubcommand bool // tracks whether this field is a subcommand - + // process each comma-separated part of the tag + var isSubcommand bool for _, key := range strings.Split(tag, ",") { if key == "" { continue @@ -407,6 +406,7 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) { } } + // placeholder is the string used in the help text like this: "--somearg PLACEHOLDER" placeholder, hasPlaceholder := field.Tag.Lookup("placeholder") if hasPlaceholder { spec.placeholder = placeholder -- cgit v1.2.3 From 68948b2ac14c5cda057c88f1d7cba992ca053a2c Mon Sep 17 00:00:00 2001 From: Alex Flint Date: Sun, 31 Mar 2024 12:05:26 -0400 Subject: restore 100% code coverage --- parse.go | 7 ------- parse_test.go | 8 ++++++++ 2 files changed, 8 insertions(+), 7 deletions(-) (limited to 'parse.go') diff --git a/parse.go b/parse.go index 0bdddc7..c88faae 100644 --- a/parse.go +++ b/parse.go @@ -85,13 +85,6 @@ func MustParse(dest ...interface{}) *Parser { // mustParse is a helper that facilitates testing func mustParse(config Config, dest ...interface{}) *Parser { - if config.Exit == nil { - config.Exit = os.Exit - } - if config.Out == nil { - config.Out = os.Stdout - } - p, err := NewParser(config, dest...) if err != nil { fmt.Fprintln(config.Out, err) diff --git a/parse_test.go b/parse_test.go index 06e7a76..1512800 100644 --- a/parse_test.go +++ b/parse_test.go @@ -1735,3 +1735,11 @@ func TestSubcommandGlobalFlag_InCommand_Strict_Inner(t *testing.T) { assert.False(t, args.Global) assert.True(t, args.Sub.Guard) } + +func TestExitFunctionAndOutStreamGetFilledIn(t *testing.T) { + var args struct{} + p, err := NewParser(Config{}, &args) + require.NoError(t, err) + assert.NotNil(t, p.config.Exit) // go prohibits function pointer comparison + assert.Equal(t, p.config.Out, os.Stdout) +} -- cgit v1.2.3 From aa844c7de9f0314b1fe66b9bdcc12090c7d0905e Mon Sep 17 00:00:00 2001 From: Hugo Hromic Date: Thu, 27 Jun 2024 00:02:41 +0100 Subject: Fix crash on errors in package-level `MustParse` --- example_test.go | 7 +++++++ parse.go | 5 +++-- parse_test.go | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) (limited to 'parse.go') diff --git a/example_test.go b/example_test.go index 4bd7632..d3622bf 100644 --- a/example_test.go +++ b/example_test.go @@ -163,6 +163,7 @@ func Example_helpText() { // This is only necessary when running inside golang's runnable example harness mustParseExit = func(int) {} + mustParseOut = os.Stdout MustParse(&args) @@ -195,6 +196,7 @@ func Example_helpPlaceholder() { // This is only necessary when running inside golang's runnable example harness mustParseExit = func(int) {} + mustParseOut = os.Stdout MustParse(&args) @@ -235,6 +237,7 @@ func Example_helpTextWithSubcommand() { // This is only necessary when running inside golang's runnable example harness mustParseExit = func(int) {} + mustParseOut = os.Stdout MustParse(&args) @@ -272,6 +275,7 @@ func Example_helpTextWhenUsingSubcommand() { // This is only necessary when running inside golang's runnable example harness mustParseExit = func(int) {} + mustParseOut = os.Stdout MustParse(&args) @@ -392,6 +396,7 @@ func Example_errorText() { // This is only necessary when running inside golang's runnable example harness mustParseExit = func(int) {} + mustParseOut = os.Stdout MustParse(&args) @@ -415,6 +420,7 @@ func Example_errorTextForSubcommand() { // This is only necessary when running inside golang's runnable example harness mustParseExit = func(int) {} + mustParseOut = os.Stdout MustParse(&args) @@ -450,6 +456,7 @@ func Example_subcommand() { // This is only necessary when running inside golang's runnable example harness mustParseExit = func(int) {} + mustParseOut = os.Stdout MustParse(&args) diff --git a/parse.go b/parse.go index 251b005..98c21cd 100644 --- a/parse.go +++ b/parse.go @@ -76,12 +76,13 @@ var ErrHelp = errors.New("help requested by user") // ErrVersion indicates that the builtin --version was provided var ErrVersion = errors.New("version requested by user") -// for monkey patching in example code +// for monkey patching in example and test code var mustParseExit = os.Exit +var mustParseOut io.Writer = os.Stdout // MustParse processes command line arguments and exits upon failure func MustParse(dest ...interface{}) *Parser { - return mustParse(Config{Exit: mustParseExit}, dest...) + return mustParse(Config{Exit: mustParseExit, Out: mustParseOut}, dest...) } // mustParse is a helper that facilitates testing diff --git a/parse_test.go b/parse_test.go index fe055fe..07af7ed 100644 --- a/parse_test.go +++ b/parse_test.go @@ -692,6 +692,21 @@ func TestMustParse(t *testing.T) { assert.NotNil(t, parser) } +func TestMustParseError(t *testing.T) { + var args struct { + Foo []string `default:""` + } + var exitCode int + var stdout bytes.Buffer + mustParseExit = func(code int) { exitCode = code } + mustParseOut = &stdout + os.Args = []string{"example"} + parser := MustParse(&args) + assert.Nil(t, parser) + assert.Equal(t, -1, exitCode) + assert.Contains(t, stdout.String(), "default values are not supported for slice or map fields") +} + func TestEnvironmentVariable(t *testing.T) { var args struct { Foo string `arg:"env"` -- cgit v1.2.3 From a7c40c36a3a425dd1d28cbc97a3340aafb494d19 Mon Sep 17 00:00:00 2001 From: Hugo Hromic Date: Sat, 29 Jun 2024 15:42:05 +0100 Subject: Use standard exit status code for usage errors * The stdlib `flags` package and most command line utilities use status code `2`. --- parse.go | 2 +- parse_test.go | 6 +++--- usage.go | 8 ++++---- usage_test.go | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) (limited to 'parse.go') diff --git a/parse.go b/parse.go index 98c21cd..2bed8bf 100644 --- a/parse.go +++ b/parse.go @@ -90,7 +90,7 @@ func mustParse(config Config, dest ...interface{}) *Parser { p, err := NewParser(config, dest...) if err != nil { fmt.Fprintln(config.Out, err) - config.Exit(-1) + config.Exit(2) return nil } diff --git a/parse_test.go b/parse_test.go index 07af7ed..5bc781c 100644 --- a/parse_test.go +++ b/parse_test.go @@ -703,7 +703,7 @@ func TestMustParseError(t *testing.T) { os.Args = []string{"example"} parser := MustParse(&args) assert.Nil(t, parser) - assert.Equal(t, -1, exitCode) + assert.Equal(t, 2, exitCode) assert.Contains(t, stdout.String(), "default values are not supported for slice or map fields") } @@ -921,7 +921,7 @@ func TestParserMustParse(t *testing.T) { }{ {name: "help", args: struct{}{}, cmdLine: []string{"--help"}, code: 0, output: "display this help and exit"}, {name: "version", args: versioned{}, cmdLine: []string{"--version"}, code: 0, output: "example 3.2.1"}, - {name: "invalid", args: struct{}{}, cmdLine: []string{"invalid"}, code: -1, output: ""}, + {name: "invalid", args: struct{}{}, cmdLine: []string{"invalid"}, code: 2, output: ""}, } for _, tt := range tests { @@ -1571,7 +1571,7 @@ func TestMustParseInvalidParser(t *testing.T) { } parser := mustParse(Config{Out: &stdout, Exit: exit}, &args) assert.Nil(t, parser) - assert.Equal(t, -1, exitCode) + assert.Equal(t, 2, exitCode) } func TestMustParsePrintsHelp(t *testing.T) { diff --git a/usage.go b/usage.go index 6b578a5..f5e4b38 100644 --- a/usage.go +++ b/usage.go @@ -9,13 +9,13 @@ import ( // the width of the left column const colWidth = 25 -// Fail prints usage information to stderr and exits with non-zero status +// Fail prints usage information to p.Config.Out and exits with status code 2. func (p *Parser) Fail(msg string) { p.FailSubcommand(msg) } -// FailSubcommand prints usage information for a specified subcommand to stderr, -// then exits with non-zero status. To write usage information for a top-level +// FailSubcommand prints usage information for a specified subcommand to p.Config.Out, +// then exits with status code 2. To write usage information for a top-level // subcommand, provide just the name of that subcommand. To write usage // information for a subcommand that is nested under another subcommand, provide // a sequence of subcommand names starting with the top-level subcommand and so @@ -27,7 +27,7 @@ func (p *Parser) FailSubcommand(msg string, subcommand ...string) error { } fmt.Fprintln(p.config.Out, "error:", msg) - p.config.Exit(-1) + p.config.Exit(2) return nil } diff --git a/usage_test.go b/usage_test.go index b2bcab1..71324eb 100644 --- a/usage_test.go +++ b/usage_test.go @@ -738,7 +738,7 @@ error: something went wrong p.Fail("something went wrong") assert.Equal(t, expectedStdout[1:], stdout.String()) - assert.Equal(t, -1, exitCode) + assert.Equal(t, 2, exitCode) } func TestFailSubcommand(t *testing.T) { @@ -761,7 +761,7 @@ error: something went wrong require.NoError(t, err) assert.Equal(t, expectedStdout[1:], stdout.String()) - assert.Equal(t, -1, exitCode) + assert.Equal(t, 2, exitCode) } type lengthOf struct { -- cgit v1.2.3 From b13a62172a12a2b2f0cfd7eeed10d846845a5f77 Mon Sep 17 00:00:00 2001 From: Alex Flint Date: Thu, 5 Sep 2024 17:15:02 -0400 Subject: update api docs for Parser.Parse --- README.md | 14 ++++---------- parse.go | 9 ++++++++- 2 files changed, 12 insertions(+), 11 deletions(-) (limited to 'parse.go') diff --git a/README.md b/README.md index 9b8b071..761af56 100644 --- a/README.md +++ b/README.md @@ -582,7 +582,7 @@ if p.Subcommand() == nil { ``` -### Programmatic error handling +### Custom handling of --help and --version The following reproduces the internal logic of `MustParse` for the simple case where you are not using subcommands or --version. This allows you to respond @@ -625,9 +625,6 @@ Usage: ./example --something SOMETHING $ ./example error: --something is required Usage: ./example --something SOMETHING - -$ ./example --something abc -got "abc" ``` To also handle --version programatically, use the following: @@ -686,13 +683,10 @@ Usage: example --something SOMETHING $ ./example error: --something is required Usage: example --something SOMETHING - -$ ./example --something abc -got "abc" ``` -To also handle subcommands, use this most general version (also works in absence of subcommands but -is a bit more complex): +To generate subcommand-specific help messages, use the following most general version +(this also works in absence of subcommands but is a bit more complex): ```go type fetchCmd struct { @@ -761,7 +755,7 @@ Global options: ### API Documentation -https://godoc.org/github.com/alexflint/go-arg +https://pkg.go.dev/github.com/alexflint/go-arg ### Rationale diff --git a/parse.go b/parse.go index 2bed8bf..8f99a21 100644 --- a/parse.go +++ b/parse.go @@ -494,7 +494,14 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) { } // Parse processes the given command line option, storing the results in the field -// of the structs from which NewParser was constructed +// of the structs from which NewParser was constructed. +// +// It returns ErrHelp if "--help" is one of the command line args and ErrVersion if +// "--version" is one of the command line args (the latter only applies if the +// destination struct passed to NewParser implements Versioned.) +// +// To respond to --help and --version in the way that MustParse does, see examples +// in the README under "Custom handling of --help and --version". func (p *Parser) Parse(args []string) error { err := p.process(args) if err != nil { -- cgit v1.2.3 From 12fffac1d812461638322f8eed15b1b740e38040 Mon Sep 17 00:00:00 2001 From: Alex Flint Date: Thu, 5 Sep 2024 17:16:23 -0400 Subject: field -> fields --- parse.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'parse.go') diff --git a/parse.go b/parse.go index 8f99a21..172c7cd 100644 --- a/parse.go +++ b/parse.go @@ -493,7 +493,7 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) { return &cmd, nil } -// Parse processes the given command line option, storing the results in the field +// Parse processes the given command line option, storing the results in the fields // of the structs from which NewParser was constructed. // // It returns ErrHelp if "--help" is one of the command line args and ErrVersion if -- cgit v1.2.3 From 9b5c76b1c4eda926cbec2442dd24021653178b6b Mon Sep 17 00:00:00 2001 From: Hugo Hromic Date: Mon, 1 Jul 2024 23:07:32 +0100 Subject: Add support for setting a global env var prefix --- parse.go | 13 ++++++++----- parse_test.go | 52 +++++++++++++++++++++++++++++++--------------------- 2 files changed, 39 insertions(+), 26 deletions(-) (limited to 'parse.go') diff --git a/parse.go b/parse.go index 172c7cd..ece7dde 100644 --- a/parse.go +++ b/parse.go @@ -131,6 +131,9 @@ type Config struct { // subcommand StrictSubcommands bool + // EnvPrefix instructs the library to use a name prefix when reading environment variables. + EnvPrefix string + // Exit is called to terminate the process with an error code (defaults to os.Exit) Exit func(int) @@ -235,7 +238,7 @@ func NewParser(config Config, dests ...interface{}) (*Parser, error) { panic(fmt.Sprintf("%s is not a pointer (did you forget an ampersand?)", t)) } - cmd, err := cmdFromStruct(name, path{root: i}, t) + cmd, err := cmdFromStruct(name, path{root: i}, t, config.EnvPrefix) if err != nil { return nil, err } @@ -285,7 +288,7 @@ func NewParser(config Config, dests ...interface{}) (*Parser, error) { return &p, nil } -func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) { +func cmdFromStruct(name string, dest path, t reflect.Type, envPrefix string) (*command, error) { // commands can only be created from pointers to structs if t.Kind() != reflect.Ptr { return nil, fmt.Errorf("subcommands must be pointers to structs but %s is a %s", @@ -372,9 +375,9 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) { case key == "env": // Use override name if provided if value != "" { - spec.env = value + spec.env = envPrefix + value } else { - spec.env = strings.ToUpper(field.Name) + spec.env = envPrefix + strings.ToUpper(field.Name) } case key == "subcommand": // decide on a name for the subcommand @@ -389,7 +392,7 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) { } // parse the subcommand recursively - subcmd, err := cmdFromStruct(cmdnames[0], subdest, field.Type) + subcmd, err := cmdFromStruct(cmdnames[0], subdest, field.Type, envPrefix) if err != nil { errs = append(errs, err.Error()) return false diff --git a/parse_test.go b/parse_test.go index 5bc781c..a932c22 100644 --- a/parse_test.go +++ b/parse_test.go @@ -28,11 +28,11 @@ func parse(cmdline string, dest interface{}) error { } func pparse(cmdline string, dest interface{}) (*Parser, error) { - return parseWithEnv(cmdline, nil, dest) + return parseWithEnv(Config{}, cmdline, nil, dest) } -func parseWithEnv(cmdline string, env []string, dest interface{}) (*Parser, error) { - p, err := NewParser(Config{}, dest) +func parseWithEnv(config Config, cmdline string, env []string, dest interface{}) (*Parser, error) { + p, err := NewParser(config, dest) if err != nil { return nil, err } @@ -231,7 +231,7 @@ func TestRequiredWithEnvOnly(t *testing.T) { var args struct { Foo string `arg:"required,--,-,env:FOO"` } - _, err := parseWithEnv("", []string{}, &args) + _, err := parseWithEnv(Config{}, "", []string{}, &args) require.Error(t, err, "environment variable FOO is required") } @@ -711,7 +711,7 @@ func TestEnvironmentVariable(t *testing.T) { var args struct { Foo string `arg:"env"` } - _, err := parseWithEnv("", []string{"FOO=bar"}, &args) + _, err := parseWithEnv(Config{}, "", []string{"FOO=bar"}, &args) require.NoError(t, err) assert.Equal(t, "bar", args.Foo) } @@ -720,7 +720,7 @@ func TestEnvironmentVariableNotPresent(t *testing.T) { var args struct { NotPresent string `arg:"env"` } - _, err := parseWithEnv("", nil, &args) + _, err := parseWithEnv(Config{}, "", nil, &args) require.NoError(t, err) assert.Equal(t, "", args.NotPresent) } @@ -729,7 +729,7 @@ func TestEnvironmentVariableOverrideName(t *testing.T) { var args struct { Foo string `arg:"env:BAZ"` } - _, err := parseWithEnv("", []string{"BAZ=bar"}, &args) + _, err := parseWithEnv(Config{}, "", []string{"BAZ=bar"}, &args) require.NoError(t, err) assert.Equal(t, "bar", args.Foo) } @@ -738,7 +738,7 @@ func TestEnvironmentVariableOverrideArgument(t *testing.T) { var args struct { Foo string `arg:"env"` } - _, err := parseWithEnv("--foo zzz", []string{"FOO=bar"}, &args) + _, err := parseWithEnv(Config{}, "--foo zzz", []string{"FOO=bar"}, &args) require.NoError(t, err) assert.Equal(t, "zzz", args.Foo) } @@ -747,7 +747,7 @@ func TestEnvironmentVariableError(t *testing.T) { var args struct { Foo int `arg:"env"` } - _, err := parseWithEnv("", []string{"FOO=bar"}, &args) + _, err := parseWithEnv(Config{}, "", []string{"FOO=bar"}, &args) assert.Error(t, err) } @@ -755,7 +755,7 @@ func TestEnvironmentVariableRequired(t *testing.T) { var args struct { Foo string `arg:"env,required"` } - _, err := parseWithEnv("", []string{"FOO=bar"}, &args) + _, err := parseWithEnv(Config{}, "", []string{"FOO=bar"}, &args) require.NoError(t, err) assert.Equal(t, "bar", args.Foo) } @@ -764,7 +764,7 @@ func TestEnvironmentVariableSliceArgumentString(t *testing.T) { var args struct { Foo []string `arg:"env"` } - _, err := parseWithEnv("", []string{`FOO=bar,"baz, qux"`}, &args) + _, err := parseWithEnv(Config{}, "", []string{`FOO=bar,"baz, qux"`}, &args) require.NoError(t, err) assert.Equal(t, []string{"bar", "baz, qux"}, args.Foo) } @@ -773,7 +773,7 @@ func TestEnvironmentVariableSliceEmpty(t *testing.T) { var args struct { Foo []string `arg:"env"` } - _, err := parseWithEnv("", []string{`FOO=`}, &args) + _, err := parseWithEnv(Config{}, "", []string{`FOO=`}, &args) require.NoError(t, err) assert.Len(t, args.Foo, 0) } @@ -782,7 +782,7 @@ func TestEnvironmentVariableSliceArgumentInteger(t *testing.T) { var args struct { Foo []int `arg:"env"` } - _, err := parseWithEnv("", []string{`FOO=1,99`}, &args) + _, err := parseWithEnv(Config{}, "", []string{`FOO=1,99`}, &args) require.NoError(t, err) assert.Equal(t, []int{1, 99}, args.Foo) } @@ -791,7 +791,7 @@ func TestEnvironmentVariableSliceArgumentFloat(t *testing.T) { var args struct { Foo []float32 `arg:"env"` } - _, err := parseWithEnv("", []string{`FOO=1.1,99.9`}, &args) + _, err := parseWithEnv(Config{}, "", []string{`FOO=1.1,99.9`}, &args) require.NoError(t, err) assert.Equal(t, []float32{1.1, 99.9}, args.Foo) } @@ -800,7 +800,7 @@ func TestEnvironmentVariableSliceArgumentBool(t *testing.T) { var args struct { Foo []bool `arg:"env"` } - _, err := parseWithEnv("", []string{`FOO=true,false,0,1`}, &args) + _, err := parseWithEnv(Config{}, "", []string{`FOO=true,false,0,1`}, &args) require.NoError(t, err) assert.Equal(t, []bool{true, false, false, true}, args.Foo) } @@ -809,7 +809,7 @@ func TestEnvironmentVariableSliceArgumentWrongCsv(t *testing.T) { var args struct { Foo []int `arg:"env"` } - _, err := parseWithEnv("", []string{`FOO=1,99\"`}, &args) + _, err := parseWithEnv(Config{}, "", []string{`FOO=1,99\"`}, &args) assert.Error(t, err) } @@ -817,7 +817,7 @@ func TestEnvironmentVariableSliceArgumentWrongType(t *testing.T) { var args struct { Foo []bool `arg:"env"` } - _, err := parseWithEnv("", []string{`FOO=one,two`}, &args) + _, err := parseWithEnv(Config{}, "", []string{`FOO=one,two`}, &args) assert.Error(t, err) } @@ -825,7 +825,7 @@ func TestEnvironmentVariableMap(t *testing.T) { var args struct { Foo map[int]string `arg:"env"` } - _, err := parseWithEnv("", []string{`FOO=1=one,99=ninetynine`}, &args) + _, err := parseWithEnv(Config{}, "", []string{`FOO=1=one,99=ninetynine`}, &args) require.NoError(t, err) assert.Len(t, args.Foo, 2) assert.Equal(t, "one", args.Foo[1]) @@ -836,11 +836,21 @@ func TestEnvironmentVariableEmptyMap(t *testing.T) { var args struct { Foo map[int]string `arg:"env"` } - _, err := parseWithEnv("", []string{`FOO=`}, &args) + _, err := parseWithEnv(Config{}, "", []string{`FOO=`}, &args) require.NoError(t, err) assert.Len(t, args.Foo, 0) } +func TestEnvironmentVariableWithPrefix(t *testing.T) { + var args struct { + Foo string `arg:"env"` + } + + _, err := parseWithEnv(Config{EnvPrefix: "MYAPP_"}, "", []string{"MYAPP_FOO=bar"}, &args) + require.NoError(t, err) + assert.Equal(t, "bar", args.Foo) +} + func TestEnvironmentVariableIgnored(t *testing.T) { var args struct { Foo string `arg:"env"` @@ -873,7 +883,7 @@ func TestRequiredEnvironmentOnlyVariableIsMissing(t *testing.T) { Foo string `arg:"required,--,env:FOO"` } - _, err := parseWithEnv("", []string{""}, &args) + _, err := parseWithEnv(Config{}, "", []string{""}, &args) assert.Error(t, err) } @@ -882,7 +892,7 @@ func TestOptionalEnvironmentOnlyVariable(t *testing.T) { Foo string `arg:"env:FOO"` } - _, err := parseWithEnv("", []string{}, &args) + _, err := parseWithEnv(Config{}, "", []string{}, &args) assert.NoError(t, err) } -- cgit v1.2.3 From 51d9bef113c82cff90c5929d28934bb241e1b1df Mon Sep 17 00:00:00 2001 From: Alex Flint Date: Mon, 21 Oct 2024 17:08:37 -0400 Subject: passing the no-more-options string "--" twice or more should pass the second and subsequent ones through as positionals --- parse.go | 2 +- parse_test.go | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) (limited to 'parse.go') diff --git a/parse.go b/parse.go index 172c7cd..d9c73d0 100644 --- a/parse.go +++ b/parse.go @@ -615,7 +615,7 @@ func (p *Parser) process(args []string) error { // must use explicit for loop, not range, because we manipulate i inside the loop for i := 0; i < len(args); i++ { arg := args[i] - if arg == "--" { + if arg == "--" && !allpositional { allpositional = true continue } diff --git a/parse_test.go b/parse_test.go index 5bc781c..7e9cbf9 100644 --- a/parse_test.go +++ b/parse_test.go @@ -609,6 +609,15 @@ func TestNoMoreOptionsBeforeHelp(t *testing.T) { assert.NotEqual(t, ErrHelp, err) } +func TestNoMoreOptionsTwice(t *testing.T) { + var args struct { + X []string `arg:"positional"` + } + err := parse("-- --", &args) + require.NoError(t, err) + assert.Equal(t, []string{"--"}, args.X) +} + func TestHelpFlag(t *testing.T) { var args struct { Foo string -- cgit v1.2.3