Skip to content

Commit 902cd96

Browse files
committed
Add a help option by default as long as it doesn't conflict with existing options.
This removes the requirement for users to specify [HelpOption] in order to get generated help text. Add [SuppressDefaultHelpOption] to the type or assembly if you wish to opt-out of this behavior change.
1 parent 87063cb commit 902cd96

25 files changed

+269
-51
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
Minor improvement:
66

7-
- Automatically add conventions from attributes that implement IConvention and IMemberConvention
7+
- Add conventions from attributes that implement IConvention and IMemberConvention
8+
- Add a help option by default as long as it doesn't conflict with existing options
89

910
## [v2.2.1]
1011

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<IsPackable>false</IsPackable>
1919
<NoPackageAnalysis>true</NoPackageAnalysis>
2020
<LangVersion>7.2</LangVersion>
21-
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)build\StrongName.snk</AssemblyOriginatorKeyFile>
21+
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)src\StrongName.snk</AssemblyOriginatorKeyFile>
2222
<SignAssembly>true</SignAssembly>
2323
<PublicSign Condition="'$(OS)' != 'Windows_NT'">true</PublicSign>
2424

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ See [samples/](./samples/) for more examples, such as:
5353
using System;
5454
using McMaster.Extensions.CommandLineUtils;
5555

