diff --git a/src/System.CommandLine.Tests/Help/HelpBuilderTests.Approval.cs b/src/System.CommandLine.Tests/Help/HelpBuilderTests.Approval.cs index 576956c9ad..0d0672d721 100644 --- a/src/System.CommandLine.Tests/Help/HelpBuilderTests.Approval.cs +++ b/src/System.CommandLine.Tests/Help/HelpBuilderTests.Approval.cs @@ -53,12 +53,10 @@ public void Help_describes_default_values_for_complex_root_command_scenario() new Option(aliases: new string[] {"--the-root-option-enum-arg", "-troea"}, () => FileAccess.Read) { Description = "the-root-option-description", - ArgumentHelpName = "the-root-option-arg", }, new Option(aliases: new string[] {"--the-root-option-required-enum-arg", "-trorea"}, () => FileAccess.Read) { Description = "the-root-option-description", - ArgumentHelpName = "the-root-option-arg", IsRequired = true }, new Option(aliases: new string[] {"--the-root-option-multi-line-description", "-tromld"}) { diff --git a/src/System.CommandLine.Tests/Help/HelpBuilderTests.cs b/src/System.CommandLine.Tests/Help/HelpBuilderTests.cs index 589096ce89..c6bb2c4759 100644 --- a/src/System.CommandLine.Tests/Help/HelpBuilderTests.cs +++ b/src/System.CommandLine.Tests/Help/HelpBuilderTests.cs @@ -469,6 +469,32 @@ public void Arguments_section_includes_configured_argument_aliases() help.Should().Contain("Sets the verbosity."); } + + private enum VerbosityOptions + { + q, + m, + n, + d, + } + + [Fact] + public void Arguments_section_uses_name_over_suggestions_if_specified() + { + var command = new Command("the-command") + { + new Option(new[] { "-v", "--verbosity" }) + { + ArgumentHelpName = "LEVEL" + } + }; + + _helpBuilder.Write(command); + + var help = _console.Out.ToString(); + help.Should().Contain("-v, --verbosity "); + } + [Fact] public void Arguments_section_uses_description_if_provided() { @@ -1240,7 +1266,7 @@ public void Help_describes_default_value_for_option_with_argument_having_default help.Should().Contain($"[default: the-arg-value]"); } - + [Fact] public void Option_arguments_with_default_values_that_are_enumerable_display_pipe_delimited_list() { diff --git a/src/System.CommandLine.Tests/OptionTests.cs b/src/System.CommandLine.Tests/OptionTests.cs index 7096df7f74..c00315e3d7 100644 --- a/src/System.CommandLine.Tests/OptionTests.cs +++ b/src/System.CommandLine.Tests/OptionTests.cs @@ -240,7 +240,7 @@ public void Argument_takes_option_alias_as_its_name_when_it_is_not_provided() { var command = new Option("--alias", arity: ArgumentArity.ZeroOrOne); - command.ArgumentHelpName.Should().Be("alias"); + command.Name.Should().Be("alias"); } [Fact] diff --git a/src/System.CommandLine/Argument.cs b/src/System.CommandLine/Argument.cs index 850980db73..b7d4e3ee55 100644 --- a/src/System.CommandLine/Argument.cs +++ b/src/System.CommandLine/Argument.cs @@ -65,6 +65,11 @@ public IArgumentArity? Arity set => _arity = value; } + /// + /// Argument help name + /// + internal string? HelpName { get; set; } + internal TryConvertArgument? ConvertArguments { get diff --git a/src/System.CommandLine/Help/HelpBuilder.cs b/src/System.CommandLine/Help/HelpBuilder.cs index 23ce4d6b36..5cba0c8502 100644 --- a/src/System.CommandLine/Help/HelpBuilder.cs +++ b/src/System.CommandLine/Help/HelpBuilder.cs @@ -522,7 +522,12 @@ protected string GetArgumentDescriptor(IArgument argument) string descriptor; var suggestions = argument.GetSuggestions().ToArray(); - if (suggestions.Length > 0) + var helpName = GetArgumentHelpName(argument); + if (!string.IsNullOrEmpty(helpName)) + { + descriptor = helpName!; + } + else if (suggestions.Length > 0) { descriptor = string.Join("|", suggestions); } @@ -538,6 +543,12 @@ protected string GetArgumentDescriptor(IArgument argument) return descriptor; } + private string? GetArgumentHelpName(IArgument argument) + { + var arg = argument as Argument; + return arg?.HelpName; + } + private class Customization { public Customization(Func? getDescriptor, diff --git a/src/System.CommandLine/Option.cs b/src/System.CommandLine/Option.cs index d9139b0ccd..423738cb14 100644 --- a/src/System.CommandLine/Option.cs +++ b/src/System.CommandLine/Option.cs @@ -131,10 +131,13 @@ internal virtual Argument Argument /// /// The name of the argument when displayed in help. /// - public string ArgumentHelpName + public string? ArgumentHelpName { - get => Argument.Name; - set => Argument.Name = value; + get => Argument.HelpName; + set + { + Argument.HelpName = value; + } } ///