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

subcommand help flags cause exception #86

Closed
siennathesane opened this issue Apr 12, 2018 · 7 comments
Closed

subcommand help flags cause exception #86

siennathesane opened this issue Apr 12, 2018 · 7 comments
Labels
bug good first issue This seems like a good issue if you're a new contributor. help wanted We would be willing to take a well-written PR to help fix this.
Milestone

Comments

@siennathesane
Copy link

When trying to use a help flag on a subcommand, I get an Unhandled Exception: System.InvalidOperationException: Sequence contains more than one matching element error.

Example code:

using System;
using McMaster.Extensions.CommandLineUtils;

namespace updater
{
    class Program
    {
        public static int Main(string[] args)
        {
            var app = new CommandLineApplication();
            app.Name = "updater";
            app.HelpOption(inherited: true);

            app.Command("get", (getcmd) =>
            {
                getcmd.Description = "Gets a list of things.";
                getcmd.HelpOption();
                
                getcmd.OnExecute(() =>
                {
                    getcmd.ShowHelp();
                    return 1;
                });
            });

            app.OnExecute(() => {
                app.ShowHelp();
                return 1;
            });

            return app.Execute(args);
        }
    }
}

Here are the steps I am taking to create this:

  • > dotnet publish --self-contained -r win10-x64
  • > .\bin\Debug\netcoreapp2.0\win10-x64\publish\updater.exe get
  • > .\bin\Debug\netcoreapp2.0\win10-x64\publish\updater.exe get --help

It throws this exception:

Unhandled Exception: System.InvalidOperationException: Sequence contains more than one matching element
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
   at McMaster.Extensions.CommandLineUtils.CommandLineProcessor.ProcessOption() in C:\projects\commandlineutils\src\CommandLineUtils\Internal\CommandLineProcessor.cs:line 156
   at McMaster.Extensions.CommandLineUtils.CommandLineProcessor.ProcessNext() in C:\projects\commandlineutils\src\CommandLineUtils\Internal\CommandLineProcessor.cs:line 62
   at McMaster.Extensions.CommandLineUtils.CommandLineProcessor.Process() in C:\projects\commandlineutils\src\CommandLineUtils\Internal\CommandLineProcessor.cs:line 35
   at McMaster.Extensions.CommandLineUtils.CommandLineApplication.Parse(String[] args) in C:\projects\commandlineutils\src\CommandLineUtils\CommandLineApplication.cs:line 532
   at McMaster.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args) in C:\projects\commandlineutils\src\CommandLineUtils\CommandLineApplication.cs:line 597
   at updater.Program.Main(String[] args) in P:\C#\updater\Program.cs:line 31

Here is my dotnet configuration:

> dotnet --info
.NET Command Line Tools (2.1.2)

Product Information:
 Version:            2.1.2
 Commit SHA-1 hash:  5695315371

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.16299
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\2.1.2\

Microsoft .NET Core Shared Framework Host

  Version  : 2.0.5
  Build    : 17373eb129b3b05aa18ece963f8795d65ef8ea54

I very well could be doing something wrong, too.

@atruskie
Copy link
Contributor

atruskie commented Apr 13, 2018

I've run into a similar the same problem as well. The problem has to do with the Inherited option generally. The logic that checks for duplicated options does seem to be aware of Inherited options.

Here is an example that fails in the same way, using attributes and options other than HelpOption:

    [Command("fake-git")]
    [Subcommand("commit", typeof(CommitCommand))]
    class Git
    {
        protected int OnExecute(CommandLineApplication app)
        {
            return 1;
        }
        
        [Option(Inherited = true)]
        public string Message2 { get; set; }
    }

    [Command(Description = "Record changes to the repository")]
    class CommitCommand
    {
        [Option("-m")]
        public string Message { get; set; }

    }

Stack trace:

