|
| 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 | +``` |
0 commit comments