Skip to content

Commit df57bcb

Browse files
committed
fix #1157
1 parent 82e8198 commit df57bcb

File tree

2 files changed

+44
-34
lines changed

2 files changed

+44
-34
lines changed

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

+24-29
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.CommandLine.Builder;
77
using System.CommandLine.Help;
8+
using System.CommandLine.Invocation;
89
using System.CommandLine.IO;
910
using System.CommandLine.Parsing;
1011
using System.IO;
@@ -668,7 +669,7 @@ public void Arguments_section_properly_wraps()
668669
var name = "argument-name-for-a-command-that-is-long-enough-to-wrap-to-a-new-line";
669670
var description = "Argument description for a command with line breaks that is long enough to wrap to a new line.";
670671

671-
var command = new RootCommand()
672+
var command = new RootCommand
672673
{
673674
new Argument
674675
{
@@ -690,33 +691,6 @@ public void Arguments_section_properly_wraps()
690691
_console.Out.ToString().Should().Contain(expected);
691692
}
692693

693-
[Theory]
694-
[InlineData(typeof(bool))]
695-
[InlineData(typeof(bool?))]
696-
public void Command_argument_descriptor_is_empty_for_boolean_values(Type type)
697-
{
698-
var description = "This is the argument description";
699-
700-
var command = new Command("outer", "Help text for the outer command")
701-
{
702-
new Argument
703-
{
704-
Description = description,
705-
ArgumentType = type
706-
}
707-
};
708-
709-
HelpBuilder helpBuilder = GetHelpBuilder(SmallMaxWidth);
710-
711-
helpBuilder.Write(command);
712-
713-
var expected =
714-
$"Arguments:{NewLine}" +
715-
$"{_indentation}{_columnPadding}{description}";
716-
717-
_console.Out.ToString().Should().Contain(expected);
718-
}
719-
720694
[Theory]
721695
[InlineData(typeof(FileAccess))]
722696
[InlineData(typeof(FileAccess?))]
@@ -764,6 +738,27 @@ public void Option_argument_descriptor_is_empty_for_boolean_values(Type type)
764738
_console.Out.ToString().Should().Contain($"--opt{_columnPadding}{description}");
765739
}
766740

741+
[Fact] // https://github.com/dotnet/command-line-api/issues/1157
742+
public void Command_arguments_show_argument_name_as_descriptor()
743+
{
744+
var command = new RootCommand
745+
{
746+
new Argument<bool>("boolArgument", "Some value"),
747+
new Argument<int>("intArgument", "Another value"),
748+
};
749+
750+
var helpBuilder = GetHelpBuilder(SmallMaxWidth);
751+
752+
helpBuilder.Write(command);
753+
754+
var expected =
755+
$"Arguments:{NewLine}" +
756+
$"{_indentation}<boolArgument>{_columnPadding}Some value{NewLine}" +
757+
$"{_indentation}<intArgument> {_columnPadding}Another value{NewLine}";
758+
759+
_console.Out.ToString().Should().Contain(expected);
760+
}
761+
767762
[Theory]
768763
[InlineData(typeof(FileAccess))]
769764
[InlineData(typeof(FileAccess?))]
@@ -878,7 +873,7 @@ public void Command_arguments_can_customize_default_value()
878873
}
879874

880875
[Fact]
881-
public void Command_arguments_can_customize_dedescriptor()
876+
public void Command_arguments_can_customize_descriptor()
882877
{
883878
var argument = new Argument<string>("some-arg", getDefaultValue: () => "not 42");
884879
var command = new Command("the-command", "command help")

src/System.CommandLine/Help/HelpBuilder.cs

+20-5
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ protected IEnumerable<HelpItem> GetCommandArguments(ICommand command)
134134
.SelectMany(GetArguments)
135135
.Distinct();
136136

137-
138137
IEnumerable<HelpItem> GetArguments(ICommand command)
139138
{
140139
var arguments = command.Arguments.Where(x => !x.IsHidden).ToList();
140+
141141
foreach (IArgument argument in arguments)
142142
{
143143
string argumentDescriptor = GetArgumentDescriptor(argument);
@@ -286,7 +286,11 @@ bool IsOptional(IArgument argument) =>
286286

287287
protected void RenderAsColumns(params HelpItem[] items)
288288
{
289-
if (items.Length == 0) return;
289+
if (items.Length == 0)
290+
{
291+
return;
292+
}
293+
290294
int windowWidth = MaxWidth;
291295

292296
int firstColumnWidth = items.Select(x => x.Descriptor.Length).Max();
@@ -338,9 +342,13 @@ protected void RenderAsColumns(params HelpItem[] items)
338342

339343
private static IEnumerable<string> WrapItem(string item, int maxWidth)
340344
{
341-
if (string.IsNullOrWhiteSpace(item)) yield break;
345+
if (string.IsNullOrWhiteSpace(item))
346+
{
347+
yield break;
348+
}
349+
342350
//First handle existing new lines
343-
var parts = item.Split(new string[] { "\r\n", "\n", }, StringSplitOptions.None);
351+
var parts = item.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
344352

345353
foreach (string part in parts)
346354
{
@@ -499,7 +507,14 @@ protected string GetArgumentDescriptor(IArgument argument)
499507
if (argument.ValueType == typeof(bool) ||
500508
argument.ValueType == typeof(bool?))
501509
{
502-
return "";
510+
if (argument.Parents.FirstOrDefault() is ICommand)
511+
{
512+
return $"<{argument.Name}>";
513+
}
514+
else
515+
{
516+
return "";
517+
}
503518
}
504519

505520
string descriptor;

0 commit comments

Comments
 (0)