function __construct($line) { // TODO: properly parse single and double quotes $this->words = explode(' ', $line); // first word is always `wp` array_shift($this->words); // last word is either empty or an incomplete subcommand $this->cur_word = end($this->words); $r = $this->get_command($this->words); if (!is_array($r)) { return; } list($command, $args, $assoc_args) = $r; $spec = SynopsisParser::parse($command->get_synopsis()); foreach ($spec as $arg) { if ($arg['type'] == 'positional' && $arg['name'] == 'file') { $this->add('<file> '); return; } } if ($command->can_have_subcommands()) { foreach ($command->get_subcommands() as $name => $_) { $this->add("{$name} "); } } else { foreach ($spec as $arg) { if (in_array($arg['type'], array('flag', 'assoc'))) { if (isset($assoc_args[$arg['name']])) { continue; } $opt = "--{$arg['name']}"; if ($arg['type'] == 'flag') { $opt .= ' '; } elseif (!$arg['value']['optional']) { $opt .= '='; } $this->add($opt); } } } }
function __construct($line) { // TODO: properly parse single and double quotes $this->words = explode(' ', $line); // first word is always `wp` array_shift($this->words); // last word is either empty or an incomplete subcommand $this->cur_word = end($this->words); if ("" !== $this->cur_word && !preg_match("/^\\-/", $this->cur_word)) { array_pop($this->words); } $is_alias = false; $is_help = false; if (!empty($this->words[0]) && preg_match("/^@/", $this->words[0])) { array_shift($this->words); // `wp @al` is false, but `wp @all ` is true. if (count($this->words)) { $is_alias = true; } } elseif (!empty($this->words[0]) && 'help' === $this->words[0]) { array_shift($this->words); $is_help = true; } $r = $this->get_command($this->words); if (!is_array($r)) { return; } list($command, $args, $assoc_args) = $r; $spec = SynopsisParser::parse($command->get_synopsis()); foreach ($spec as $arg) { if ($arg['type'] == 'positional' && $arg['name'] == 'file') { $this->add('<file> '); return; } } if ($command->can_have_subcommands()) { // add completion when command is `wp` and alias isn't set. if ("wp" === $command->get_name() && false === $is_alias && false == $is_help) { $aliases = \WP_CLI::get_configurator()->get_aliases(); foreach ($aliases as $name => $_) { $this->add("{$name} "); } } foreach ($command->get_subcommands() as $name => $_) { $this->add("{$name} "); } } else { foreach ($spec as $arg) { if (in_array($arg['type'], array('flag', 'assoc'))) { if (isset($assoc_args[$arg['name']])) { continue; } $opt = "--{$arg['name']}"; if ($arg['type'] == 'flag') { $opt .= ' '; } elseif (!$arg['value']['optional']) { $opt .= '='; } $this->add($opt); } } } }
/** * Object constructor. Puts synopsis parsing into spec property * * @param string $synopsis Synopsis from command's internal documentation */ public function __construct($synopsis) { $this->spec = SynopsisParser::parse($synopsis); }