commandlineparser / commandline

The best C# command line parser that brings standardized *nix getopt style, for .NET. Includes F# support
MIT License
4.53k stars 477 forks source link

Just giving --version as argument gives an error #789

Open lsilvand opened 2 years ago

lsilvand commented 2 years ago

I look around the issues and found a couple of issues that are close to mine but not quite. The problem I have is just giving the "--version" as a argument gives an error. I can handle the version with .WithNotParsed and write out "version" at the end of the list but it always gives an error too. "--help" works fine without an error. Am I missing something? Is it also possible to not show the options if you just want to give the version information?

H:>SEMImages.exe --version SEMImages 2.0.0.0 Linus Silvander © 2021

ERROR(S): Option 'version' is unknown.

-f, --file Choose file to analyze. If both a file and directory is given, the file will be used.

-d, --dir Choose directory to analyze. If no directory is given the current path will be used.

--aperture Add aperture size.

--detector Add detector used.

--date Add date when image was taken.

--EHT Add acceleration voltage.

--mag Add magnification.

--stage Add all stage values. Scan rotation is only added if turned on.

--time Add time when image was taken.

--WD Add working distance.

--help Display this help screen.

--version Display version information.

version

H:>

elgonzo commented 2 years ago

Dunno.

I have no problems with the built-in "--version" option here (using the library version 2.9.0-preview3). Also, this example using 2.8.0 on dotnetfiddle (https://dotnetfiddle.net/VNWTP8) exhibits no problem with the built-in "--version" option. The only conclusion i can draw from this is that the problem might either be caused by

If the problem is not resolved by updating/using the current CommandLineParser library version, i am afraid you'll have to offer a minimal code example reproducing the observed behavior for anyone willing to look into the problem.

lsilvand commented 2 years ago

Thanks elgonzo for your help! Reading your answer I realized that there was a difference between my code and the example code. I had inserted the CommandLineParser in an existing project where I had used "Environment.GetCommandLineArgs();" instead of "public static void Main(string[] args)". Changing that fixed the problem. What is the difference between them and why is there only a problem with --version, everything else worked ok? Is it a bug in CommandLineParser or GetCommandLineArgs?

elgonzo commented 2 years ago

Okay, that explains it. I see it now, too. The bug occurs if another option or argument preceeds the "--version" option.

Note that the Environment.GetCommandLineArgs() method, despite its name, not only yields the commandline arguments for the executed program but the program name/path together with the commandline arguments. From the documentation:

The first element in the array contains the file name of the executing program. If the file name is not available, the first element is equal to String.Empty. The remaining elements contain any additional tokens entered on the command line.

Which means, you are feeding not just string[] {"--version"} to the CommandLineParser, but you are actually passing string[] {<MyProgramName>, "--version"}. (As a side note, it also means that the heading of your report here is technically incorrect ;-) ) If you remove the program name/path from the result given by Environment.GetCommandLineArgs() before feeding it to CommandLineParser, it should work. With the range operator available since C# 8, this would be as simple as:

var args = Environment.GetCommandLineArgs()[1..];

CommandLine.Parser.Default
    .ParseArguments<Options>(args)
    ...

Still, even with this improvement, the bug is still present. Even after applying this code change, you would still be able to trigger the bug by executing your program like this, for example SEMImages.exe --mag 2 --version, whether you use Environment.GetCommandLineArgs() or not.

lsilvand commented 2 years ago

Thanks for the info. A bit new to this all, just writing programs to make my life easier.