get_cli_args( string $param, bool $required = false ): string|true|null
- Source
wp-admin/includes/class-wp-importer.php:307
Description
Exits when a required param is not set.
Compatibility
- WordPress
- core
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$paramstring- The parameter name to retrieve.
$requiredbooloptional- Whether the parameter is required. Default false.Default:
false
Return value
string|true|null- The parameter value, or true if the parameter was supplied without a value, or null if it was not supplied at all.
Never returns when$requiredis true and the parameter is missing, as the function exits instead.
Performance profile
How much work a call to get_cli_args() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 19–28
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 79.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_cli_args() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$i | 19–22 | none |
!$i && !isset($out[$param]) | 26–28 | exit() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 79 | 19–28 | 10 | |
| 8.5 | 79 | 19–28 | 10 | |
| 8.4 | 79 | 19–28 | 10 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 81 | 19–26 | 10 | |
| 8.2 | 81 | 19–26 | 10 | |
| 8.1 | 81 | 19–26 | 10 | |
| 7.4 | 81 | 19–26 | 10 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Source code
function get_cli_args( $param, $required = false ) { $args = $_SERVER['argv']; if ( ! is_array( $args ) ) { $args = array(); } $out = array(); $last_arg = null; $return = null; $il = count( $args ); for ( $i = 1, $il; $i < $il; $i++ ) { if ( (bool) preg_match( '/^--(.+)/', $args[ $i ], $match ) ) { $parts = explode( '=', $match[1] ); $key = preg_replace( '/[^a-z0-9]+/', '', $parts[0] ); $out[ $key ] = $parts[1] ?? true; $last_arg = $key; } elseif ( (bool) preg_match( '/^-([a-zA-Z0-9]+)/', $args[ $i ], $match ) ) { for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) { $key = $match[1][ $j ]; $out[ $key ] = true; } $last_arg = $key; } elseif ( null !== $last_arg ) { $out[ $last_arg ] = $args[ $i ]; } } // Check array for specified param. if ( isset( $out[ $param ] ) ) { // Set return value. $return = $out[ $param ]; } // Check for missing required param. if ( ! isset( $out[ $param ] ) && $required ) { // Display message and exit. echo "\"$param\" parameter is required but was not specified\n"; exit; } return $return;}Changelog
2 changes between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
string|true|null|never to string|true|null.verified against sourcemixed to string|true|null|never.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-admin/includes/class-wp-importer.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.