Skip to content

Commit

Permalink
fix: update help text generator to display HelpOption correctly (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
daveMueller authored and natemcmaster committed Jan 2, 2020
1 parent 4fe7d70 commit fcfd448
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/CommandLineUtils/CommandLineApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ public virtual void ShowHint()
var flag = !string.IsNullOrEmpty(OptionHelp.LongName)
? "--" + OptionHelp.LongName
: !string.IsNullOrEmpty(OptionHelp.ShortName)
? "-" + OptionHelp.LongName
? "-" + OptionHelp.ShortName
: "-" + OptionHelp.SymbolName;

Out.WriteLine($"Specify {flag} for a list of available options and commands.");
Expand Down
2 changes: 1 addition & 1 deletion src/CommandLineUtils/HelpText/DefaultHelpTextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ protected virtual void GenerateCommands(
if (application.OptionHelp != null)
{
output.WriteLine();
output.WriteLine($"Run '{application.Name} [command] --{application.OptionHelp.LongName}' for more information about a command.");
output.WriteLine($"Run '{application.Name} [command] {Format(application.OptionHelp)}' for more information about a command.");
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions test/CommandLineUtils.Tests/CommandLineApplicationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Moq;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -1052,5 +1053,21 @@ public async Task OperationCanceledReturnsExpectedOsCode()
var exitCode = await executeTask;
Assert.Equal(expectedCode, exitCode);
}

[Theory]
[InlineData("-h")]
[InlineData("--h")]
[InlineData("-?")]
public void ShowHintDisplaysValidInfo(string helpOption)
{
var app = new CommandLineApplication();
var textWriter = new Mock<TextWriter>();
app.Out = textWriter.Object;
app.HelpOption(helpOption);

app.ShowHint();

textWriter.Verify(mock => mock.WriteLine($"Specify {helpOption} for a list of available options and commands."), Times.Once);
}
}
}
31 changes: 31 additions & 0 deletions test/CommandLineUtils.Tests/DefaultHelpTextGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using McMaster.Extensions.CommandLineUtils.HelpText;
Expand Down Expand Up @@ -175,5 +177,34 @@ public class MyApp
[Argument(1, Description = "enum arg desc")]
public SomeEnum SomeEnumArgument { get; set; }
}

[Theory]
[InlineData("-h", "-h", " -h Show help information", " Subcommand ")]
[InlineData("--help", "--help", " --help Show help information", " Subcommand ")]
[InlineData("-?", "-?", " -? Show help information", " Subcommand ")]
[InlineData(null, "-?|-h|--help", " -?|-h|--help Show help information", " Subcommand ")]
public void ShowHelpWithSubcommands(string helpOption, string expectedHintText, string expectedOptionsText,
string expectedCommandsText)
{
var app = new CommandLineApplication {Name = "test"};
if (helpOption != null) app.HelpOption(helpOption);
app.Command("Subcommand", _ => { });
app.Conventions.UseDefaultConventions();
var helpText = GetHelpText(app);

Assert.Equal($@"Usage: test [command] [options]
Options:
{expectedOptionsText}
Commands:
{expectedCommandsText}
Run 'test [command] {expectedHintText}' for more information about a command.
",
helpText,
ignoreLineEndingDifferences: true);
}
}
}

0 comments on commit fcfd448

Please sign in to comment.