From 5701595a7cb618823735bb2668249cc198aeb6a0 Mon Sep 17 00:00:00 2001 From: Jon Sequeira Date: Thu, 2 Apr 2026 12:50:56 -0700 Subject: [PATCH] fix #2754 --- .../ParsingValidationTests.cs | 32 +++++++++++++++++++ .../Parsing/SymbolResult.cs | 2 +- 2 files changed, 33 insertions(+), 1 deletion(-) 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>("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>("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() { 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(Argument argument) => GetResult(argument) switch { ArgumentResult argumentResult => argumentResult.GetValueOrDefault(), - null => throw new InvalidOperationException($"{argument.Name} is required but was not provided."), + null => Argument.CreateDefaultValue() ?? throw new InvalidOperationException($"{argument.Name} is required but was not provided."), }; ///