Skip to content

Fix IsOptional method for arguments #1862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/System.CommandLine.Tests/Help/HelpBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,38 @@ public void Command_arguments_with_default_values_that_are_enumerable_display_pi
_console.ToString().Should().Contain(expected);
}

[Fact]
public void Command_shared_arguments_with_one_or_more_arity_are_displayed_as_being_required()
{
var arg = new Argument<string[]>
{
Name = "shared-args",
Arity = ArgumentArity.OneOrMore
};

var inner = new Command("inner", "command help")
{
arg
};
_ = new Command("outer", "command help")
{
inner,
arg
};
_ = new Command("unused", "command help")
{
arg
};

_helpBuilder.Write(inner, _console);

var expected =
$"Usage:{NewLine}" +
$"{_indentation}outer <shared-args>... inner <shared-args>...";

_console.ToString().Should().Contain(expected);
}

#endregion Arguments

#region Options
Expand Down
6 changes: 1 addition & 5 deletions src/System.CommandLine/Help/HelpBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,8 @@ private string FormatArgumentUsage(IReadOnlyList<Argument> arguments)
{
StringBuilderPool.Default.ReturnToPool(sb);
}

bool IsMultiParented(Argument a) =>
a.FirstParent is not null && a.FirstParent.Next is not null;


bool IsOptional(Argument argument) =>
IsMultiParented(argument) ||
argument.Arity.MinimumNumberOfValues == 0;
}

Expand Down