function test_complete() { $doc = new DocParser(<<<EOB /** * Rock and roll! * * ## OPTIONS * * --volume=<number> * : Sets the volume. * * ## EXAMPLES * * wp rock-on --volume=11 * * @synopsis [--volume=<number>] * @alias rock-on */ EOB ); $this->assertEquals('Rock and roll!', $doc->get_shortdesc()); $this->assertEquals('[--volume=<number>]', $doc->get_synopsis()); $this->assertEquals('rock-on', $doc->get_tag('alias')); $longdesc = <<<EOB ## OPTIONS --volume=<number> : Sets the volume. ## EXAMPLES wp rock-on --volume=11 EOB; $this->assertEquals($longdesc, $doc->get_longdesc()); }
function test_complete() { $doc = new DocParser(<<<EOB /** * Rock and roll! * * ## OPTIONS * * <genre>... * : Start with one or more genres. * * --volume=<number> * : Sets the volume. * * --artist=<artist-name> * : Limit to a specific artist. * * ## EXAMPLES * * wp rock-on --volume=11 * * @synopsis [--volume=<number>] * @alias rock-on */ EOB ); $this->assertEquals('Rock and roll!', $doc->get_shortdesc()); $this->assertEquals('[--volume=<number>]', $doc->get_synopsis()); $this->assertEquals('Start with one or more genres.', $doc->get_arg_desc('genre')); $this->assertEquals('Sets the volume.', $doc->get_param_desc('volume')); $this->assertEquals('rock-on', $doc->get_tag('alias')); $longdesc = <<<EOB ## OPTIONS <genre>... : Start with one or more genres. --volume=<number> : Sets the volume. --artist=<artist-name> : Limit to a specific artist. ## EXAMPLES wp rock-on --volume=11 EOB; $this->assertEquals($longdesc, $doc->get_longdesc()); }
public function test_desc_doesnt_parse_far_args_yaml() { $longdesc = <<<EOB ## OPTIONS <hook> : The name of the action or filter. <format> : List callbacks as a table, JSON, CSV, or YAML. --- default: table options: - table - json - csv - yaml --- EOB; $doc = new DocParser($longdesc); $this->assertEquals(array('default' => 'table', 'options' => array('table', 'json', 'csv', 'yaml')), $doc->get_arg_args('format')); $this->assertNull($doc->get_arg_args('hook')); }
public function test_desc_parses_yaml() { $longdesc = <<<EOB ## OPTIONS <genre>... : Start with one or more genres. --- options: - rock - electronic default: rock --- --volume=<number> : Sets the volume. --- default: 10 --- --artist=<artist-name> : Limit to a specific artist. ## EXAMPLES wp rock-on electronic --volume=11 EOB; $doc = new DocParser($longdesc); $this->assertEquals('Start with one or more genres.', $doc->get_arg_desc('genre')); $this->assertEquals('Sets the volume.', $doc->get_param_desc('volume')); $this->assertEquals(array('options' => array('rock', 'electronic'), 'default' => 'rock'), $doc->get_arg_args('genre')); $this->assertEquals(array('default' => 10), $doc->get_param_args('volume')); $this->assertNull($doc->get_param_args('artist')); }
/** * Validate the supplied arguments to the command. * Throws warnings or errors if arguments are missing * or invalid. * * @param array $args * @param array $assoc_args * @param array $extra_args * @return array list of invalid $assoc_args keys to unset */ private function validate_args($args, $assoc_args, $extra_args) { $synopsis = $this->get_synopsis(); if (!$synopsis) { return array(array(), $args, $assoc_args, $extra_args); } $validator = new \WP_CLI\SynopsisValidator($synopsis); $cmd_path = implode(' ', get_path($this)); foreach ($validator->get_unknown() as $token) { \WP_CLI::warning(sprintf("The `%s` command has an invalid synopsis part: %s", $cmd_path, $token)); } if (!$validator->enough_positionals($args)) { $this->show_usage(); exit(1); } $unknown_positionals = $validator->unknown_positionals($args); if (!empty($unknown_positionals)) { \WP_CLI::error('Too many positional arguments: ' . implode(' ', $unknown_positionals)); } $synopsis_spec = \WP_CLI\SynopsisParser::parse($synopsis); $i = 0; $errors = array('fatal' => array(), 'warning' => array()); $mock_doc = array($this->get_shortdesc(), ''); $mock_doc = array_merge($mock_doc, explode(PHP_EOL, $this->get_longdesc())); $mock_doc = '/**' . PHP_EOL . '* ' . implode(PHP_EOL . '* ', $mock_doc) . PHP_EOL . '*/'; $docparser = new \WP_CLI\DocParser($mock_doc); foreach ($synopsis_spec as $spec) { if ('positional' === $spec['type']) { $spec_args = $docparser->get_arg_args($spec['name']); if (!isset($args[$i])) { if (isset($spec_args['default'])) { $args[$i] = $spec_args['default']; } } if (isset($spec_args['options'])) { if (!empty($spec['repeating'])) { do { if (isset($args[$i]) && !in_array($args[$i], $spec_args['options'])) { \WP_CLI::error('Invalid value specified for positional arg.'); } $i++; } while (isset($args[$i])); } else { if (isset($args[$i]) && !in_array($args[$i], $spec_args['options'])) { \WP_CLI::error('Invalid value specified for positional arg.'); } } } $i++; } else { if ('assoc' === $spec['type']) { $spec_args = $docparser->get_param_args($spec['name']); if (!isset($assoc_args[$spec['name']]) && !isset($extra_args[$spec['name']])) { if (isset($spec_args['default'])) { $assoc_args[$spec['name']] = $spec_args['default']; } } if (isset($assoc_args[$spec['name']]) && isset($spec_args['options'])) { if (!in_array($assoc_args[$spec['name']], $spec_args['options'])) { $errors['fatal'][$spec['name']] = "Invalid value specified for '{$spec['name']}'"; } } } } } list($returned_errors, $to_unset) = $validator->validate_assoc(array_merge(\WP_CLI::get_config(), $extra_args, $assoc_args)); foreach (array('fatal', 'warning') as $error_type) { $errors[$error_type] = array_merge($errors[$error_type], $returned_errors[$error_type]); } if ($this->name != 'help') { foreach ($validator->unknown_assoc($assoc_args) as $key) { $errors['fatal'][] = "unknown --{$key} parameter"; } } if (!empty($errors['fatal'])) { $out = 'Parameter errors:'; foreach ($errors['fatal'] as $key => $error) { $out .= "\n {$error}"; if ($desc = $docparser->get_param_desc($key)) { $out .= " ({$desc})"; } } \WP_CLI::error($out); } array_map('\\WP_CLI::warning', $errors['warning']); return array($to_unset, $args, $assoc_args, $extra_args); }