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

Invalid verbs do not cause errors if a default verb is set #849

Open
JimSuplizio opened this issue Sep 23, 2022 · 1 comment
Open

Invalid verbs do not cause errors if a default verb is set #849

JimSuplizio opened this issue Sep 23, 2022 · 1 comment

Comments

@JimSuplizio
Copy link

JimSuplizio commented Sep 23, 2022

Below is a very simplified C# console application I called DefaultVerb. Running the application with start or finish works as expected, outputting Start or Finish correctly, but if I run the application with DefaultVerb.exe foo, it outputs Start. If I set isDefault to false, and run DefaultVerb.exe foo I receive the following expected error:
ERROR(S): Verb 'foo' is not recognized. --help Display this help screen. --version Display version information.
When I have isDefault: true on my verb, what should be the behavior if I pass in an invalid verb? I'm not expecting a bad verb to run my default verb.

`
using CommandLine;
using System;

internal class Program
{
[Verb("start", isDefault: true, HelpText = "start help")]
class StartOptions
{
}

[Verb("finish", HelpText = "finish help")]
class FinishOptions
{
}

static void Main(string[] args)
{
    var parser = new Parser(settings =>
    {
        settings.AutoVersion = false;
        settings.CaseSensitive = false;
        settings.HelpWriter = Console.Out;
    });

    parser.ParseArguments<StartOptions, FinishOptions>(args)
        .WithNotParsed(ExitWithError)
        .WithParsed(DoSomething);
}

static void DoSomething(object commandObj)
{
    switch (commandObj)
    {
        case StartOptions:
            Console.WriteLine("Start");
            break;
        case FinishOptions:
            Console.WriteLine("Finish");
            break;
    }
}
static void ExitWithError(IEnumerable<Error> errors)
{
    Environment.Exit(1);
}

}
`

@hallipr
Copy link

hallipr commented Feb 23, 2023

This looks like it's related to #847 "unused positional argument / value".

An unmapped positional argument with value foo is being passed to the default verb start, even though start doesn't define any positional arguments.

If you add a positional argument to StartOptions you'll see the value foo being passed:

[Verb("start", isDefault: true, HelpText = "start help")]
class StartOptions
{
	[Value(0)]
	public string Positional { get; set; }
}

...

static void DoSomething(object commandObj)
{
    switch (commandObj)
    {
        case StartOptions startOptions:
            Console.WriteLine($"Start: {startOptions.Positional}");
            break;
        case FinishOptions:
            Console.WriteLine("Finish");
            break;
    }
}

DefaultVerb.exe foo --> Start: foo

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