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

When flags.HelpFlag used, -h shows up as positional arg #107

Closed
conorr opened this issue Oct 9, 2014 · 3 comments
Closed

When flags.HelpFlag used, -h shows up as positional arg #107

conorr opened this issue Oct 9, 2014 · 3 comments

Comments

@conorr
Copy link

conorr commented Oct 9, 2014

When I create a parser using flags.NewParser and specify the flags.Default option so the help message option is added, I am seeing an unexpected result when setting the -h/--help option on execution.

When I run a program with the -h option, the help message is printed as expected but -h gets passed through as a leftover argument. Same goes for --help. When I set other options (ones I've defined), those options don't get passed through as leftover arguments.

Is this expected? Here is a gist that shows how I am producing the issue: https://gist.github.com/ctryan3/8dd552b02fdd8ae8efa1

@conorr conorr changed the title When default.AddHelp added automatically, -h shows up as positional arg When flags.HelpFlag used, -h shows up as positional arg Oct 9, 2014
@jessevdk
Copy link
Owner

jessevdk commented Oct 9, 2014

This is a side effect from help being treated as an error. Basically what happens currently when an error happens is that you get as the returned args all the remaining arguments from and including the error location. This is more or less intentional, but I can see how that doesn't work out for the help flag hijacking the error system to return early.

We could special case help to act as double dash, i.e. pass-through of all remaining args, and then return that (along with the error). Ideally we would have a better solution for doing the help handling, maybe we can add a HelpHandler field in the parser which people can set to handle what should happen after the help has been shown (which usually would be an os.Exit(1)).

@zimmski
Copy link
Contributor

zimmski commented Oct 9, 2014

How about

  • turn help options into booleans
  • adding a new option attribute so canCli can be false (to get rid of isBuiltinHelp)
  • adding hooks which return an error between the option parsing and the command execution which are executed before executing the active command/group
  • If the parser option HelpFlag is set, the hook does exactly the same as now: return the ErrHelp error if help option is true.
  • Add a parser method to add your own Help hook function (instead of adding the parser option HelpFlag) -> don't like ErrHelp? do an os.Exit(1) or something else instead

This would remove -h/--help from the remaining arguments, would remove some extra help handling which is currently scattered and put help handling into one parser method.

@jessevdk
Copy link
Owner

I went with the easy way out, which is to just not prepend the -h arg to the retargs when the error is ErrHelp. @zimmski regarding your proposal, I thought about it and although it does remove some help specific things, it also adds some new that aren't really that useful outside of help. At least currently the special handling of help flags is internal.

I'd still like to see a better way to implement -h/--help without all the internal support that we need to do currently. It would be nice if we could find a way for -h/--help to work without any of the special tricks we do.

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

3 participants