Skip to content

Commit abe20c5

Browse files
committed
docs: improve docs site
Signed-off-by: Christian Stewart <christian@aperture.us>
1 parent 177bac8 commit abe20c5

15 files changed

+182
-148
lines changed

docs/examples/arguments.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ search:
33
boost: 2
44
---
55

6-
You can lookup arguments by calling the `Args` function on `cli.Context`, e.g.:
6+
Accessing command-line arguments (values passed after the command and flags) is straightforward using the `Args` method on `cli.Context`. Here's an example:
77

88
<!-- {
99
"output": "Hello \""

docs/examples/bash-completions.md

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ search:
33
boost: 2
44
---
55

6-
You can enable completion commands by setting the `EnableBashCompletion` flag on
7-
the `App` object to `true`. By default, this setting will allow auto-completion
8-
for an app's subcommands, but you can write your own completion methods for the
9-
App or its subcommands as well.
6+
You can enable built-in bash completion support by setting the `EnableBashCompletion` field on your `cli.App` to `true`. This automatically provides completion suggestions for your app's subcommands. You can also define custom completion logic for specific commands or flags.
107

118
#### Default auto-completion
129

@@ -128,16 +125,13 @@ func main() {
128125

129126
#### Enabling
130127

131-
To enable auto-completion for the current shell session, a bash script,
132-
`autocomplete/bash_autocomplete` is included in this repo.
128+
To enable auto-completion for your application in the current shell session, you can use the `autocomplete/bash_autocomplete` script provided in the `aperturerobotics/cli` repository.
133129

134130
> :warning: The `bash-completion` package or equivalent that provides the
135131
> `_get_comp_words_by_ref` function for the target platform must be installed and
136132
> initialized for this completion script to work correctly.
137133
138-
To use `autocomplete/bash_autocomplete` set an environment variable named `PROG`
139-
to the name of your program and then `source` the
140-
`autocomplete/bash_autocomplete` file.
134+
First, set the `PROG` environment variable to the name of your compiled application binary. Then, `source` the `autocomplete/bash_autocomplete` script:
141135

142136
For example, if your cli program is called `myprogram`:
143137

@@ -150,19 +144,16 @@ a new shell.
150144

151145
#### Distribution and Persistent Autocompletion
152146

153-
Copy `autocomplete/bash_autocomplete` into `/etc/bash_completion.d/` and rename
154-
it to the name of the program you wish to add autocomplete support for (or
155-
automatically install it there if you are distributing a package). Don't forget
156-
to source the file or restart your shell to activate the auto-completion.
147+
To make autocompletion persistent across shell sessions, you have a few options:
148+
149+
1. **System-wide Installation:** Copy `autocomplete/bash_autocomplete` to `/etc/bash_completion.d/` and rename it to match your program's name (e.g., `/etc/bash_completion.d/myprogram`). This is common when distributing packages. Users may need to restart their shell or source the file manually (`source /etc/bash_completion.d/<myprogram>`) for the changes to take effect immediately.
157150

158151
```sh-session
159152
$ sudo cp path/to/autocomplete/bash_autocomplete /etc/bash_completion.d/<myprogram>
160153
$ source /etc/bash_completion.d/<myprogram>
161154
```
162155

163-
Alternatively, you can just document that users should `source` the generic
164-
`autocomplete/bash_autocomplete` and set `$PROG` within their bash configuration
165-
file, adding these lines:
156+
2. **User Configuration:** Instruct users to add the following lines to their shell configuration file (e.g., `~/.bashrc` or `~/.bash_profile`), ensuring they replace `<myprogram>` with the actual program name and `path/to/cli` with the correct path to the script:
166157

167158
```sh-session
168159
$ PROG=<myprogram>

docs/examples/combining-short-options.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ search:
33
boost: 2
44
---
55

6-
Traditional use of options using their shortnames look like this:
6+
Normally, short options (like `-s`, `-o`, `-m`) are specified individually:
77

88
```sh-session
99
$ cmd -s -o -m "Some message"
1010
```
1111

12-
Suppose you want users to be able to combine options with their shortnames. This
13-
can be done using the `UseShortOptionHandling` bool in your app configuration,
14-
or for individual commands by attaching it to the command configuration. For
15-
example:
12+
If you want to allow users to combine multiple short boolean options (and optionally one non-boolean short option at the end), you can enable this behavior by setting `UseShortOptionHandling` to `true` on your `cli.App` or a specific `cli.Command`.
13+
14+
Here's an example:
1615

1716
<!-- {
1817
"args": ["short", "&#45;som", "Some message"],
@@ -66,7 +65,4 @@ following syntax:
6665
$ cmd -som "Some message"
6766
```
6867

69-
If you enable `UseShortOptionHandling`, then you must not use any flags that
70-
have a single leading `-` or this will result in failures. For example,
71-
`-option` can no longer be used. Flags with two leading dashes (such as
72-
`--options`) are still valid.
68+
**Important:** When `UseShortOptionHandling` is enabled, you cannot define flags that use a single dash followed by multiple characters (e.g., `-option`). This syntax becomes ambiguous with combined short options. Standard double-dash flags (e.g., `--option`) remain unaffected.

docs/examples/exit-codes.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ search:
33
boost: 2
44
---
55

6-
Calling `App.Run` will not automatically call `os.Exit`, which means that by
7-
default the exit code will "fall through" to being `0`. An explicit exit code
8-
may be set by returning a non-nil error that fulfills `cli.ExitCoder`, *or* a
9-
`cli.MultiError` that includes an error that fulfills `cli.ExitCoder`, e.g.:
6+
By default, `App.Run` does not call `os.Exit`. If your application's `Action` (or subcommand actions) returns `nil`, the process exits with code `0`. To exit with a specific non-zero code, return an error that implements the `cli.ExitCoder` interface. The `cli.Exit` helper function is provided for convenience. If using `cli.MultiError`, the exit code will be determined by the first `cli.ExitCoder` found within the wrapped errors.
7+
8+
Here's an example using `cli.Exit`:
109
<!-- {
1110
"error": "Ginger croutons are not in the soup"
1211
} -->

docs/examples/flags.md

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ search:
33
boost: 2
44
---
55

6-
Setting and querying flags is simple.
6+
Flags provide ways for users to modify the behavior of your command-line application. Defining and accessing flags is straightforward.
77

88
<!-- {
99
"output": "Hello Nefertiti"
@@ -48,9 +48,7 @@ func main() {
4848
}
4949
```
5050

51-
You can also set a destination variable for a flag, to which the content will be
52-
scanned. Note that if the `Value` is set for the flag, it will be shown as default,
53-
and destination will be set to this value before parsing flag on the command line.
51+
You can also bind a flag directly to a variable in your code using the `Destination` field. The flag's value will be automatically parsed and stored in the specified variable. If a default `Value` is also set for the flag, the `Destination` variable will be initialized to this default before command-line arguments are parsed.
5452

5553
<!-- {
5654
"output": "Hello someone"
@@ -98,11 +96,11 @@ func main() {
9896
}
9997
```
10098

101-
See full list of flags at https://pkg.go.dev/github.com/aperturerobotics/cli
99+
See the [Go Reference](https://pkg.go.dev/github.com/aperturerobotics/cli) for a full list of available flag types.
102100

103-
For bool flags you can specify the flag multiple times to get a count(e.g -v -v -v or -vvv)
101+
For boolean flags (`BoolFlag`), you can use the `Count` field to track how many times a flag is provided. This is useful for flags like `-v` for verbosity.
104102

105-
> If you want to support the `-vvv` flag, you need to set `App.UseShortOptionHandling`.
103+
> Note: To support combining short boolean flags like `-vvv`, you must set `UseShortOptionHandling: true` on your `App` or `Command`. See the [Combining Short Options](./combining-short-options/) example for details.
106104
107105
<!-- {
108106
"args": ["&#45;&#45;f", "&#45;&#45;f", "&#45;fff", "&#45;f"],
@@ -146,8 +144,7 @@ func main() {
146144

147145
#### Placeholder Values
148146

149-
Sometimes it's useful to specify a flag's value within the usage string itself.
150-
Such placeholders are indicated with back quotes.
147+
You can indicate a placeholder for a flag's value directly within the `Usage` string. This helps clarify what kind of value the flag expects in the help output. Placeholders are denoted using backticks (` `` `).
151148

152149
For example this:
153150

@@ -193,8 +190,7 @@ be left as-is.
193190

194191
#### Alternate Names
195192

196-
You can set alternate (or short) names for flags by providing a comma-delimited
197-
list for the `Name`. e.g.
193+
Flags can have multiple names, often a longer descriptive name and a shorter alias. You can define aliases using the `Aliases` field, which accepts a slice of strings.
198194

199195
<!-- {
200196
"args": ["&#45;&#45;help"],
@@ -214,8 +210,8 @@ func main() {
214210
app := &cli.App{
215211
Flags: []cli.Flag{
216212
&cli.StringFlag{
217-
Name: "lang",
218-
Aliases: []string{"l"},
213+
Name: "lang", // Primary name
214+
Aliases: []string{"l"}, // Alternate names
219215
Value: "english",
220216
Usage: "language for the greeting",
221217
},
@@ -228,13 +224,11 @@ func main() {
228224
}
229225
```
230226

231-
That flag can then be set with `--lang spanish` or `-l spanish`. Note that
232-
giving two different forms of the same flag in the same command invocation is an
233-
error.
227+
That flag can then be set with `--lang spanish` or `-l spanish`. Providing both forms (e.g., `--lang spanish -l spanish`) in the same command invocation will result in an error.
234228

235229
#### Multiple Values per Single Flag
236230

237-
Using a slice flag allows you to pass multiple values for a single flag; the values will be provided as a slice:
231+
Slice flags allow users to specify a flag multiple times, collecting all provided values into a slice. Available types include:
238232

239233
- `Int64SliceFlag`
240234
- `IntSliceFlag`
@@ -274,7 +268,7 @@ func main() {
274268
}
275269
```
276270

277-
Multiple values need to be passed as separate, repeating flags, e.g. `--greeting Hello --greeting Hola`.
271+
To pass multiple values, the user repeats the flag, e.g., `--greeting Hello --greeting Hola`.
278272

279273
#### Grouping
280274

@@ -403,7 +397,7 @@ Will result in help output like:
403397

404398
#### Values from the Environment
405399

406-
You can also have the default value set from the environment via `EnvVars`. e.g.
400+
You can specify environment variables that can provide default values for flags using the `EnvVars` field (a slice of strings).
407401

408402
<!-- {
409403
"args": ["&#45;&#45;help"],
@@ -438,8 +432,7 @@ func main() {
438432
}
439433
```
440434

441-
If `EnvVars` contains more than one string, the first environment variable that
442-
resolves is used.
435+
If `EnvVars` contains multiple variable names, the library uses the value of the first environment variable found to be set.
443436

444437
<!-- {
445438
"args": ["&#45;&#45;help"],
@@ -474,11 +467,11 @@ func main() {
474467
}
475468
```
476469

477-
When `Value` is not set for the flag, but a matching environment variable is found, the value from the environment will be used in the generated docs as the default value.
470+
If a flag's `Value` field is not explicitly set, but a corresponding environment variable from `EnvVars` is found, the environment variable's value will be used as the default and shown in the help text.
478471

479-
#### Values from files
472+
#### Values from Files
480473

481-
You can also have the default value set from file via `FilePath`. e.g.
474+
Similarly to environment variables, you can specify a file path from which to read a flag's default value using the `FilePath` field.
482475

483476
<!-- {
484477
"args": ["&#45;&#45;help"],
@@ -512,13 +505,11 @@ func main() {
512505
}
513506
```
514507

515-
Note that default values set from the environment (e.g. `EnvVar`) take precedence over
516-
default values set from file (e.g. `FilePath`).
508+
Note that default values sourced from environment variables (`EnvVars`) take precedence over those sourced from files (`FilePath`). See the full precedence order below.
517509

518510
#### Required Flags
519511

520-
You can make a flag required by setting the `Required` field to `true`. If a user
521-
does not provide a required flag, they will be shown an error message.
512+
You can enforce that a flag must be provided by the user by setting its `Required` field to `true`. If a required flag is missing, the application will print an error message and exit.
522513

523514
Take for example this app that requires the `lang` flag:
524515

@@ -568,11 +559,9 @@ If the app is run without the `lang` flag, the user will see the following messa
568559
Required flag "lang" not set
569560
```
570561

571-
#### Default Values for help output
562+
#### Default Values for Help Output
572563

573-
Sometimes it's useful to specify a flag's default help-text value within the
574-
flag declaration. This can be useful if the default value for a flag is a
575-
computed value. The default value can be set via the `DefaultText` struct field.
564+
In cases where a flag's default value (`Value` field) is dynamic or complex to represent (e.g., a randomly generated port), you can specify custom text to display as the default in the help output using the `DefaultText` field. This text is purely for display purposes and doesn't affect the actual default value used by the application.
576565

577566
For example this:
578567

@@ -616,17 +605,16 @@ Will result in help output like:
616605

617606
#### Precedence
618607

619-
The precedence for flag value sources is as follows (highest to lowest):
608+
The order of precedence for determining a flag's value is as follows (from highest to lowest):
620609

621-
0. Command line flag value from user
622-
0. Environment variable (if specified)
623-
0. Configuration file (if specified)
624-
0. Default defined on the flag
610+
1. Value provided on the command line by the user.
611+
2. Value from the first set environment variable listed in `EnvVars`.
612+
3. Value read from the file specified in `FilePath`.
613+
4. Default value specified in the `Value` field of the flag definition.
625614

626615
#### Flag Actions
627616

628-
Handlers can be registered per flag which are triggered after a flag has been processed.
629-
This can be used for a variety of purposes, one of which is flag validation
617+
You can attach an `Action` function directly to a flag definition. This function will be executed *after* the flag's value has been parsed from the command line, environment, or file. This is useful for performing validation or other logic specific to that flag. The action receives the `*cli.Context` and the parsed value of the flag.
630618

631619
<!-- {
632620
"args": ["&#45;&#45;port","70000"],

docs/examples/generated-help-text.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ search:
33
boost: 2
44
---
55

6-
The default help flag (`-h/--help`) is defined as `cli.HelpFlag` and is checked
7-
by the cli internals in order to print generated help text for the app, command,
8-
or subcommand, and break execution.
6+
The library automatically generates help text for your application, commands, and subcommands. By default, this help text is displayed when the user provides the `-h` or `--help` flag (defined by `cli.HelpFlag`). When this flag is detected, the help text is printed, and the application exits.
97

108
#### Customization
119

12-
All of the help text generation may be customized, and at multiple levels. The
13-
templates are exposed as variables `AppHelpTemplate`, `CommandHelpTemplate`, and
14-
`SubcommandHelpTemplate` which may be reassigned or augmented, and full override
15-
is possible by assigning a compatible func to the `cli.HelpPrinter` variable,
16-
e.g.:
10+
You have several ways to customize the generated help output:
11+
12+
1. **Modify Templates:** The default Go text templates used for generation (`cli.AppHelpTemplate`, `cli.CommandHelpTemplate`, `cli.SubcommandHelpTemplate`) are exported variables. You can modify them directly, for example, by appending extra information or completely replacing them with your own template strings.
13+
2. **Replace Help Printer:** For complete control over rendering, you can replace the `cli.HelpPrinter` function. This function receives the output writer, the template string, and the data object (like `*cli.App` or `*cli.Command`) and is responsible for executing the template or generating help in any way you choose.
14+
15+
Here are examples of these customization techniques:
1716

1817
<!-- {
1918
"output": "Ha HA. I pwnd the help!!1"
@@ -70,8 +69,7 @@ VERSION:
7069
}
7170
```
7271

73-
The default flag may be customized to something other than `-h/--help` by
74-
setting `cli.HelpFlag`, e.g.:
72+
You can also change the flag used to trigger the help display (instead of the default `-h/--help`) by assigning a different `cli.Flag` implementation to the `cli.HelpFlag` variable:
7573

7674
<!-- {
7775
"args": ["&#45;&#45halp"],

docs/examples/greet.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ search:
33
boost: 2
44
---
55

6-
Being a programmer can be a lonely job. Thankfully by the power of automation
7-
that is not the case! Let's create a greeter app to fend off our demons of
8-
loneliness!
6+
Let's build a simple "greeter" application to demonstrate the basic structure of a `cli` app. This example will create a command that prints a friendly greeting.
97

108
Start by creating a directory named `greet`, and within it, add a file,
119
`greet.go` with the following code in it:

docs/examples/subcommands-categories.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ search:
33
boost: 2
44
---
55

6-
For additional organization in apps that have many subcommands, you can
7-
associate a category for each command to group them together in the help
8-
output, e.g.:
6+
When your application has numerous subcommands, organizing them into categories can significantly improve the clarity of the help output. You can assign a category to a command by setting its `Category` field. Commands with the same category will be grouped together in the help text.
97

108
<!-- {
119
"output": ".*COMMANDS:\\n.*noop[ ]*\\n.*\\n[ ]*template:\\n[ ]*add[ ]*\\n[ ]*remove.*"

docs/examples/subcommands.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ search:
33
boost: 2
44
---
55

6-
Subcommands can be defined for a more git-like command line app.
6+
You can structure your application with subcommands, similar to tools like `git` or `docker`. Define subcommands by assigning a slice of `*cli.Command` structs to the `Commands` field of your `cli.App` or another `cli.Command`.
77

88
<!-- {
99
"args": ["template", "add"],

docs/examples/suggestions.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,4 @@ search:
33
boost: 2
44
---
55

6-
To enable flag and command suggestions, set `app.Suggest = true`. If the suggest
7-
feature is enabled, then the help output of the corresponding command will
8-
provide an appropriate suggestion for the provided flag or subcommand if
9-
available.
6+
You can enable suggestions for mistyped flags or commands by setting `Suggest: true` on your `cli.App`. When enabled, if a user enters an unrecognized flag or command, the application will suggest the closest match if one is found based on Levenshtein distance.

0 commit comments

Comments
 (0)