Repository: dotnet/command-line-api (2026-04-02T19:51:38Z)

Issue:
Missing required IEnumerable argument causes runtime error
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.


Discussion:

Patch:
diff --git a/src/System.CommandLine/Parsing/SymbolResult.cs b/src/System.CommandLine/Parsing/SymbolResult.cs
index 18df4af83e..5affe2124a 100644
--- a/src/System.CommandLine/Parsing/SymbolResult.cs
+++ b/src/System.CommandLine/Parsing/SymbolResult.cs
@@ -130,7 +130,7 @@ public T GetRequiredValue<T>(Argument<T> argument)
             => GetResult(argument) switch
             {
                 ArgumentResult argumentResult => argumentResult.GetValueOrDefault<T>(),
-                null => throw new InvalidOperationException($"{argument.Name} is required but was not provided."),
+                null => Argument<T>.CreateDefaultValue() ?? throw new InvalidOperationException($"{argument.Name} is required but was not provided."),
             };
 
         /// <inheritdoc cref="ParseResult.GetRequiredValue{T}(Option{T})"/>


Test patch:
diff --git a/src/System.CommandLine.Tests/ParsingValidationTests.cs b/src/System.CommandLine.Tests/ParsingValidationTests.cs
index fa32438991..a038013d20 100644
--- a/src/System.CommandLine.Tests/ParsingValidationTests.cs
+++ b/src/System.CommandLine.Tests/ParsingValidationTests.cs
@@ -214,6 +214,38 @@ public void When_a_required_argument_is_not_supplied_then_an_error_is_returned()
                   .Contain(e => e.Message == "Required argument missing for option: '-x'.");
         }
 
+        [Fact]
+        public void When_an_IEnumerable_argument_has_zero_minimum_arity_and_no_tokens_GetRequiredValue_returns_empty()
+        {
+            var argument = new Argument<IEnumerable<string>>("items");
+            var command = new RootCommand { argument };
+
+            var result = command.Parse("");
+
+            result.Errors.Should().BeEmpty();
+            result.GetRequiredValue(argument).Should().BeEmpty();
+        }
+
+        [Fact]
+        public void When_an_IEnumerable_argument_has_OneOrMore_arity_and_no_tokens_then_an_error_is_returned()
+        {
+            var argument = new Argument<IEnumerable<string>>("items")
+            {
+                Arity = ArgumentArity.OneOrMore
+            };
+            var command = new RootCommand { argument };
+
+            var result = command.Parse("");
+
+            result.Errors
+                  .Should()
+                  .ContainSingle()
+                  .Which
+                  .Message
+                  .Should()
+                  .Contain("items");
+        }
+
         [Fact]
         public void When_a_required_option_is_not_supplied_then_an_error_is_returned()
         {