Skip to content

Option/Argument.GetRequiredValue throws when DefaultValueFactory is set and help is requested #2592

@jonsequitur

Description

@jonsequitur

When an option is marked Required and a DefaultValueFactory is set, calling Option.GetRequiredValue still throws.

Program code:

using System.CommandLine;

var rootCommand = new RootCommand("This is the description")
{
    new Option<string>("--required") 
    {
        Required = true,
        DefaultValueFactory = _ => "hello"
    }
};

var parseResult = rootCommand.Parse("-h");

Console.WriteLine(parseResult.GetRequiredValue<string>("--required"));

Exception:

Unhandled exception. System.InvalidOperationException: --required is required but was not provided.
   at System.CommandLine.Parsing.SymbolResult.GetRequiredValue[T](String name)
   at System.CommandLine.ParseResult.GetRequiredValue[T](String name)
   at Program.<Main>$(String[] args) in C:\temp\command-name-issue\Program.cs:line 14

Image

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions