Skip to content

Commit 0c26dde

Browse files
committed
Add docs for help text, and a test to ensure HelpOption is inherited via attribute API
1 parent 59f11a6 commit 0c26dde

File tree

3 files changed

+124
-1
lines changed

3 files changed

+124
-1
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Help Text
2+
3+
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.
5+
6+
## Adding a help option
7+
8+
Using the **builder API**, call [`.HelpOption()`](xref:McMaster.Extensions.CommandLineUtils.CommandLineApplicationExtensions.HelpOption(McMaster.Extensions.CommandLineUtils.CommandLineApplication)).
9+
```c#
10+
var app = new CommandLineApplication();
11+
app.HelpOption();
12+
```
13+
14+
This adds three flags to the command line app that will show help, `-?`, `-h`, and `--help`.
15+
You can change these flags by calling [`.HelpOption(string template)`](xref:McMaster.Extensions.CommandLineUtils.CommandLineApplication.HelpOption(System.String)) with a template string.
16+
17+
```c#
18+
app.HelpOption("-m|--my-help");
19+
```
20+
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
29+
30+
If you have subcommands on your application, you can make a help option available on all subcommands without
31+
needing to explicitly add a HelpOption to each subcommand type or object. To do this, set `inherited` to true
32+
when adding the help option.
33+
34+
```c#
35+
app.HelpOption(inherited: true);
36+
```
37+
38+
```c#
39+
[HelpOption(Inherited = true)]
40+
public class Program
41+
```
42+
43+
## Extending help text
44+
45+
By default, the help text only includes information about arguments, options, and commands.
46+
If you would like to provide additional information, you can use the
47+
[ExtendedHelpText](xref:McMaster.Extensions.CommandLineUtils.CommandLineApplication.ExtendedHelpText)
48+
property to add additional information to help output.
49+
50+
```c#
51+
var app = new CommandLineApplication();
52+
app.ExtendedHelpText = @"
53+
Remarks:
54+
This command should only be used on Tuesdays.
55+
";
56+
```
57+
58+
```c#
59+
[Command(
60+
ExtendedHelpText = @"
61+
Remarks:
62+
This command should only be used on Tuesdays.
63+
"
64+
)]
65+
public class Program
66+
67+
```
68+
69+
## Custom help text
70+
71+
Help text generation can be completely customized by implementing the
72+
[IHelpTextGenerator](xref:McMaster.Extensions.CommandLineUtils.HelpText.IHelpTextGenerator)
73+
interface.
74+
75+
```c#
76+
class MyHelpTextGenerator : IHelpTextGenerator
77+
{
78+
public void void Generate(CommandLineApplication application, TextWriter output)
79+
{
80+
output.WriteLine(@"To use this command, throw salt over your shoulder and spit twice.");
81+
}
82+
}
83+
84+
class Program
85+
{
86+
public static int Main(string[] args)
87+
{
88+
var app = new CommandLineApplication();
89+
app.HelpTextGenerator = new MyHelpTextGenerator();
90+
return app.Execute(args);
91+
}
92+
}
93+
```

docfx_project/docs/toc.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# [Introduction](intro.md)
22
# Concepts
3-
## [Dependency Injection](concepts/dependency-injection.md)
3+
## [Dependency Injection](concepts/dependency-injection.md)
4+
## [Help Text](concepts/help-text.md)

test/CommandLineUtils.Tests/HelpOptionAttributeTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
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.IO;
56
using System.Reflection;
7+
using System.Text;
68
using Xunit;
79
using Xunit.Abstractions;
810

@@ -147,5 +149,32 @@ public void OnExecuteIsNotInvokedWhenHelpOptionSpecified(string arg)
147149
{
148150
Assert.Equal(0, CommandLineApplication.Execute<SimpleHelpApp>(new TestConsole(_output), arg));
149151
}
152+
153+
[Command(Name = "lvl1")]
154+
[HelpOption(Inherited = true)]
155+
[Subcommand("lvl2", typeof(Sub))]
156+
private class Parent
157+
{
158+
private class Sub
159+
{
160+
[Argument(0, Name = "lvl-arg", Description = "subcommand argument")]
161+
public string Arg { get; }
162+
}
163+
}
164+
165+
[Fact]
166+
public void HelpOptionIsInherited()
167+
{
168+
var sb = new StringBuilder();
169+
var outWriter = new StringWriter(sb);
170+
var app = new CommandLineApplication<Parent> { Out = outWriter };
171+
app.Conventions.UseDefaultConventions();
172+
app.Commands.ForEach(f => f.Out = outWriter);
173+
app.Execute("lvl2", "--help");
174+
var outData = sb.ToString();
175+
176+
Assert.True(app.OptionHelp.HasValue());
177+
Assert.Contains("Usage: lvl1 lvl2 [arguments] [options]", outData);
178+
}
150179
}
151180
}

0 commit comments

Comments
 (0)