You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/examples/arguments.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ search:
3
3
boost: 2
4
4
---
5
5
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:
Copy file name to clipboardExpand all lines: docs/examples/bash-completions.md
+7-16Lines changed: 7 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,10 +3,7 @@ search:
3
3
boost: 2
4
4
---
5
5
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.
10
7
11
8
#### Default auto-completion
12
9
@@ -128,16 +125,13 @@ func main() {
128
125
129
126
#### Enabling
130
127
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.
133
129
134
130
> :warning: The `bash-completion` package or equivalent that provides the
135
131
> `_get_comp_words_by_ref` function for the target platform must be installed and
136
132
> initialized for this completion script to work correctly.
137
133
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:
141
135
142
136
For example, if your cli program is called `myprogram`:
143
137
@@ -150,19 +144,16 @@ a new shell.
150
144
151
145
#### Distribution and Persistent Autocompletion
152
146
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.
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:
Copy file name to clipboardExpand all lines: docs/examples/combining-short-options.md
+5-9Lines changed: 5 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,16 +3,15 @@ search:
3
3
boost: 2
4
4
---
5
5
6
-
Traditional use of options using their shortnames look like this:
6
+
Normally, short options (like `-s`, `-o`, `-m`) are specified individually:
7
7
8
8
```sh-session
9
9
$ cmd -s -o -m "Some message"
10
10
```
11
11
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:
16
15
17
16
<!-- {
18
17
"args": ["short", "-som", "Some message"],
@@ -66,7 +65,4 @@ following syntax:
66
65
$ cmd -som "Some message"
67
66
```
68
67
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.
Copy file name to clipboardExpand all lines: docs/examples/exit-codes.md
+3-4Lines changed: 3 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,10 +3,9 @@ search:
3
3
boost: 2
4
4
---
5
5
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.
Copy file name to clipboardExpand all lines: docs/examples/flags.md
+27-39Lines changed: 27 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ search:
3
3
boost: 2
4
4
---
5
5
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.
7
7
8
8
<!-- {
9
9
"output": "Hello Nefertiti"
@@ -48,9 +48,7 @@ func main() {
48
48
}
49
49
```
50
50
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.
54
52
55
53
<!-- {
56
54
"output": "Hello someone"
@@ -98,11 +96,11 @@ func main() {
98
96
}
99
97
```
100
98
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.
102
100
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.
104
102
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.
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 (` `` `).
151
148
152
149
For example this:
153
150
@@ -193,8 +190,7 @@ be left as-is.
193
190
194
191
#### Alternate Names
195
192
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.
198
194
199
195
<!-- {
200
196
"args": ["--help"],
@@ -214,8 +210,8 @@ func main() {
214
210
app:= &cli.App{
215
211
Flags: []cli.Flag{
216
212
&cli.StringFlag{
217
-
Name: "lang",
218
-
Aliases: []string{"l"},
213
+
Name: "lang",// Primary name
214
+
Aliases: []string{"l"},// Alternate names
219
215
Value: "english",
220
216
Usage: "language for the greeting",
221
217
},
@@ -228,13 +224,11 @@ func main() {
228
224
}
229
225
```
230
226
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.
234
228
235
229
#### Multiple Values per Single Flag
236
230
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:
238
232
239
233
-`Int64SliceFlag`
240
234
-`IntSliceFlag`
@@ -274,7 +268,7 @@ func main() {
274
268
}
275
269
```
276
270
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`.
278
272
279
273
#### Grouping
280
274
@@ -403,7 +397,7 @@ Will result in help output like:
403
397
404
398
#### Values from the Environment
405
399
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).
407
401
408
402
<!-- {
409
403
"args": ["--help"],
@@ -438,8 +432,7 @@ func main() {
438
432
}
439
433
```
440
434
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.
443
436
444
437
<!-- {
445
438
"args": ["--help"],
@@ -474,11 +467,11 @@ func main() {
474
467
}
475
468
```
476
469
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.
478
471
479
-
#### Values from files
472
+
#### Values from Files
480
473
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.
482
475
483
476
<!-- {
484
477
"args": ["--help"],
@@ -512,13 +505,11 @@ func main() {
512
505
}
513
506
```
514
507
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.
517
509
518
510
#### Required Flags
519
511
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.
522
513
523
514
Take for example this app that requires the `lang` flag:
524
515
@@ -568,11 +559,9 @@ If the app is run without the `lang` flag, the user will see the following messa
568
559
Required flag "lang" not set
569
560
```
570
561
571
-
#### Default Values for help output
562
+
#### Default Values for Help Output
572
563
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.
576
565
577
566
For example this:
578
567
@@ -616,17 +605,16 @@ Will result in help output like:
616
605
617
606
#### Precedence
618
607
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):
620
609
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.
625
614
626
615
#### Flag Actions
627
616
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.
Copy file name to clipboardExpand all lines: docs/examples/generated-help-text.md
+8-10Lines changed: 8 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,17 +3,16 @@ search:
3
3
boost: 2
4
4
---
5
5
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.
9
7
10
8
#### Customization
11
9
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:
17
16
18
17
<!-- {
19
18
"output": "Ha HA. I pwnd the help!!1"
@@ -70,8 +69,7 @@ VERSION:
70
69
}
71
70
```
72
71
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:
Copy file name to clipboardExpand all lines: docs/examples/greet.md
+1-3Lines changed: 1 addition & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,9 +3,7 @@ search:
3
3
boost: 2
4
4
---
5
5
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.
9
7
10
8
Start by creating a directory named `greet`, and within it, add a file,
Copy file name to clipboardExpand all lines: docs/examples/subcommands-categories.md
+1-3Lines changed: 1 addition & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,9 +3,7 @@ search:
3
3
boost: 2
4
4
---
5
5
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.
Copy file name to clipboardExpand all lines: docs/examples/subcommands.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ search:
3
3
boost: 2
4
4
---
5
5
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`.
Copy file name to clipboardExpand all lines: docs/examples/suggestions.md
+1-4Lines changed: 1 addition & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,4 @@ search:
3
3
boost: 2
4
4
---
5
5
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