commandline icon indicating copy to clipboard operation
commandline copied to clipboard

CommandLine.HelpRequestedError and CommandLine.VersionRequestedError

Open dineshlappathy opened this issue 4 years ago • 7 comments

Not sure it is already under consideration!

ProgXYZ --version ProgXYZ --help

invokes WithNotParsed and list contains error HelpRequestedError and VersionRequestedError

dineshlappathy avatar May 15 '20 11:05 dineshlappathy

yes --version expose VersionRequestedError --help expose HelpRequestedError

moh-hassan avatar May 22 '20 23:05 moh-hassan

I just changed my code to handle these.

bool stopProcessing = false;

Parser.Default.ParseArguments<CommandLineOptions>(args)
	.WithNotParsed(o =>
	{
		List<Error> errors = o.ToList();

		stopProcessing = errors.Any(e => e.StopsProcessing);

		if (errors.Any(e => e.Tag == ErrorType.HelpRequestedError || e.Tag == ErrorType.VersionRequestedError))
		{
			return;
		}

		foreach (Error error in errors)
		{
			Console.WriteLine($"Error command line parameter '{error.Tag}'");
		}
	})
	.WithParsed(o => CommandLineOptions = o);

if (stopProcessing)
{
	return 1;
}

KoalaBear84 avatar Feb 02 '21 14:02 KoalaBear84

I don't understand why this is considered as an error by default ? KoalaBear84, I did the same than you, but to handle "Help requested error", note that apparently we have to test for "HelpVerbRequestedError"

AFract avatar Mar 08 '21 09:03 AFract

You mean there is something not right in my code yet?

KoalaBear84 avatar Mar 08 '21 09:03 KoalaBear84

For me, when the application is started with the verb "help", the "error" tag is not e.Tag == ErrorType.HelpRequestedError like in your example, but e.Tag == ErrorType.HelpVerbRequestedError. So, as far as I can see, to avoid to process it (in your case with a console.WriteLine) it's this check that you have to do. But it's just based on my own usage experience, when I encountered the same problem than you.

AFract avatar Mar 08 '21 17:03 AFract

I stumbled into this too. Imho, --help and --version should not be considered as error. I've just spent 10 minutes to look for an error although there was none hence until I noticed that both mentioned options are just considered as such.

I used the same workaround as @KoalaBear84 which works for me because there is no verb help nor the word version used in my application - only the options --help and --version.

Siam1205 avatar Jun 11 '21 07:06 Siam1205

Just an FYI it's actually slightly worse than what's above. help/--help doesn't give you the HelpRequestedError, as shown above, it gives HelpVerbRequestedError whereas version or --version just gives VersionRequestedError. There's definitely a mismatch here.

JimSuplizio avatar Oct 13 '22 23:10 JimSuplizio