feat: add command usage examples
Add an Examples string array property to CommandAttribute so command authors can document example invocations. Each example contains only the arguments that follow the command name; the executable and command name are prepended automatically when the example is rendered. Examples are rendered as extra lines in the USAGE section of the help text. The source generator resolves the values from the attribute and emits them into the command descriptor consumed at runtime.
This commit is contained in:
1 parent
e3e64a40e4
commit
8f11b8ff38
6 files changed
+132
-3
No files matched your search
@@ -381,6 +381,81 @@ public class HelpSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput)
|
||||
.ContainAllInOrder("USAGE", "--foo <value>", "--baz <values...>", "[options]");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task I_can_request_help_to_see_the_usage_examples_of_a_command()
|
||||
{
|
||||
// Arrange
|
||||
var application = new CommandLineApplicationBuilder()
|
||||
.AddCommands(
|
||||
CommandCompiler.Compile(
|
||||
// lang=csharp
|
||||
"""
|
||||
[Command("foo", Examples = new[] { "--bar 42", "-b 42" })]
|
||||
public partial class Command : ICommand
|
||||
{
|
||||
[CommandOption("bar", 'b')]
|
||||
public int Bar { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
"""
|
||||
)
|
||||
)
|
||||
.UseConsole(FakeConsole)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
["foo", "--help"],
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut
|
||||
.Should()
|
||||
.ContainAllInOrder("USAGE", "foo [options]", "foo --bar 42", "foo -b 42", "OPTIONS");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task I_do_not_see_any_usage_examples_if_the_command_does_not_define_any()
|
||||
{
|
||||
// Arrange
|
||||
var application = new CommandLineApplicationBuilder()
|
||||
.AddCommands(
|
||||
CommandCompiler.Compile(
|
||||
// lang=csharp
|
||||
"""
|
||||
[Command("foo")]
|
||||
public partial class Command : ICommand
|
||||
{
|
||||
[CommandOption("bar", 'b')]
|
||||
public int Bar { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
"""
|
||||
)
|
||||
)
|
||||
.UseConsole(FakeConsole)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
["foo", "--help"],
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().Contain("foo [options]");
|
||||
stdOut.Should().NotContain("foo --bar 42");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task I_can_request_help_to_see_the_list_of_all_inputs()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user