go-arg icon indicating copy to clipboard operation
go-arg copied to clipboard

Repeated flags not detected....

Open dyfet opened this issue 3 years ago • 5 comments

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".

dyfet avatar Dec 27 '21 17:12 dyfet

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.

alexflint avatar Dec 28 '21 16:12 alexflint

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...

dyfet avatar Dec 28 '21 18:12 dyfet

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"

suntong avatar Jan 08 '22 23:01 suntong

Will try to support this in upcoming v2

alexflint avatar Oct 29 '22 15:10 alexflint