Skip to content

Commit c776cd4

Browse files
iBichajonsequitur
andauthored
Fix IsOptional method for arguments (#1862)
* Fix IsOptional method for arguments * Add test * Add shared arg test * Revert "Add test" This reverts commit 5959570. * Update src/System.CommandLine.Tests/Help/HelpBuilderTests.cs Co-authored-by: Jon Sequeira <[email protected]>
1 parent cf7a861 commit c776cd4

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/System.CommandLine.Tests/Help/HelpBuilderTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,38 @@ public void Command_arguments_with_default_values_that_are_enumerable_display_pi
932932
_console.ToString().Should().Contain(expected);
933933
}
934934

935+
[Fact]
936+
public void Command_shared_arguments_with_one_or_more_arity_are_displayed_as_being_required()
937+
{
938+
var arg = new Argument<string[]>
939+
{
940+
Name = "shared-args",
941+
Arity = ArgumentArity.OneOrMore
942+
};
943+
944+
var inner = new Command("inner", "command help")
945+
{
946+
arg
947+
};
948+
_ = new Command("outer", "command help")
949+
{
950+
inner,
951+
arg
952+
};
953+
_ = new Command("unused", "command help")
954+
{
955+
arg
956+
};
935957

958+
_helpBuilder.Write(inner, _console);
959+
960+
var expected =
961+
$"Usage:{NewLine}" +
962+
$"{_indentation}outer <shared-args>... inner <shared-args>...";
963+
964+
_console.ToString().Should().Contain(expected);
965+
}
966+
936967
#endregion Arguments
937968

938969
#region Options

src/System.CommandLine/Help/HelpBuilder.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,12 +325,8 @@ private string FormatArgumentUsage(IReadOnlyList<Argument> arguments)
325325
{
326326
StringBuilderPool.Default.ReturnToPool(sb);
327327
}
328-
329-
bool IsMultiParented(Argument a) =>
330-
a.FirstParent is not null && a.FirstParent.Next is not null;
331-
328+
332329
bool IsOptional(Argument argument) =>
333-
IsMultiParented(argument) ||
334330
argument.Arity.MinimumNumberOfValues == 0;
335331
}
336332

0 commit comments

Comments
 (0)