Skip to content

Passing extra arguments after "help (verb)" causes an InvalidOperationException #225

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

Closed
rmunn opened this issue Aug 17, 2015 · 6 comments
Closed

Comments

@rmunn
Copy link
Contributor

rmunn commented Aug 17, 2015

Using CommandLineParser 2.0.235.0 beta, I can cause an InvalidOperationException by simply running "my-cmd.exe help verb foo" instead of "my-cmd.exe help verb". "my-cmd.exe help verb" produces the expected help text, but "my-cmd.exe help verb foo" produces the following exception:

Unhandled Exception: System.InvalidOperationException: Sequence contains more than one element
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
   at CommandLine.Core.InstanceChooser.<>c__DisplayClass0_0.<Choose>b__0()
   at CommandLine.Core.InstanceChooser.Choose(Func`3 tokenizer, IEnumerable`1 types, IEnumerable`1 arguments, StringComparer nameComparer, CultureInfo parsingCulture, IEnumerable`1 nonFatalErrors)
   at CommandLine.Parser.<>c__DisplayClass13_0.<ParseArguments>b__0()
   at CommandLine.Parser.MakeParserResult[T](Func`1 parseFunc, ParserSettings settings)
   at TryOutCommandLineParser.Program.Main(String[] args) in c:\src\try\TryOutCommandLineParser\Program.cs:line 17

Running "my-cmd.exe verb foo --help" works, sort of (I don't like the "Option 'help' is unknown) error, but I can live with it. But I'm pretty sure that users will at one point try replacing "my-cmd.exe verb foo bar -v" with "my-cmd.exe help verb foo bar -v" to get help usage, and I'd like this to be handled. So when the "help" verb is invoked, I'd like it to ignore any further arguments, but not throw an exception if they are present.

@gsscoder
Copy link
Owner

@rmunn, thanks for detailed reporting, I'll try fix both issues (the exception and help unknown).

I'll update you here.

Thanks, again!

gsscoder added a commit that referenced this issue Aug 19, 2015
gsscoder added a commit that referenced this issue Aug 19, 2015
@gsscoder
Copy link
Owner

When you invoke your app with:

my-cmd.exe verb foo --help

--help is enumerated as an option because the canonical help request, requires you to put "--help" as first argument. In this case the help screen is showed not because you added "--help" but because the command line syntax rules are not respected.

I need reason a little more on this... The main issue anyway is fixed, in seconds I'll merge to master...

@gsscoder
Copy link
Owner

Version 2.0.239-beta fixes the issue (just released to NuGet), if this works can you please close the issue?

Thanks, @rmunn.

@rmunn
Copy link
Contributor Author

rmunn commented Aug 19, 2015

Regarding --help, the logic I've come to expect from a verb-using command-line program like git is the following:

  1. Scan all the arguments (up to the -- argument, if present) for a -h or --help argument.
  2. If it was present, display appropriate help for the verb (or, if no verb was present, for the program as a whole).
  3. If no -h or --help was present, process the verb's normal arguments.

For example:

$ git pull -v --ff-only --help
usage: [...]

Whereas:

$ git pull -v --ff-only --bad-argument
error: unknown option `bad-argument'
usage: [...]

But the --help argument, and how to treat it, should probably be discussed in a separate issue.

@rmunn
Copy link
Contributor Author

rmunn commented Aug 19, 2015

I've tested version 2.0.239-beta, and it works the way I want now (barring the "Option 'help' is unknown" error). This issue is fixed.

@rmunn rmunn closed this as completed Aug 19, 2015
gsscoder added a commit that referenced this issue Aug 19, 2015
gsscoder added a commit that referenced this issue Aug 19, 2015
@gsscoder
Copy link
Owner

@rmunn, after some reasoning I'm agree with you that listing help as unknown option is senseless.

This is now fixed in version 2.0.241-beta on master and released to NuGet.

Anyway an eventual -h will not be removed because, this is not a built-in command for requesting help.

If you're interested in internals:
this is the test -> https://github.com/gsscoder/commandline/blob/master/tests/CommandLine.Tests/Unit/ParserTests.cs#L542
this is the fix -> https://github.com/gsscoder/commandline/blob/master/src/CommandLine/ErrorExtensions.cs#L23

Bye! :)

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

No branches or pull requests

2 participants