commandlineparser / commandline

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

Option without value should throw an error. #861

Open virtualdreams opened 1 year ago

virtualdreams commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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