get_cli_args

Returns value of command line params.

Description

get_cli_args( (string) $param, (bool) $required = false ); 

Exits when a required param is not set.

Parameters (2)

0. $param (string)
The param.
1. $required — Optional. (bool) => false
The required.

Usage

  1. if ( !function_exists( 'get_cli_args' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/class-wp-importer.php'; 
  3.  
  4. // The param. 
  5. $param = ''; 
  6.  
  7. // The required. 
  8. $required = false; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_cli_args($param, $required); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/class-wp-importer.php  
  1. function get_cli_args( $param, $required = false ) { 
  2. $args = $_SERVER['argv']; 
  3.  
  4. $out = array(); 
  5.  
  6. $last_arg = null; 
  7. $return = null; 
  8.  
  9. $il = sizeof( $args ); 
  10.  
  11. for ( $i = 1, $il; $i < $il; $i++ ) { 
  12. if ( (bool) preg_match( "/^--(.+)/", $args[$i], $match ) ) { 
  13. $parts = explode( "=", $match[1] ); 
  14. $key = preg_replace( "/[^a-z0-9]+/", "", $parts[0] ); 
  15.  
  16. if ( isset( $parts[1] ) ) { 
  17. $out[$key] = $parts[1]; 
  18. } else { 
  19. $out[$key] = true; 
  20.  
  21. $last_arg = $key; 
  22. } elseif ( (bool) preg_match( "/^-([a-zA-Z0-9]+)/", $args[$i], $match ) ) { 
  23. for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) { 
  24. $key = $match[1]{$j}; 
  25. $out[$key] = true; 
  26.  
  27. $last_arg = $key; 
  28. } elseif ( $last_arg !== null ) { 
  29. $out[$last_arg] = $args[$i]; 
  30.  
  31. // Check array for specified param 
  32. if ( isset( $out[$param] ) ) { 
  33. // Set return value 
  34. $return = $out[$param]; 
  35.  
  36. // Check for missing required param 
  37. if ( !isset( $out[$param] ) && $required ) { 
  38. // Display message and exit 
  39. echo "\"$param\" parameter is required but was not specified\n"; 
  40. exit(); 
  41.  
  42. return $return;