wppaste
WordPress

get_cli_args( string $param, bool $required = false ): string|true|null|never

Source
wp-admin/includes/class-wp-importer.php:302
Returns value of command line params.

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|never
The parameter value or true if found, null otherwise.
The function exits when a required parameter is missing.

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

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
19–28

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 79.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

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.

WhenInstructionsCalls it makes
!$i19–22none
!$i && !isset($out[$param])26–28exit()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev7919–2810
8.57919–2810
8.47919–28102 fewer instructions than PHP 8.3
8.38119–2610
8.28119–2610
8.18119–2610
7.48119–2610

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

7.1.0
Return type changed from string|true|null|never to string|true|null.verified against source
7.0.4
Return type changed from mixed to string|true|null|never.verified against source

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 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.