-
Notifications
You must be signed in to change notification settings - Fork 359
Document guidelines for use of exit codes within commands #632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
009b706
Initial plan
Copilot 49e0e9a
Document guidelines for use of exit codes within commands
Copilot 8288d81
Remove ## EXIT STATUS PHPDoc section references per review feedback
Copilot 0f874a2
Update references/exit-codes.md
swissspidy 9e46e55
Update references/exit-codes.md
swissspidy d54e689
Rename 'Using non-standard exit codes' section for clarity
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # Exit Codes | ||
|
|
||
| WP-CLI follows widely-adopted Unix conventions for exit codes, where `0` signals success and any non-zero value signals failure. Scripts that call WP-CLI commands can inspect the exit code to determine whether to continue, retry, or abort. | ||
|
|
||
| ## Standard exit codes | ||
|
|
||
| | Exit code | Meaning | | ||
| |:----------|:--------| | ||
| | `0` | Command completed successfully. The requested operation was performed as expected. | | ||
| | `1` | Command failed. WP-CLI could not perform the operation as expected (e.g. invalid arguments, WordPress not found, a fatal error was encountered). | | ||
|
|
||
| `WP_CLI::error()` ([doc](https://make.wordpress.org/cli/handbook/references/internal-api/wp-cli-error/)) is the conventional way to report a failure and exit with code `1`. | ||
|
|
||
| `WP_CLI::halt()` ([doc](https://make.wordpress.org/cli/handbook/references/internal-api/wp-cli-halt/)) can be used to exit with a specific non-zero code when the command needs to communicate a particular outcome to the calling script. | ||
swissspidy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Using non-standard exit codes | ||
|
|
||
| Some commands produce a meaningful binary result and communicate it through the exit code rather than output. For example: | ||
swissspidy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| * `wp plugin is-installed <plugin>` exits with `0` if the plugin is installed, `1` if it is not. | ||
| * `wp theme is-installed <theme>` follows the same pattern. | ||
| * `wp core is-installed` exits with `0` when WordPress is installed, `1` when it is not. | ||
|
|
||
| Use a non-standard exit code when: | ||
|
|
||
| 1. The command result is binary (pass/fail, found/not-found) and is intended to be tested directly in a shell conditional. | ||
| 2. The behavior deviates from the default `0`/`1` convention and the deviation is intentional and documented. | ||
|
|
||
| Do **not** return non-zero exit codes for conditions that are informational rather than erroneous (e.g. a list command that finds no items is still a successful operation). | ||
|
|
||
| ## Using exit codes in scripts | ||
|
|
||
| Because WP-CLI uses standard exit codes, you can use WP-CLI commands directly in shell conditionals: | ||
|
|
||
| ```bash | ||
| # Check if a plugin is installed before trying to activate it | ||
| if wp plugin is-installed my-plugin; then | ||
| wp plugin activate my-plugin | ||
| else | ||
| echo "Plugin not installed" | ||
| exit 1 | ||
| fi | ||
| ``` | ||
|
|
||
| You can also inspect `$?` after any WP-CLI command to check whether it succeeded: | ||
|
|
||
| ```bash | ||
| wp core update | ||
| if [ "$?" -ne 0 ]; then | ||
| echo "Update failed" | ||
| fi | ||
| ``` | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.