| 1 |
[`pacman`](https://wiki.archlinux.org/index.php/pacman) defines subcommands via flags. |
| 2 |
|
| 3 |
Here, `-S` is a short flag subcommand: |
| 4 |
```console |
| 5 |
$ pacman -S package |
| 6 |
Installing package... |
| 7 |
|
| 8 |
``` |
| 9 |
|
| 10 |
Here `--sync` is a long flag subcommand: |
| 11 |
```console |
| 12 |
$ pacman --sync package |
| 13 |
Installing package... |
| 14 |
|
| 15 |
``` |
| 16 |
|
| 17 |
Now the short flag subcommand (`-S`) with a long flag: |
| 18 |
```console |
| 19 |
$ pacman -S --search name |
| 20 |
Searching for name... |
| 21 |
|
| 22 |
``` |
| 23 |
|
| 24 |
And the various forms of short flags that work: |
| 25 |
```console |
| 26 |
$ pacman -S -s name |
| 27 |
Searching for name... |
| 28 |
|
| 29 |
$ pacman -Ss name |
| 30 |
Searching for name... |
| 31 |
|
| 32 |
``` |
| 33 |
*(users can "stack" short subcommands with short flags or with other short flag subcommands)* |
| 34 |
|
| 35 |
In the help, this looks like: |
| 36 |
```console |
| 37 |
$ pacman -h |
| 38 |
package manager utility |
| 39 |
|
| 40 |
Usage: pacman[EXE] <COMMAND> |
| 41 |
|
| 42 |
Commands: |
| 43 |
query, -Q, --query Query the package database. |
| 44 |
sync, -S, --sync Synchronize packages. |
| 45 |
help Print this message or the help of the given subcommand(s) |
| 46 |
|
| 47 |
Options: |
| 48 |
-h, --help Print help |
| 49 |
-V, --version Print version |
| 50 |
|
| 51 |
$ pacman -S -h |
| 52 |
Synchronize packages. |
| 53 |
|
| 54 |
Usage: pacman[EXE] {sync|--sync|-S} [OPTIONS] [package]... |
| 55 |
|
| 56 |
Arguments: |
| 57 |
[package]... packages |
| 58 |
|
| 59 |
Options: |
| 60 |
-s, --search <search>... search remote repositories for matching strings |
| 61 |
-i, --info view package information |
| 62 |
-h, --help Print help |
| 63 |
|
| 64 |
``` |
| 65 |
|
| 66 |
And errors: |
| 67 |
```console |
| 68 |
$ pacman -S -s foo -i bar |
| 69 |
? failed |
| 70 |
error: the argument '--search <search>...' cannot be used with '--info' |
| 71 |
|
| 72 |
Usage: pacman[EXE] {sync|--sync|-S} --search <search>... <package>... |
| 73 |
|
| 74 |
For more information, try '--help'. |
| 75 |
|
| 76 |
``` |
| 77 |
|
| 78 |
<div class="warning"> |
| 79 |
|
| 80 |
**NOTE:** Keep in mind that subcommands, flags, and long flags are *case sensitive*: `-Q` and `-q` are different flags/subcommands. For example, you can have both `-Q` subcommand and `-q` flag, and they will be properly disambiguated. |
| 81 |
Let's make a quick program to illustrate. |
| 82 |
|
| 83 |
</div> |
| 84 |
|
| 85 |
## Reviewing help output |
| 86 |
|
| 87 |
The test renders every command's long help. |
| 88 |
Each Markdown heading contains the complete command path. |
| 89 |
Generated `help` subcommands are skipped. |
| 90 |
`term_width(0)` prevents terminal-dependent wrapping. |
| 91 |
|
| 92 |
[`snapbox`](https://docs.rs/snapbox) compares this output with a snapshot. |
| 93 |
The resulting diff makes CLI changes easy to review. |
| 94 |
Set `SNAPSHOTS=overwrite` to accept intentional changes. |