Skip to content

No way to disambiguate between a missing required param error and default help or version #2659

@smaillet

Description

@smaillet

Consider the following app:

using System.CommandLine;

// Requires NuGet package: System.CommandLine 2.0.0-beta6.25358.103 (Latest prerelease)
namespace TestCommandLineParsing
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var foo = new Option<string>("--foo")
            {
                Required = true,
            };

            var root = new RootCommand("Test app to illustrate issue")
            {
                foo
            };

            ParseResult result = root.Parse("--version");
            // AMBIGUITY: 
            //result.Action is System.CommandLine.VersionOption.VersionOptionAction (A private type so can't be tested)
            // result.Errors.Count == 1
            // Result.Errors[0].Message == "Option '--foo' is required."
        }
    }
}

As listed in the comments starting with AMBIGUITY there is no way to distinguish between a missing required parameter and the default version parameter. How can a consumer know if the default options are provided and should terminate ignoring the error reports? There's an inherent ambiguity on whether an error condition exists (because the required parm isn't provided) and the --version parameter. But THAT parameter will generate an error if it is used with any other options... (Effectively the validator for a required parameter should not add an error if --Version is applied of the validator for that shouldn't fail if it isn't solo)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions