|
| 1 | +# Customizing Help for Commands |
| 2 | + |
| 3 | +Define your command's abstract, extended discussion, or usage string, and set the flags used to invoke the help display. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +In addition to configuring the command name and subcommands, as described in <doc:CommandsAndSubcommands>, you can also configure a command's help text by providing an abstract, discussion, or custom usage string. |
| 8 | + |
| 9 | +```swift |
| 10 | +struct Repeat: ParsableCommand { |
| 11 | + static var configuration = CommandConfiguration( |
| 12 | + abstract: "Repeats your input phrase.", |
| 13 | + usage: """ |
| 14 | + repeat <phrase> |
| 15 | + repeat --count <count> <phrase> |
| 16 | + """, |
| 17 | + discussion: """ |
| 18 | + Prints to stdout forever, or until you halt the program. |
| 19 | + """) |
| 20 | + |
| 21 | + @Argument(help: "The phrase to repeat.") |
| 22 | + var phrase: String |
| 23 | + |
| 24 | + @Option(help: "How many times to repeat.") |
| 25 | + var count: Int? |
| 26 | + |
| 27 | + mutating func run() throws { |
| 28 | + for _ in 0..<(count ?? Int.max) { |
| 29 | + print(phrase) |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +The customized components now appear in the generated help screen: |
| 36 | + |
| 37 | +``` |
| 38 | +% repeat --help |
| 39 | +OVERVIEW: Repeats your input phrase. |
| 40 | +
|
| 41 | +Prints to stdout forever, or until you halt the program. |
| 42 | +
|
| 43 | +USAGE: repeat <phrase> |
| 44 | + repeat --count <count> <phrase> |
| 45 | +
|
| 46 | +ARGUMENTS: |
| 47 | + <phrase> The phrase to repeat. |
| 48 | +
|
| 49 | +OPTIONS: |
| 50 | + -h, --help Show help information. |
| 51 | +
|
| 52 | +% repeat hello! |
| 53 | +hello! |
| 54 | +hello! |
| 55 | +hello! |
| 56 | +hello! |
| 57 | +hello! |
| 58 | +hello! |
| 59 | +... |
| 60 | +``` |
| 61 | + |
| 62 | +## Modifying the Help Flag Names |
| 63 | + |
| 64 | +Users can see the help screen for a command by passing either the `-h` or the `--help` flag, by default. If you need to use one of those flags for another purpose, you can provide alternative names when configuring a root command. |
| 65 | + |
| 66 | +```swift |
| 67 | +struct Example: ParsableCommand { |
| 68 | + static let configuration = CommandConfiguration( |
| 69 | + helpNames: [.long, .customShort("?")]) |
| 70 | + |
| 71 | + @Option(name: .shortAndLong, help: "The number of history entries to show.") |
| 72 | + var historyDepth: Int |
| 73 | + |
| 74 | + mutating func run() throws { |
| 75 | + printHistory(depth: historyDepth) |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +When running the command, `-h` matches the short name of the `historyDepth` property, and `-?` displays the help screen. |
| 81 | + |
| 82 | +``` |
| 83 | +% example -h 3 |
| 84 | +nmap -v -sS -O 10.2.2.2 |
| 85 | +sshnuke 10.2.2.2 -rootpw="Z1ON0101" |
| 86 | +ssh 10.2.2.2 -l root |
| 87 | +% example -? |
| 88 | +USAGE: example --history-depth <history-depth> |
| 89 | +
|
| 90 | +ARGUMENTS: |
| 91 | + <phrase> The phrase to repeat. |
| 92 | +
|
| 93 | +OPTIONS: |
| 94 | + -h, --history-depth The number of history entries to show. |
| 95 | + -?, --help Show help information. |
| 96 | +``` |
| 97 | + |
| 98 | +When not overridden, custom help names are inherited by subcommands. In this example, the parent command defines `--help` and `-?` as its help names: |
| 99 | + |
| 100 | +```swift |
| 101 | +struct Parent: ParsableCommand { |
| 102 | + static let configuration = CommandConfiguration( |
| 103 | + subcommands: [Child.self], |
| 104 | + helpNames: [.long, .customShort("?")]) |
| 105 | + |
| 106 | + struct Child: ParsableCommand { |
| 107 | + @Option(name: .shortAndLong, help: "The host the server will run on.") |
| 108 | + var host: String |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +The `child` subcommand inherits the parent's help names, allowing the user to distinguish between the host argument (`-h`) and help (`-?`). |
| 114 | + |
| 115 | +``` |
| 116 | +% parent child -h 192.0.0.0 |
| 117 | +... |
| 118 | +% parent child -? |
| 119 | +USAGE: parent child --host <host> |
| 120 | +
|
| 121 | +OPTIONS: |
| 122 | + -h, --host <host> The host the server will run on. |
| 123 | + -?, --help Show help information. |
| 124 | +``` |
| 125 | + |
| 126 | +## Hiding Commands |
| 127 | + |
| 128 | +You may not want to show every one of your command as part of your command-line interface. To render a command invisible (but still usable), pass `shouldDisplay: false` to the ``CommandConfiguration`` initializer. |
| 129 | + |
| 130 | +## Generating Help Text Programmatically |
| 131 | + |
| 132 | +The help screen is automatically shown to users when they call your command with the help flag. You can generate the same text from within your program by calling the `helpMessage()` method. |
| 133 | + |
| 134 | +```swift |
| 135 | +let help = Repeat.helpMessage() |
| 136 | +// `help` matches the output above |
| 137 | + |
| 138 | +let fortyColumnHelp = Repeat.helpMessage(columns: 40) |
| 139 | +// `fortyColumnHelp` is the same help screen, but wrapped to 40 columns |
| 140 | +``` |
| 141 | + |
| 142 | +When generating help text for a subcommand, call `helpMessage(for:)` on the `ParsableCommand` type that represents the root of the command tree and pass the subcommand type as a parameter to ensure the correct display. |
0 commit comments