56-
[HelpOption]
5756
public class Program
5857
{
5958
public static int Main(string[] args)

docfx_project/docs/concepts/help-text.md

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,97 @@
11
# Help Text
22

33
CommandLineUtils provides API to automatically generate help text for a command line application.
4-
By default, the help text will only be shown if you indicate that a help option should exit.
54

6-
## Adding a help option
5+
## Configure the help option
6+
7+
### Attribute API
8+
By default, three options will exist on the command line app that will show help: `-?`, `-h`, and `--help`.
9+
When users specify one of these, generated help text will display on the command line.
10+
11+
```
12+
> MyApp.exe --help
13+
14+
Usage: MyApp.exe [options]
15+
16+
Options:
17+
-?|-h|--help Show help output
18+
```
19+
20+
To customize the flags that can show help text, add an instance of [HelpOptionAttribute](xref:McMaster.Extensions.CommandLineUtils.HelpOptionAttribute) to your model type.
21+
22+
```csharp
23+
[HelpOption("--my-custom-help-option")]
24+
public class Program
25+
```
26+
27+
```
28+
> MyApp.exe --help
29+
Unrecognized option '--help'
30+
31+
> MyApp.exe --my-custom-help-option
32+
33+
Usage: MyApp.exe [options]
34+
35+
Options:
36+
--my-custom-help-option Show help output
37+
```
38+
39+
#### Disabling the default
40+
41+
To disable the help option by default, add `[SuppressDefaultHelpOption]` to your program type or the containing assembly.
42+
43+
```csharp
44+
// suppress the help option on all types define in this project
45+
[assembly: SuppressDefaultHelpOption]
46+
47+
// disable help option on a specific command
48+
[SuppressDefaultHelpOption]
49+
public class MySecretProgram
50+
```
51+
52+
### Builder API
53+
By default, the help text will only be shown if you define a help option on the command.
754

855
Using the **builder API**, call [`.HelpOption()`](xref:McMaster.Extensions.CommandLineUtils.CommandLineApplicationExtensions.HelpOption(McMaster.Extensions.CommandLineUtils.CommandLineApplication)).
9-
```c#
56+
57+
```csharp
1058
var app = new CommandLineApplication();
1159
app.HelpOption();
1260
```
1361

1462
This adds three flags to the command line app that will show help, `-?`, `-h`, and `--help`.
1563
You can change these flags by calling [`.HelpOption(string template)`](xref:McMaster.Extensions.CommandLineUtils.CommandLineApplication.HelpOption(System.String)) with a template string.
1664

17-
```c#
65+
```csharp
1866
app.HelpOption("-m|--my-help");
1967
```
2068

21-
When using the **attribute API**, add an instance of [HelpOptionAttribute](xref:McMaster.Extensions.CommandLineUtils.HelpOptionAttribute) to your model type.
22-
23-
```c#
24-
[HelpOption]
25-
public class Program
26-
```
27-
28-
## Add a help option to all subcommands
69+
#### Add a help option to all subcommands
2970

3071
If you have subcommands on your application, you can make a help option available on all subcommands without
3172
needing to explicitly add a HelpOption to each subcommand type or object. To do this, set `inherited` to true
3273
when adding the help option.
3374

34-
```c#
75+
```csharp
3576
app.HelpOption(inherited: true);
3677
```
3778

38-
```c#
39-
[HelpOption(Inherited = true)]
40-
public class Program
41-
```
42-
4379
## Extending help text
4480

4581
By default, the help text only includes information about arguments, options, and commands.
4682
If you would like to provide additional information, you can use the
4783
[ExtendedHelpText](xref:McMaster.Extensions.CommandLineUtils.CommandLineApplication.ExtendedHelpText)
4884
property to add additional information to help output.
4985

50-
```c#
86+
```csharp
5187
var app = new CommandLineApplication();
5288
app.ExtendedHelpText = @"
5389
Remarks:
5490
This command should only be used on Tuesdays.
5591
";
5692
```
5793

58-
```c#
94+
```csharp
5995
[Command(
6096
ExtendedHelpText = @"
6197
Remarks:
@@ -72,7 +108,7 @@ Help text generation can be completely customized by implementing the
72108
[IHelpTextGenerator](xref:McMaster.Extensions.CommandLineUtils.HelpText.IHelpTextGenerator)
73109
interface.
74110

75-
```c#
111+
```csharp
76112
class MyHelpTextGenerator : IHelpTextGenerator
77113
{
78114
public void void Generate(CommandLineApplication application, TextWriter output)

docfx_project/docs/intro.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
using System;
3737
using McMaster.Extensions.CommandLineUtils;
3838
39-
[HelpOption]
4039
public class Program
4140
{
4241
public static int Main(string[] args)

docfx_project/index.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ See [samples](https://github.com/natemcmaster/CommandLineUtils/tree/master/sampl
4040
using System;
4141
using McMaster.Extensions.CommandLineUtils;
4242

43-
[HelpOption]
4443
public class Program
4544
{
4645
public static int Main(string[] args)

samples/HelloWorld/Attributes.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using McMaster.Extensions.CommandLineUtils;
66

7-
[HelpOption]
87
public class Attributes
98
{
109
public static int Main(string[] args)

samples/HelloWorld/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// Copyright (c) Nate McMaster.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System.ComponentModel.DataAnnotations;
54
using System.Threading.Tasks;
6-
using McMaster.Extensions.CommandLineUtils;
75

86
public class Program
97
{

samples/Subcommands/BuilderApi.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Generic;
65
using McMaster.Extensions.CommandLineUtils;
76

87
namespace SubcommandSample

samples/Subcommands/NestedTypes.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ namespace SubcommandSample
1212
/// This isn't required. See the <see cref="Git"/> example for a sample on how to use inheritance.
1313
/// </summary>
1414
[Command(Name ="fake-docker", Description = "A self-sufficient runtime for containers"),
15-
HelpOption,
1615
Subcommand("container", typeof(Containers)),
1716
Subcommand("image", typeof(Images))]
1817
class Docker
@@ -30,7 +29,6 @@ private int OnExecute(CommandLineApplication app, IConsole console)
3029
/// once so that all subcommand types automatically support '--help'.
3130
/// </summary>
3231
[Command(Description = "Manage containers"),
33-
HelpOption,
3432
Subcommand("ls", typeof(List)),
3533
Subcommand("run", typeof(Run))]
3634
private class Containers
@@ -59,8 +57,7 @@ private void OnExecute(IConsole console)
5957

6058
[Command(Description = "Run a command in a new container",
6159
AllowArgumentSeparator = true,
62-
ThrowOnUnexpectedArgument = false),
63-
HelpOption]
60+
ThrowOnUnexpectedArgument = false)]
6461
private class Run
6562
{
6663
[Required(ErrorMessage = "You must specify the image name")]
@@ -84,7 +81,6 @@ private void OnExecute(IConsole console)
8481
}
8582

8683
[Command(Description = "Manage images"),
87-
HelpOption,
8884
Subcommand("ls", typeof(List))]
8985
private class Images
9086
{
@@ -96,8 +92,7 @@ private int OnExecute(IConsole console)
9692

9793

9894
[Command(Description = "List images",
99-
ThrowOnUnexpectedArgument = false),
100-
HelpOption]
95+
ThrowOnUnexpectedArgument = false)]
10196
private class List
10297
{
10398
[Option(Description = "Show all containers (default shows just running)")]

0 commit comments

Comments
 (0)