Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misleading "Usage:" when slice option and positional arguments? #89

Closed
marco-m opened this issue Oct 4, 2019 · 3 comments · Fixed by #90
Closed

Misleading "Usage:" when slice option and positional arguments? #89

marco-m opened this issue Oct 4, 2019 · 3 comments · Fixed by #90

Comments

@marco-m
Copy link
Contributor

marco-m commented Oct 4, 2019

Hello, thanks a lot for go-arg, I love it!

consider the following program (using go-arg v1.1.0)

package main

import (
	"fmt"
	arg "github.com/alexflint/go-arg"
)

func main() {
	var args struct {
		Foo []string
		A   string `arg:"positional,required"`
	}
	arg.MustParse(&args)
	fmt.Printf("%+v\n", args)
}

Let's ask for its help:

$ ./bin/positional -h
Usage: positional [--foo FOO] A

Positional arguments:
  A

Options:
  --foo FOO
  --help, -h             display this help and exit

If I try to follow the "Usage" example, I get:

$ ./bin/positional --foo 1 x
Usage: positional [--foo FOO] A
error: a is required

which is very confusing for the end user (and at first for the programmer too, then the programmer understands the reason :-D)

After thinking about it, I tried the classic -- to mean end of flag processing, but it didn't work:

 $ ./bin/positional --foo 1 -- x
Usage: positional [--foo FOO] A
error: a is required

After a while I saw the light and inverted the order, and it worked as expected:

$ ./bin/positional x --foo 1
{Foo:[1] A:x}

So I guess I have two questions:

  1. Would it be possible/suitable to make the -- trick work? (Is this maybe a regression of Positional arguments with dashes #55 ?)
  2. Would it make sense, in presence of a required positional argument AND a slice of options, to invert the usage, something like:
Usage: positional A [--foo FOO]

or any other way to give an hint to the user?

@alexflint
Copy link
Owner

Woah this is definitely a bug. Both of the examples you gave are supposed to work:

./bin/positional --foo 1 x
./bin/positional --foo 1 -- x

If these are broken then go-arg has a regression in a recent release. I'll investigate.

@alexflint
Copy link
Owner

Oh on first read I didn't see that foo is a []string. Given that, I still expect the second example you gave to work (but not the first):

./bin/positional --foo 1 -- x   // is supposed to work given the program you gave
./bin/positional --foo 1 x     // not expected to work

@marco-m
Copy link
Contributor Author

marco-m commented Oct 4, 2019

Would it make sense then to change the usage text to mention the -- if the positional is required? Maybe:

Usage: positional [--foo FOO --] A

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants