Skip to content

Commit 9a82823

Browse files
committed
update Help customization based on our recent changes
1 parent 3153d7a commit 9a82823

File tree

4 files changed

+92
-205
lines changed

4 files changed

+92
-205
lines changed

docs/standard/commandline/help.md

Lines changed: 39 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,27 @@ ms.topic: how-to
1212

1313
## Help option
1414

15-
Command-line apps typically provide an option to display a brief description of the available commands, options, and arguments. `System.CommandLine` provides <xref:System.CommandLine.Help.HelpOption> that is by default included in the [RootCommand](syntax.md#root-command) options. It generates help output.
16-
For example:
15+
Command-line apps typically provide an option to display a brief description of the available commands, options, and arguments. `System.CommandLine` provides <xref:System.CommandLine.Help.HelpOption> that is by default included in the [RootCommand](syntax.md#root-command) options. <xref:System.CommandLine.Help.HelpOption> generates help output for defined symbols by using the information exposed by <xref:System.CommandLine.Symbol.Name>, <xref:System.CommandLine.Symbol.HelpName>, <xref:System.CommandLine.Symbol.Description>, and other properties like default value or completion sources.
1716

18-
```dotnetcli
19-
dotnet list --help
20-
```
17+
:::code language="csharp" source="snippets/customize-help/csharp/Program.cs" id="original" :::
2118

22-
```output
19+
```dotnetcli
2320
Description:
24-
List references or packages of a .NET project.
21+
Read a file
2522
2623
Usage:
27-
dotnet [options] list [<PROJECT | SOLUTION>] [command]
28-
29-
Arguments:
30-
<PROJECT | SOLUTION> The project or solution file to operate on. If a file is not specified, the command will search the current directory for one.
24+
scl [options]
3125
3226
Options:
33-
-?, -h, --help Show command line help.
34-
35-
Commands:
36-
package List all package references of the project or solution.
37-
reference List all project-to-project references of the project.
27+
-?, -h, --help Show help and usage information
28+
--version Show version information
29+
--file The file to print out.
30+
--light-mode Determines whether the background color will be black
31+
or white
32+
--color Specifies the foreground color of console output
33+
<Black|Blue|Cyan|DarkBlue|DarkCyan|DarkGray|DarkGreen|Da [default: White]
34+
rkMagenta|DarkRed|DarkYellow|Gray|Green|Magenta|Red|Whit
35+
e|Yellow>
3836
```
3937

4038
App users might be accustomed to different ways to request help on different platforms, so apps built on `System.CommandLine` respond to many ways of requesting help. The following commands are all equivalent:
@@ -49,63 +47,17 @@ dotnet /?
4947

5048
Help output doesn't necessarily show all available commands, arguments, and options. Some of them may be *hidden* via the <xref:System.CommandLine.Symbol.Hidden> property, which means they don't show up in help output (and completions) but they can be specified on the command line.
5149

52-
## How to customize help in apps that are built with the System.Commandline library
53-
54-
You can customize help for a specific command, option, or argument, and you can add or replace whole help sections.
50+
## Help customization
5551

56-
The examples in this article work with the following command-line application:
52+
You can customize help output for commands by defining specific help text for each symbol, providing further clarity to users regarding their usage.
5753

58-
This code requires a `using` directive:
54+
To customize the name of an option's argument, use the option's <xref:System.CommandLine.Option.HelpName> property.
5955

60-
```csharp
61-
using System.CommandLine;
62-
```
63-
64-
:::code language="csharp" source="snippets/customize-help/csharp/Program.cs" id="original" :::
56+
In the sample app, `--light-mode` is explained adequately, but changes to the `--file` and `--color` option descriptions will be helpful. For `--file`, the argument can be identified as a `<FILEPATH>`. For the `--color` option, you can shorten the list of available colors.
6557

66-
Without customization, the following help output is produced:
67-
68-
```output
69-
Description:
70-
Read a file
58+
To make these changes, extend the previous code with the following code:
7159

72-
Usage:
73-
scl [options]
74-
75-
Options:
76-
--file <file> The file to print out. [default: scl.runtimeconfig.json]
77-
--light-mode Determines whether the background color will be black or
78-
white
79-
--color Specifies the foreground color of console output
80-
<Black|Blue|Cyan|DarkBlue|DarkCyan|DarkGray|DarkGreen|Dark [default: White]
81-
Magenta|DarkRed|DarkYellow|Gray|Green|Magenta|Red|White|Ye
82-
llow>
83-
--version Show version information
84-
-?, -h, --help Show help and usage information
85-
```
86-
87-
## Customize help for a single option or argument
88-
89-
[!INCLUDE [scl-preview](../../../includes/scl-preview.md)]
90-
91-
To customize the name of an option's argument, use the option's <xref:System.CommandLine.Option.HelpName> property. And <xref:System.CommandLine.Help.HelpBuilder.CustomizeSymbol%2A?displayProperty=nameWithType> lets you customize several parts of the help output for a command, option, or argument (<xref:System.CommandLine.Symbol> is the base class for all three types). With `CustomizeSymbol`, you can specify:
92-
93-
* The first column text.
94-
* The second column text.
95-
* The way a default value is described.
96-
97-
In the sample app, `--light-mode` is explained adequately, but changes to the `--file` and `--color` option descriptions will be helpful. For `--file`, the argument can be identified as a `<FILEPATH>` instead of `<file>`. For the `--color` option, you can shorten the list of available colors in column one, and in column two you can add a warning that some colors won't work with some backgrounds.
98-
99-
To make these changes, delete the `rootCommand.Parse(args).Invoke();` line shown in the preceding code and add in its place the following code:
100-
101-
:::code language="csharp" source="snippets/customize-help/csharp/Program.cs" id="first2columns" :::
102-
103-
The updated code requires additional `using` directives:
104-
105-
```csharp
106-
using System.CommandLine.Builder;
107-
using System.CommandLine.Help;
108-
```
60+
:::code language="csharp" source="snippets/customize-help/csharp/Program.cs" id="allowedvalues" :::
10961

11062
The app now produces the following help output:
11163

@@ -117,32 +69,24 @@ Usage:
11769
scl [options]
11870
11971
Options:
120-
--file <FILEPATH> The file to print out. [default: CustomHelp.runtimeconfig.json]
121-
--light-mode Determines whether the background color will be black or white
122-
--color <Black, White, Red, or Yellow> Specifies the foreground color. Choose a color that provides enough contrast
123-
with the background color. For example, a yellow foreground can't be read
124-
against a light mode background.
125-
--version Show version information
126-
-?, -h, --help Show help and usage information
72+
-?, -h, --help Show help and usage information
73+
--version Show version information
74+
--file <FILEPATH> The file to print out.
75+
--light-mode Determines whether the background color will be black or white
76+
--color <Black|Red|White|Yellow> Specifies the foreground color of console output [default: White]
12777
```
12878

129-
This output shows that the `firstColumnText` and `secondColumnText` parameters support word wrapping within their columns.
130-
131-
## Add or replace help sections
79+
## Add sections to help output
13280

133-
You can add or replace a whole section of the help output. For example, suppose you want to add some ASCII art to the description section by using the [Spectre.Console](https://www.nuget.org/packages/Spectre.Console/) NuGet package.
81+
You can add first or last sections to the help output. For example, suppose you want to add some ASCII art to the description section by using the [Spectre.Console](https://www.nuget.org/packages/Spectre.Console/) NuGet package.
13482

135-
Change the layout by adding a call to <xref:System.CommandLine.Help.HelpBuilder.CustomizeLayout%2A?displayProperty=nameWithType>:
83+
Define a custom action that performs some extra logic before and after calling the default `HelpAction`:
13684

137-
:::code language="csharp" source="snippets/customize-help/csharp/Program.cs" id="description" highlight="14-22" :::
85+
:::code language="csharp" source="snippets/customize-help/csharp/Program.cs" id="customaction" :::
13886

139-
The preceding code requires an additional `using` directive:
140-
141-
```csharp
142-
using Spectre.Console;
143-
```
87+
Update the `HelpAction` defined by `RootCommand` to use the custom action:
14488

145-
The <xref:System.CommandLine.Help.HelpBuilder.Default?displayProperty=nameWithType> class lets you reuse pieces of existing help formatting functionality and compose them into your custom help.
89+
:::code language="csharp" source="snippets/customize-help/csharp/Program.cs" id="setcustomaction" highlight="5" :::
14690

14791
The help output now looks like this:
14892

@@ -153,28 +97,20 @@ The help output now looks like this:
15397
| _ < | __/ | (_| | | (_| | | (_| | | _| | | | | | __/
15498
|_| \_\ \___| \__,_| \__,_| \__,_| |_| |_| |_| \___|
15599
100+
Description:
101+
Read a file
156102
157103
Usage:
158104
scl [options]
159105
160106
Options:
161-
--file <FILEPATH> The file to print out. [default: CustomHelp.runtimeconfig.json]
162-
--light-mode Determines whether the background color will be black or white
163-
--color <Black, White, Red, or Yellow> Specifies the foreground color. Choose a color that provides enough contrast
164-
with the background color. For example, a yellow foreground can't be read
165-
against a light mode background.
166-
--version Show version information
167-
-?, -h, --help Show help and usage information
168-
```
169-
170-
If you want to just use a string as the replacement section text instead of formatting it with `Spectre.Console`, replace the `Prepend` code in the preceding example with the following code:
107+
-?, -h, --help Show help and usage information
108+
--version Show version information
109+
--file <FILEPATH> The file to print out.
110+
--light-mode Determines whether the background color will be black or white
111+
--color <Black|Red|White|Yellow> Specifies the foreground color of console output [default: White]
171112
172-
```csharp
173-
.Prepend(helpContext =>
174-
{
175-
helpContext.Output.WriteLine("**New command description section**");
176-
return true;
177-
})
113+
Sample usage: --file input.txt
178114
```
179115

180116
## See also

docs/standard/commandline/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ To learn more, see the following resources:
4646

4747
* [How to bind arguments to handlers](model-binding.md)
4848
* [How to configure dependency injection](dependency-injection.md)
49-
* [How to configure help in System.CommandLine](Help.md)
49+
* [How to configure help in System.CommandLine](help.md)
5050
* [How to enable and customize tab completion](tab-completion.md)
5151
* [How to handle termination](handle-termination.md)
5252
* [System.CommandLine API reference](xref:System.CommandLine)

0 commit comments

Comments
 (0)