Skip to content

Commit 27d86d3

Browse files
authored
Show ArgumentHelpName in help if explicitly set by user (#1367)
* Show ArgumentHelpName in help if explicitly set by user * Add HelpName field to Argument
1 parent 93adae4 commit 27d86d3

File tree

6 files changed

+51
-8
lines changed

6 files changed

+51
-8
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,10 @@ public void Help_describes_default_values_for_complex_root_command_scenario()
5252
new Option<FileAccess>(aliases: new string[] {"--the-root-option-enum-arg", "-troea"}, () => FileAccess.Read)
5353
{
5454
Description = "the-root-option-description",
55-
ArgumentHelpName = "the-root-option-arg",
5655
},
5756
new Option<FileAccess>(aliases: new string[] {"--the-root-option-required-enum-arg", "-trorea"}, () => FileAccess.Read)
5857
{
5958
Description = "the-root-option-description",
60-
ArgumentHelpName = "the-root-option-arg",
6159
IsRequired = true
6260
},
6361
new Option(aliases: new string[] {"--the-root-option-multi-line-description", "-tromld"}) {

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,32 @@ public void Arguments_section_includes_configured_argument_aliases()
467467
help.Should().Contain("Sets the verbosity.");
468468
}
469469

470+
471+
private enum VerbosityOptions
472+
{
473+
q,
474+
m,
475+
n,
476+
d,
477+
}
478+
479+
[Fact]
480+
public void Arguments_section_uses_name_over_suggestions_if_specified()
481+
{
482+
var command = new Command("the-command")
483+
{
484+
new Option<VerbosityOptions>(new[] { "-v", "--verbosity" })
485+
{
486+
ArgumentHelpName = "LEVEL"
487+
}
488+
};
489+
490+
_helpBuilder.Write(command);
491+
492+
var help = _console.Out.ToString();
493+
help.Should().Contain("-v, --verbosity <LEVEL>");
494+
}
495+
470496
[Fact]
471497
public void Arguments_section_uses_description_if_provided()
472498
{
@@ -1238,7 +1264,7 @@ public void Help_describes_default_value_for_option_with_argument_having_default
12381264

12391265
help.Should().Contain($"[default: the-arg-value]");
12401266
}
1241-
1267+
12421268
[Fact]
12431269
public void Option_arguments_with_default_values_that_are_enumerable_display_pipe_delimited_list()
12441270
{

src/System.CommandLine.Tests/OptionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public void Argument_takes_option_alias_as_its_name_when_it_is_not_provided()
240240
{
241241
var command = new Option("--alias", arity: ArgumentArity.ZeroOrOne);
242242

243-
command.ArgumentHelpName.Should().Be("alias");
243+
command.Name.Should().Be("alias");
244244
}
245245

246246
[Fact]

src/System.CommandLine/Argument.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ public IArgumentArity? Arity
6565
set => _arity = value;
6666
}
6767

68+
/// <summary>
69+
/// Argument help name
70+
/// </summary>
71+
internal string? HelpName { get; set; }
72+
6873
internal TryConvertArgument? ConvertArguments
6974
{
7075
get

src/System.CommandLine/Help/HelpBuilder.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,12 @@ protected string GetArgumentDescriptor(IArgument argument)
522522

523523
string descriptor;
524524
var suggestions = argument.GetSuggestions().ToArray();
525-
if (suggestions.Length > 0)
525+
var helpName = GetArgumentHelpName(argument);
526+
if (!string.IsNullOrEmpty(helpName))
527+
{
528+
descriptor = helpName!;
529+
}
530+
else if (suggestions.Length > 0)
526531
{
527532
descriptor = string.Join("|", suggestions);
528533
}
@@ -538,6 +543,12 @@ protected string GetArgumentDescriptor(IArgument argument)
538543
return descriptor;
539544
}
540545

546+
private string? GetArgumentHelpName(IArgument argument)
547+
{
548+
var arg = argument as Argument;
549+
return arg?.HelpName;
550+
}
551+
541552
private class Customization
542553
{
543554
public Customization(Func<string?>? getDescriptor,

src/System.CommandLine/Option.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,13 @@ internal virtual Argument Argument
131131
/// <value>
132132
/// The name of the argument when displayed in help.
133133
/// </value>
134-
public string ArgumentHelpName
134+
public string? ArgumentHelpName
135135
{
136-
get => Argument.Name;
137-
set => Argument.Name = value;
136+
get => Argument.HelpName;
137+
set
138+
{
139+
Argument.HelpName = value;
140+
}
138141
}
139142

140143
/// <summary>

0 commit comments

Comments
 (0)