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

Repeated flags not detected.... #170

Open
dyfet opened this issue Dec 27, 2021 · 4 comments
Open

Repeated flags not detected.... #170

dyfet opened this issue Dec 27, 2021 · 4 comments
Labels
v2-ideas Possible inclusion in V2

Comments

@dyfet
Copy link

dyfet commented Dec 27, 2021

I can do something like -h -h -h and this is treated as a single -h rather than as an error. Yet -hhhh is treated as an error.

Closely related, some argument parsers allow for special "count" flags. For example, I typically like to use -vvv to specify verbosity/debug level, much like the ssh command does. It would seem to me that it would require a special kind of tag, "counted", much like the existing "positional".

@alexflint
Copy link
Owner

alexflint commented Dec 28, 2021

Indeed this is not possible with the current API. One option would be to do it manually like this:

var args struct {
  H bool `arg:"-h"`
}
arg.MustParse(&args)

var hCount int
for _, s := range os.Args() {
  if s == "-h" {
    hCount += 1
  }
}

I admit you're basically working around go-arg at this point but at least it would interoperate with other flags that are parsed by go-arg.

@dyfet
Copy link
Author

dyfet commented Dec 28, 2021

Actually I was looking for the -h -h -h case to be recognized as an error by default. It's actually the "-vvvv" case that I really want to count... Hence, something like:

    Level uint `arg:"counted,-v"`

This is a common feature in pflag, and often used in c/c++ option parsers, but yes, it's probably not easy to implement. Or maybe, if it is found in Args() it could be stripped out and replaced with a single -v before MustParse sees it? But the help message would be wrong if it is Level, so I guess it would have to be a dummy bool field. But that of course is also missing the point of the package if we have to steal flags and parse around it...

@suntong
Copy link

suntong commented Jan 8, 2022

It's actually the "-vvvv" case that I really want to count...

I badly need that as well, as I also "like to use -vvv to specify verbosity/debug level, much like the ssh command does"

@alexflint
Copy link
Owner

Will try to support this in upcoming v2

@alexflint alexflint added the v2-ideas Possible inclusion in V2 label Oct 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v2-ideas Possible inclusion in V2
Projects
None yet
Development

No branches or pull requests

3 participants