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

Counter Options #77

Closed
runeimp opened this issue Apr 14, 2019 · 2 comments
Closed

Counter Options #77

runeimp opened this issue Apr 14, 2019 · 2 comments

Comments

@runeimp
Copy link

runeimp commented Apr 14, 2019

I'd love it if there was support for multiple instance options that count their repetition instead of accepting multiple values. This is commonly used for verbosity where a setup, something along the lines of

type args struct {
	Debug   bool   `arg:"-D" help:"output debug info"`
	Verbose uint8 `arg:"-v,counter" help:"Increases verbosity"`
}

would allow for --verbose blah -v on the command line would set args.verbose == 2, -vvv would set args.verbose == 3, etc. And not specifying any verbosity options would set args.verbose == 0.

@alexflint
Copy link
Owner

Hey @runeimp - thanks for the suggestion. This seems quite reasonable, but it also feels like it might only be used for the common "verbose" flag. I can't really think of another realistic use-case. I wonder if it would be better just to do it manually with something like

func main() {
  var verbose int
  for i, arg := range os.Args {
    if strings.TrimRight(arg, "v") == "-" {
      verbose = len(arg) - 1
      os.Args = append(os.Args[:i], os.Args[i+1:]...)
      break
    }
  }
  var args struct {
    ...
  }
  arg.MustParse(&args)
  ...
}

This seems like a reasonably small amount of code for the special case of "-v", whereas implementing the general version inside go-arg and covering all the corner cases feels like it would add quite a bit of complexity.

@runeimp
Copy link
Author

runeimp commented Apr 15, 2019

I totally understand. I've seen it used in other places but verbosity is definitely the major use case. Thanks for the suggestion on how to work around it.

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

No branches or pull requests

2 participants