System.InvalidOperationException
  HResult=0x80131509
  Message=Sequence contains more than one matching element
  Source=System.Linq
  StackTrace:
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
   at McMaster.Extensions.CommandLineUtils.CommandLineProcessor.ProcessOption() in C:\Work\Github\CommandLineUtils\src\CommandLineUtils\Internal\CommandLineProcessor.cs:line 147
   at McMaster.Extensions.CommandLineUtils.CommandLineProcessor.ProcessNext() in C:\Work\Github\CommandLineUtils\src\CommandLineUtils\Internal\CommandLineProcessor.cs:line 62
   at McMaster.Extensions.CommandLineUtils.CommandLineProcessor.Process() in C:\Work\Github\CommandLineUtils\src\CommandLineUtils\Internal\CommandLineProcessor.cs:line 38
   at McMaster.Extensions.CommandLineUtils.CommandLineApplication.Parse(String[] args) in C:\Work\Github\CommandLineUtils\src\CommandLineUtils\CommandLineApplication.cs:line 533
   at McMaster.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args) in C:\Work\Github\CommandLineUtils\src\CommandLineUtils\CommandLineApplication.cs:line 597
   at McMaster.Extensions.CommandLineUtils.CommandLineApplication.Execute[TApp](CommandLineContext context) in C:\Work\Github\CommandLineUtils\src\CommandLineUtils\CommandLineApplication.Execute.cs:line 57
   at McMaster.Extensions.CommandLineUtils.CommandLineApplication.Execute[TApp](IConsole console, String[] args) in C:\Work\Github\CommandLineUtils\src\CommandLineUtils\CommandLineApplication.Execute.cs:line 97
   at McMaster.Extensions.CommandLineUtils.CommandLineApplication.Execute[TApp](String[] args) in C:\Work\Github\CommandLineUtils\src\CommandLineUtils\CommandLineApplication.Execute.cs:line 79
   at SubcommandSample.Program.OnExecute() in C:\Work\Github\CommandLineUtils\samples\Subcommands\Program.cs:line 45

@atruskie
Copy link
Contributor

To clarify both my example and the example from @mxplusb are incorrect configurations, but usually a better error message (the OptionNameIsAmbiguous error I believe) is shown.

@mxplusb I believe you don't need to do getcmd.HelpOption(); for your app to work.

@natemcmaster
Copy link
Owner

Indeed, we can probably provide a better error, or just choose the HelpOption nearest to the selected command. Marking as up-for-grabs if someone wants to contribute a fix.

@natemcmaster natemcmaster added help wanted We would be willing to take a well-written PR to help fix this. good first issue This seems like a good issue if you're a new contributor. labels Apr 13, 2018
@siennathesane
Copy link
Author

Ah, I was just following what I understood was the right way to do it via the builder interface. If there is a better/more specific way to use the builder interface, I'd love to take a look at it.

@natemcmaster
Copy link
Owner

@mxplusb the simplest fix here for you is to only have one call to .HelpOption. If you call app.HelpOption(inherited: true) on your top level command, you don't need to call .HelpOption on any of the subcommand objects.

             app.Command("get", (getcmd) =>
             {
                 getcmd.Description = "Gets a list of things.";
-                getcmd.HelpOption();
                
                 getcmd.OnExecute(() =>
                 {
                     getcmd.ShowHelp();

@natemcmaster natemcmaster added this to the Backlog milestone May 31, 2018
@natemcmaster natemcmaster removed this from the Backlog milestone Sep 28, 2018
@handcraftedsource
Copy link
Contributor

handcraftedsource commented Oct 3, 2018

Hello @natemcmaster,
since I like to use the CommandLineUtils myself, I would like to contribute something. This issue seems to be a good start. I hope this is okay.

@natemcmaster natemcmaster added this to the 2.3.0 milestone Oct 4, 2018
@natemcmaster
Copy link
Owner

Thanks @handcraftedsource! Yes, this is a good place to start.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug good first issue This seems like a good issue if you're a new contributor. help wanted We would be willing to take a well-written PR to help fix this.
Projects
None yet
Development

No branches or pull requests

4 participants