Skip to content

Commit e0edf18

Browse files
committed
Show ArgumentHelpName in help if explicitly set by user
1 parent d2203f9 commit e0edf18

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,10 @@ public void Help_describes_default_values_for_complex_root_command_scenario()
5353
new Option<FileAccess>(aliases: new string[] {"--the-root-option-enum-arg", "-troea"}, () => FileAccess.Read)
5454
{
5555
Description = "the-root-option-description",
56-
ArgumentHelpName = "the-root-option-arg",
5756
},
5857
new Option<FileAccess>(aliases: new string[] {"--the-root-option-required-enum-arg", "-trorea"}, () => FileAccess.Read)
5958
{
6059
Description = "the-root-option-description",
61-
ArgumentHelpName = "the-root-option-arg",
6260
IsRequired = true
6361
},
6462
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
@@ -469,6 +469,32 @@ public void Arguments_section_includes_configured_argument_aliases()
469469
help.Should().Contain("Sets the verbosity.");
470470
}
471471

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

12411267
help.Should().Contain($"[default: the-arg-value]");
12421268
}
1243-
1269+
12441270
[Fact]
12451271
public void Option_arguments_with_default_values_that_are_enumerable_display_pipe_delimited_list()
12461272
{

src/System.CommandLine/Argument.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class Argument : Symbol, IArgument
1717
{
1818
private Func<ArgumentResult, object?>? _defaultValueFactory;
1919
private IArgumentArity? _arity;
20+
private bool _isHelpNameSpecified;
2021
private TryConvertArgument? _convertArguments;
2122
private Type _argumentType = typeof(string);
2223
private SuggestionSourceList? _suggestions = null;
@@ -65,6 +66,16 @@ public IArgumentArity? Arity
6566
set => _arity = value;
6667
}
6768

69+
70+
/// <summary>
71+
/// Gets or sets if the ArgumentHelpName is specified by the user.
72+
/// </summary>
73+
public bool IsHelpNameSpecified
74+
{
75+
get => _isHelpNameSpecified;
76+
set => _isHelpNameSpecified = value;
77+
}
78+
6879
internal TryConvertArgument? ConvertArguments
6980
{
7081
get

src/System.CommandLine/Help/HelpBuilder.cs

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

523523
string descriptor;
524524
var suggestions = argument.GetSuggestions().ToArray();
525-
if (suggestions.Length > 0)
525+
if (suggestions.Length > 0 && UseSuggestionsInHelp(argument))
526526
{
527527
descriptor = string.Join("|", suggestions);
528528
}
@@ -538,6 +538,12 @@ protected string GetArgumentDescriptor(IArgument argument)
538538
return descriptor;
539539
}
540540

541+
private bool UseSuggestionsInHelp(IArgument argument)
542+
{
543+
var arg = argument as Argument;
544+
return arg == null || !arg.IsHelpNameSpecified;
545+
}
546+
541547
private class Customization
542548
{
543549
public Customization(Func<string?>? getDescriptor,

src/System.CommandLine/Option.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,11 @@ internal virtual Argument Argument
134134
public string ArgumentHelpName
135135
{
136136
get => Argument.Name;
137-
set => Argument.Name = value;
137+
set
138+
{
139+
Argument.Name = value;
140+
Argument.IsHelpNameSpecified = true;
141+
}
138142
}
139143

140144
/// <summary>

0 commit comments

Comments
 (0)