commandline icon indicating copy to clipboard operation
commandline copied to clipboard

Option without value should throw an error.

Open virtualdreams opened this issue 2 years ago • 3 comments
trafficstars

If I have a string option and it is called without a value, no error is thrown. This is what I would expect.

using CommandLine;

namespace cli
{
	public class Program
	{
		static void Main(string[] args)
		{
			CommandLine.Parser.Default.ParseArguments<Options>(args)
				.WithParsed(options => RunOptions(options))
				.WithNotParsed(error => ErrorAndExit(error));
		}

		static void RunOptions(Options options)
		{

		}

		static void ErrorAndExit(IEnumerable<Error> error)
		{ }
	}

	public class Options
	{
		[Option("value", Required = false, HelpText = "A value.")]
		public string Value { get; set; }
	}
}

If i call this command line i get no error.

cli --value # should throw an error
cli --value blah # ok, this is what i want

virtualdreams avatar Dec 06 '22 13:12 virtualdreams

I second this. There's no easy way of knowing whether a parameter was supplied without value or if it was omitted, and those are two different kinds of failure for us.

HaxtonFale avatar Feb 15 '23 14:02 HaxtonFale

Why would this throw an error? You have required = false in the Option attribute...

theres a third scenario:

cli --value
cli --value blah
cli --value --othervalue

Effectively 1 and 3 are the same

ericnewton76 avatar Mar 07 '23 18:03 ericnewton76

Required requires the option. This has nothing todo with the requirement of an option value.

https://github.com/commandlineparser/commandline/wiki/Option-Attribute

Required Gets or sets a value indicating whether a command line option is required.

A string option should also force a value as with getopt has_arg https://man7.org/linux/man-pages/man3/getopt.3.html

virtualdreams avatar Mar 15 '23 09:03 virtualdreams