If you have only one verb defined, but using an undefined verb will not output help, but will execute the code path for the only verb defined.
Using CommandLineParser Version 2.5.0 and given this simple program:
class Program
{
static int Main(string[] args)
{
return Parser.Default.ParseArguments<UpdateOptions>(args)
.MapResult(
(UpdateOptions opts) => RunUpdateAndReturnExitCode(opts),
errs => 1);
}
private static int RunUpdateAndReturnExitCode(UpdateOptions opts)
{
Console.WriteLine("Running update verb");
if (opts.Test)
{
return 1;
}
return 0;
}
}
[Verb("update", HelpText = "Updates")]
public class UpdateOptions
{
[Option(HelpText = "Whether this is a test")]
public bool Test { get; set; }
}
Running this with a verb other than update should print the help text and point out, that the verb does not exist:
位 dotnet run -- foo
Program 1.0.0
Copyright 漏 2019 Structed
ERROR(S):
Verb 'foo' is not recognized.
--help Display this help screen.
--version Display version information.
Running with the verb help should display the help screen, and show the available verb:
位 dotnet run -- help
Program 1.0.0
Copyright 漏 2019 Structed
update Updates
help Display more information on a specific command.
version Display version information.
Running this with the correct verb update, will of course execute the path for this verb:
位 dotnet run -- update
Running update verb
However, running this with the non existing verb foo will also (incorrectly) execute the path of the update verb:
位 dotnet run -- foo
Running update verb
Using the Verb help does also execute the update verb's path:
位 dotnet run -- help
Running update verb
Only when adding a second verb, the execution paths are correct!
This is not a very big issue, but if you start a new tool using CommandLineParser, and want to start with just one verb to set the style, and later add on to it, it will confuse users and can lead to unexpected results.
This issue is similar to #384 and as a work around solution provided by @kvalev, pass object as second verb type - this forces the library to use the correct extension method:
static int Main(string[] args)
{
return Parser.Default.ParseArguments<UpdateOptions,object>(args)
.MapResult(
(UpdateOptions opts) => RunUpdateAndReturnExitCode(opts),
errs => 1);
}
Most helpful comment
This issue is similar to #384 and as a work around solution provided by @kvalev, pass object as second verb type - this forces the library to use the correct extension method: