Skip to content

Missing required IEnumerable argument causes runtime error #2754

@hatulaile

Description

@hatulaile

If an IEnumerable type argument is defined, the error message "Required argument missing for command" will not be shown; instead, it will directly throw an error.

Argument<string[]> argument = new("test argument");
RootCommand rootCommand = new RootCommand("Test") { argument };
rootCommand.SetAction(result => Console.WriteLine(result.GetRequiredValue(argument)));
await rootCommand.Parse(args).InvokeAsync();

If you run the example above, an error will occur.

Unhandled exception: System.InvalidOperationException: test argument is required but was not provided.
   at System.CommandLine.Parsing.SymbolResult.GetRequiredValue[T](Argument`1 argument)
   at System.CommandLine.ParseResult.GetRequiredValue[T](Argument`1 argument)
   at Program.<>c__DisplayClass0_0.<<Main>$>b__0(ParseResult result) in D:\Dev\Study\ConsoleApp1\Program.cs:line 5
   at System.CommandLine.Command.<>c__DisplayClass30_0.<SetAction>b__0(ParseResult context)
   at System.CommandLine.Invocation.AnonymousSynchronousCommandLineAction.Invoke(ParseResult parseResult)
   at System.CommandLine.Invocation.InvocationPipeline.InvokeAsync(ParseResult parseResult, CancellationToken cancellationToken)

If the Argument is changed to string, no error will occur.

My English is not very good, so I used an AI translator. Please let me know if I haven't expressed myself clearly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions