WP_CLI::get_value_from_arg_or_stdin PHP Method

get_value_from_arg_or_stdin() public static method

Read value from a positional argument or from STDIN.
public static get_value_from_arg_or_stdin ( array $args, integer $index ) : string
$args array The list of positional arguments.
$index integer At which position to check for the value.
return string
    public static function get_value_from_arg_or_stdin($args, $index)
    {
        if (isset($args[$index])) {
            $raw_value = $args[$index];
        } else {
            // We don't use file_get_contents() here because it doesn't handle
            // Ctrl-D properly, when typing in the value interactively.
            $raw_value = '';
            while (($line = fgets(STDIN)) !== false) {
                $raw_value .= $line;
            }
        }
        return $raw_value;
    }

Usage Example

 /**
  * Update an option.
  *
  * ## OPTIONS
  *
  * <key>
  * : The name of the option to add.
  *
  * [<value>]
  * : The new value. If ommited, the value is read from STDIN.
  *
  * [--format=<format>]
  * : The serialization format for the value. Default is plaintext.
  *
  * ## EXAMPLES
  *
  *     # Update an option by reading from a file
  *     wp option update my_option < value.txt
  *
  *     # Update one option on multiple sites using xargs
  *     wp site list --field=url | xargs -n1 -I {} sh -c 'wp --url={} option update <key> <value>'
  *
  * @alias set
  */
 public function update($args, $assoc_args)
 {
     $key = $args[0];
     $value = WP_CLI::get_value_from_arg_or_stdin($args, 1);
     $value = WP_CLI::read_value($value, $assoc_args);
     $result = update_option($key, $value);
     // update_option() returns false if the value is the same
     if (!$result && $value != get_option($key)) {
         WP_CLI::error("Could not update option '{$key}'.");
     } else {
         WP_CLI::success("Updated '{$key}' option.");
     }
 }
All Usage Examples Of WP_CLI::get_value_from_arg_or_stdin