From 18b4187ae85c9ecce0e666c9c2bb5da079184534 Mon Sep 17 00:00:00 2001 From: Nate McMaster Date: Thu, 30 Jun 2016 15:38:55 -0700 Subject: [PATCH] Allow elements to be hidden from help text (#133) --- .gitignore | 1 + .../CommandLine/CommandArgument.cs | 1 + .../CommandLine/CommandLineApplication.cs | 21 ++++++---- .../CommandLine/CommandOption.cs | 2 +- .../CommandLineApplicationTests.cs | 42 ++++++++++++++++++- 5 files changed, 56 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 0b0e2686a87..c6dac842e45 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,4 @@ nuget.exe *.sln.ide node_modules **/[Cc]ompiler/[Rr]esources/**/*.js +.vscode/ \ No newline at end of file diff --git a/src/Microsoft.Extensions.CommandLineUtils/CommandLine/CommandArgument.cs b/src/Microsoft.Extensions.CommandLineUtils/CommandLine/CommandArgument.cs index a2f2127f3bc..c687485e337 100644 --- a/src/Microsoft.Extensions.CommandLineUtils/CommandLine/CommandArgument.cs +++ b/src/Microsoft.Extensions.CommandLineUtils/CommandLine/CommandArgument.cs @@ -14,6 +14,7 @@ public CommandArgument() } public string Name { get; set; } + public bool ShowInHelpText { get; set; } = true; public string Description { get; set; } public List Values { get; private set; } public bool MultipleValues { get; set; } diff --git a/src/Microsoft.Extensions.CommandLineUtils/CommandLine/CommandLineApplication.cs b/src/Microsoft.Extensions.CommandLineUtils/CommandLine/CommandLineApplication.cs index 9ae085e7cfb..f1c91519ec5 100644 --- a/src/Microsoft.Extensions.CommandLineUtils/CommandLine/CommandLineApplication.cs +++ b/src/Microsoft.Extensions.CommandLineUtils/CommandLine/CommandLineApplication.cs @@ -32,6 +32,7 @@ public CommandLineApplication(bool throwOnUnexpectedArg = true) public string FullName { get; set; } public string Syntax { get; set; } public string Description { get; set; } + public bool ShowInHelpText { get; set; } = true; public readonly List Options; public CommandOption OptionHelp { get; private set; } public CommandOption OptionVersion { get; private set; } @@ -341,7 +342,7 @@ public void ShowHelp(string commandName = null) Console.WriteLine(GetHelpText(commandName)); } - public string GetHelpText(string commandName = null) + public virtual string GetHelpText(string commandName = null) { var headerBuilder = new StringBuilder("Usage:"); for (var cmd = this; cmd != null; cmd = cmd.Parent) @@ -375,22 +376,23 @@ public string GetHelpText(string commandName = null) var commandsBuilder = new StringBuilder(); var argumentsBuilder = new StringBuilder(); - if (target.Arguments.Any()) + var arguments = target.Arguments.Where(a => a.ShowInHelpText).ToList(); + if (arguments.Any()) { headerBuilder.Append(" [arguments]"); argumentsBuilder.AppendLine(); argumentsBuilder.AppendLine("Arguments:"); - var maxArgLen = target.Arguments.Max(a => a.Name.Length); + var maxArgLen = arguments.Max(a => a.Name.Length); var outputFormat = string.Format(" {{0, -{0}}}{{1}}", maxArgLen + 2); - foreach (var arg in target.Arguments) + foreach (var arg in arguments) { argumentsBuilder.AppendFormat(outputFormat, arg.Name, arg.Description); argumentsBuilder.AppendLine(); } } - var options = target.GetOptions().ToList(); + var options = target.GetOptions().Where(o => o.ShowInHelpText).ToList(); if (options.Any()) { headerBuilder.Append(" [options]"); @@ -406,15 +408,16 @@ public string GetHelpText(string commandName = null) } } - if (target.Commands.Any()) + var commands = target.Commands.Where(c => c.ShowInHelpText).ToList(); + if (commands.Any()) { headerBuilder.Append(" [command]"); commandsBuilder.AppendLine(); commandsBuilder.AppendLine("Commands:"); - var maxCmdLen = target.Commands.Max(c => c.Name.Length); + var maxCmdLen = commands.Max(c => c.Name.Length); var outputFormat = string.Format(" {{0, -{0}}}{{1}}", maxCmdLen + 2); - foreach (var cmd in target.Commands.OrderBy(c => c.Name)) + foreach (var cmd in commands.OrderBy(c => c.Name)) { commandsBuilder.AppendFormat(outputFormat, cmd.Name, cmd.Description); commandsBuilder.AppendLine(); @@ -423,7 +426,7 @@ public string GetHelpText(string commandName = null) if (OptionHelp != null) { commandsBuilder.AppendLine(); - commandsBuilder.AppendFormat("Use \"{0} [command] --help\" for more information about a command.", Name); + commandsBuilder.AppendFormat($"Use \"{target.Name} [command] --{OptionHelp.LongName}\" for more information about a command."); commandsBuilder.AppendLine(); } } diff --git a/src/Microsoft.Extensions.CommandLineUtils/CommandLine/CommandOption.cs b/src/Microsoft.Extensions.CommandLineUtils/CommandLine/CommandOption.cs index 71f25d485d4..8f6d6af4ad2 100644 --- a/src/Microsoft.Extensions.CommandLineUtils/CommandLine/CommandOption.cs +++ b/src/Microsoft.Extensions.CommandLineUtils/CommandLine/CommandOption.cs @@ -59,7 +59,7 @@ public CommandOption(string template, CommandOptionType optionType) public string Description { get; set; } public List Values { get; private set; } public CommandOptionType OptionType { get; private set; } - + public bool ShowInHelpText { get; set; } = true; public bool Inherited { get; set; } public bool TryParse(string value) diff --git a/test/Microsoft.Extensions.CommandLineUtils.Tests/CommandLineApplicationTests.cs b/test/Microsoft.Extensions.CommandLineUtils.Tests/CommandLineApplicationTests.cs index 906c9895822..c658be65adf 100644 --- a/test/Microsoft.Extensions.CommandLineUtils.Tests/CommandLineApplicationTests.cs +++ b/test/Microsoft.Extensions.CommandLineUtils.Tests/CommandLineApplicationTests.cs @@ -447,7 +447,7 @@ public void OptionsWithSameName() app.Execute("-a", "top"); Assert.Equal("top", top.Value()); Assert.Null(nested.Value()); - + top.Values.Clear(); app.Execute("subcmd", "-a", "nested"); @@ -501,5 +501,45 @@ public void NestedInheritedOptions() Assert.Equal("N1", nest1OptionValue); Assert.Equal("N2", nest2OptionValue); } + + [Fact] + public void HelpTextIgnoresHiddenItems() + { + var app = new CommandLineApplication() + { + Name = "ninja-app", + Description = "You can't see it until it is too late" + }; + + app.Command("star", c => + { + c.Option("--points

", "How many", CommandOptionType.MultipleValue); + c.ShowInHelpText = false; + }); + app.Option("--smile", "Be a nice ninja", CommandOptionType.NoValue, o => { o.ShowInHelpText = false; }); + + var a = app.Argument("name", "Pseudonym, of course"); + a.ShowInHelpText = false; + + var help = app.GetHelpText(); + + Assert.Contains("ninja-app", help); + Assert.DoesNotContain("--points", help); + Assert.DoesNotContain("--smile", help); + Assert.DoesNotContain("name", help); + } + + [Fact] + public void HelpTextUsesHelpOptionName() + { + var app = new CommandLineApplication + { + Name = "superhombre" + }; + + app.HelpOption("--ayuda-me"); + var help = app.GetHelpText(); + Assert.Contains("--ayuda-me", help); + } } }