-
-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow custom help providers * Version option will show in help even with a default command * Reserve `-v` and `--version` as special Spectre.Console command line arguments (nb. breaking change for Spectre.Console users who have a default command with a settings class that uses either of these switches). * Help writer correctly determines if trailing commands exist and whether to display them as optional or mandatory in the usage statement. * Ability to control the number of indirect commands to display in the help text when the command itself doesn't have any examples of its own. Defaults to 5 (for backward compatibility) but can be set to any integer or zero to disable completely. * Significant increase in unit test coverage for the help writer. * Minor grammatical improvements to website documentation.
- Loading branch information
1 parent
813a53c
commit 131b37f
Showing
70 changed files
with
1,647 additions
and
331 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
Title: Command Help | ||
Order: 13 | ||
Description: "Console applications built with *Spectre.Console.Cli* include automatically generated help command line help." | ||
--- | ||
|
||
Console applications built with `Spectre.Console.Cli` include automatically generated help which is displayed when `-h` or `--help` has been specified on the command line. | ||
|
||
The automatically generated help is derived from the configured commands and their command settings. | ||
|
||
The help is also context aware and tailored depending on what has been specified on the command line before it. For example, | ||
|
||
1. When `-h` or `--help` appears immediately after the application name (eg. `application.exe --help`), then the help displayed is a high-level summary of the application, including any command line examples and a listing of all possible commands the user can execute. | ||
|
||
2. When `-h` or `--help` appears immediately after a command has been specified (eg. `application.exe command --help`), then the help displayed is specific to the command and includes information about command specific switches and any default values. | ||
|
||
`HelpProvider` is the `Spectre.Console` class responsible for determining context and preparing the help text to write to the console. It is an implementation of the public interface `IHelpProvider`. | ||
|
||
## Custom help providers | ||
|
||
Whilst it shouldn't be common place to implement your own help provider, it is however possible. | ||
|
||
You are able to implement your own `IHelpProvider` and configure a `CommandApp` to use that instead of the Spectre.Console help provider. | ||
|
||
```csharp | ||
using Spectre.Console.Cli; | ||
|
||
namespace Help; | ||
|
||
public static class Program | ||
{ | ||
public static int Main(string[] args) | ||
{ | ||
var app = new CommandApp<DefaultCommand>(); | ||
|
||
app.Configure(config => | ||
{ | ||
// Register the custom help provider | ||
config.SetHelpProvider(new CustomHelpProvider(config.Settings)); | ||
}); | ||
|
||
return app.Run(args); | ||
} | ||
} | ||
``` | ||
|
||
There is a working [example of a custom help provider](https://github.com/spectreconsole/spectre.console/tree/main/examples/Cli/Help) demonstrating this. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Linq; | ||
using Spectre.Console; | ||
using Spectre.Console.Cli; | ||
using Spectre.Console.Cli.Help; | ||
using Spectre.Console.Rendering; | ||
|
||
namespace Help; | ||
|
||
/// <summary> | ||
/// Example showing how to extend the built-in Spectre.Console help provider | ||
/// by rendering a custom banner at the top of the help information | ||
/// </summary> | ||
internal class CustomHelpProvider : HelpProvider | ||
{ | ||
public CustomHelpProvider(ICommandAppSettings settings) | ||
: base(settings) | ||
{ | ||
} | ||
|
||
public override IEnumerable<IRenderable> GetHeader(ICommandModel model, ICommandInfo? command) | ||
{ | ||
return new[] | ||
{ | ||
new Text("--------------------------------------"), Text.NewLine, | ||
new Text("--- CUSTOM HELP PROVIDER ---"), Text.NewLine, | ||
new Text("--------------------------------------"), Text.NewLine, | ||
Text.NewLine, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Spectre.Console; | ||
using Spectre.Console.Cli; | ||
|
||
namespace Help; | ||
|
||
public sealed class DefaultCommand : Command | ||
{ | ||
private IAnsiConsole _console; | ||
|
||
public DefaultCommand(IAnsiConsole console) | ||
{ | ||
_console = console; | ||
} | ||
|
||
public override int Execute(CommandContext context) | ||
{ | ||
_console.WriteLine("Hello world"); | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<ExampleName>Help</ExampleName> | ||
<ExampleDescription>Demonstrates how to extend the built-in Spectre.Console help provider to render a custom banner at the top of the help information.</ExampleDescription> | ||
<ExampleGroup>Cli</ExampleGroup> | ||
<ExampleVisible>false</ExampleVisible> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\Shared\Shared.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Spectre.Console.Cli; | ||
|
||
namespace Help; | ||
|
||
public static class Program | ||
{ | ||
public static int Main(string[] args) | ||
{ | ||
var app = new CommandApp<DefaultCommand>(); | ||
|
||
app.Configure(config => | ||
{ | ||
// Register the custom help provider | ||
config.SetHelpProvider(new CustomHelpProvider(config.Settings)); | ||
}); | ||
|
||
return app.Run(args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.