Skip to content

Commit a80feed

Browse files
Copilotswissspidy
andauthored
Extend Commands Cookbook with PHPDoc edge-cases: aliases, repeating flags, sensitive args, global arg conflicts (#630)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent b36ce98 commit a80feed

1 file changed

Lines changed: 81 additions & 1 deletion

File tree

guides/commands-cookbook.md

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,11 @@ Options defined in the longdesc are interpreted as the command's **synopsis**:
207207

208208
* `<name>` is a required positional argument. Changing it to `<name>...` would mean the command can accept one or more positional arguments. Changing it to `[<name>]` would mean that the positional argument is optional and finally, changing it to `[<name>...]` would mean that the command can accept multiple optional positional arguments.
209209
* `[--type=<type>]` is an optional associative argument which defaults to 'success' and accepts either 'success' or 'error'. Changing it to `[--error]` would change the argument to behave as an optional boolean flag.
210-
* `[--field[=<value>]]` allows an optional argument to be used with or without a value. An example of this would be using a global parameter like `--skip-plugins[=<plugins>]` which can either skip loading all plugins, or skip a comma-separated list of plugins.
210+
* `[--field[=<value>]]` allows an optional argument to be used with or without a value. An example of this would be using a global parameter like `--skip-plugins[=<plugins>]` which can either skip loading all plugins, or skip a comma-separated list of plugins.
211+
* `[--status=<status>...]` (note the trailing `...`) declares a **repeating** associative argument.
212+
* When the same flag is passed more than once (e.g. `--status=active --status=inactive`), WP-CLI collects the values into an array and `$assoc_args['status']` will be `[ 'active', 'inactive' ]`.
213+
* Without the `...` ellipsis, passing the same flag multiple times uses **last-wins** behaviour—only the final value is kept.
214+
* Boolean flags (e.g. `[--verbose]`) always use last-wins and are never collected into arrays, regardless of ellipsis.
211215

212216
*Note*: To accept arbitrary/unlimited number of optional associative arguments you would use the annotation `[--<field>=<value>]`. So for example:
213217

@@ -217,6 +221,66 @@ Options defined in the longdesc are interpreted as the command's **synopsis**:
217221
```
218222
A command's synopsis is used for validating the arguments, before passing them to the implementation.
219223

224+
#### Argument aliases
225+
226+
You can define one or more **aliases** for a parameter by appending them after a `|` separator inside the synopsis token. This lets users pass a short form (or any alternative name) and have WP-CLI automatically map it to the canonical argument name in `$assoc_args`.
227+
228+
```
229+
* [--with-dependencies|w]
230+
* : Include dependencies in the operation.
231+
```
232+
233+
With this definition, both `--with-dependencies` and `-w` are accepted, and `$assoc_args['with-dependencies']` is populated in either case.
234+
235+
Multiple aliases are separated by additional `|` characters:
236+
237+
```
238+
* [--verbose|v|wordy]
239+
* : Enable verbose output.
240+
```
241+
242+
Aliases work for value-carrying associative arguments too:
243+
244+
```
245+
* [--format=<format>|f]
246+
* : Output format.
247+
```
248+
249+
Running `wp example hello -f=json` is then equivalent to `wp example hello --format=json`. Note that WP-CLI accepts both `-<alias>=value` (single-dash) and `--<name>=value` (double-dash) forms for short-form aliases.
250+
251+
#### YAML parameter options
252+
253+
The `---` block after a parameter description allows you to set extra metadata for that parameter using YAML.
254+
255+
**`default`** sets the value used when the argument is omitted:
256+
257+
```
258+
* [--type=<type>]
259+
* : Whether or not to greet the person with success or error.
260+
* ---
261+
* default: success
262+
* options:
263+
* - success
264+
* - error
265+
* ---
266+
```
267+
268+
**`options`** restricts the accepted values. WP-CLI validates user input against this list and returns an error for any value not in it.
269+
270+
**`sensitive`** marks an argument as containing sensitive data (e.g. passwords, API keys):
271+
272+
* When the `--prompt` global flag is used, WP-CLI logs the full command it is about to run to STDOUT.
273+
* Any argument flagged with `sensitive: true` will have its value replaced with `[REDACTED]` in that log line.
274+
* This prevents secrets from leaking into logs or terminal history.
275+
276+
```
277+
* [--password=<password>]
278+
* : Database password.
279+
* ---
280+
* sensitive: true
281+
* ---
282+
```
283+
220284
The longdesc is also displayed when calling the `help` command, for example, `wp help example hello`. Its syntax is [Markdown Extra](http://michelf.ca/projects/php-markdown/extra/) and here are a few more notes on how it's handled by WP-CLI:
221285

222286
* The longdesc is generally treated as a free-form text. The `OPTIONS` and `EXAMPLES` section names are not enforced, just common and recommended.
@@ -299,6 +363,22 @@ Do keep in mind most WP-CLI hooks fire before WordPress is loaded. If your comma
299363

300364
It has no effect if the command using it is loaded from a plugin or a theme, because by that time WordPress itself will have already been loaded.
301365

366+
**@skipglobalargcheck**
367+
368+
WP-CLI emits a warning at command-registration time if a command defines an argument whose name collides with an existing [global argument](https://make.wordpress.org/cli/handbook/config/) (e.g. `--debug`, `--user`, `--quiet`). This helps command authors avoid confusing behaviour where the global argument takes precedence over the command-specific one.
369+
370+
If you intentionally want to reuse a global argument name (for example, when wrapping another tool that uses the same flag), you can silence the warning with `@skipglobalargcheck`:
371+
372+
```
373+
/**
374+
* @skipglobalargcheck
375+
* @when before_wp_load
376+
*/
377+
function my_command( $args, $assoc_args ) {
378+
...
379+
}
380+
```
381+
302382
#### WP_CLI::add_command()'s third $args parameter
303383

304384
Each of the configuration options supported by PHPDoc can instead be passed as the third argument in command registration:

0 commit comments

Comments
 (0)