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

Option without value should throw an error. #861

Open
virtualdreams opened this issue Dec 6, 2022 · 3 comments
Open

Option without value should throw an error. #861

virtualdreams opened this issue Dec 6, 2022 · 3 comments

Comments

@virtualdreams
Copy link

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
@HaxtonFale
Copy link

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.

@ericnewton76
Copy link
Member

ericnewton76 commented Mar 7, 2023

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

@virtualdreams
Copy link
Author

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

aaronpowell added a commit to Azure/data-api-builder that referenced this issue May 4, 2023
ull, enable the feature.

This might seem backwards but it's not possible to pass
ull to the options on the CLI,
ull is just the absence of the option (or the absence of a value for the option, which is a different problem. see commandlineparser/commandline#861)
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

3 participants