Skip to content

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

Closed

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

Activity

  1. added this to the Triage milestone on Jun 24, 2025
  2. self-assigned this
    on Jun 24, 2025
  3. added a commit that references this issue on Jun 27, 2025
    84e446b
  4. changed the title [-]Option/Argument.GetRequiredValue throws when DefaultValueFactory is set[/-] [+]Option/Argument.GetRequiredValue throws when DefaultValueFactory is set and help is requested[/+] on Jun 27, 2025
  5. added a commit that references this issue on Jun 30, 2025
    64c1011
  6. added a commit that references this issue on Jun 30, 2025
    341d847
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area-Parser and Binderbug

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions