Skip to content

Commit

Permalink
Allow elements to be hidden from help text (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
natemcmaster committed Jun 30, 2016
1 parent 1380d8d commit 18b4187
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ nuget.exe
*.sln.ide
node_modules
**/[Cc]ompiler/[Rr]esources/**/*.js
.vscode/
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public CommandArgument()
}

public string Name { get; set; }
public bool ShowInHelpText { get; set; } = true;
public string Description { get; set; }
public List<string> Values { get; private set; }
public bool MultipleValues { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommandOption> Options;
public CommandOption OptionHelp { get; private set; }
public CommandOption OptionVersion { get; private set; }
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]");
Expand All @@ -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();
Expand All @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public CommandOption(string template, CommandOptionType optionType)
public string Description { get; set; }
public List<string> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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 <p>", "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);
}
}
}

0 comments on commit 18b4187

Please sign in to comment.