本文整理汇总了PHP中Cake\Console\ConsoleOptionParser::subcommands方法的典型用法代码示例。如果您正苦于以下问题:PHP ConsoleOptionParser::subcommands方法的具体用法?PHP ConsoleOptionParser::subcommands怎么用?PHP ConsoleOptionParser::subcommands使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\Console\ConsoleOptionParser
的用法示例。
在下文中一共展示了ConsoleOptionParser::subcommands方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _generateUsage
/**
* Generate the usage for a shell based on its arguments and options.
* Usage strings favor short options over the long ones. and optional args will
* be indicated with []
*
* @return string
*/
protected function _generateUsage()
{
$usage = ['cake ' . $this->_parser->command()];
$subcommands = $this->_parser->subcommands();
if (!empty($subcommands)) {
$usage[] = '[subcommand]';
}
$options = [];
foreach ($this->_parser->options() as $option) {
$options[] = $option->usage();
}
if (count($options) > $this->_maxOptions) {
$options = ['[options]'];
}
$usage = array_merge($usage, $options);
$args = [];
foreach ($this->_parser->arguments() as $argument) {
$args[] = $argument->usage();
}
if (count($args) > $this->_maxArgs) {
$args = ['[arguments]'];
}
$usage = array_merge($usage, $args);
return implode(' ', $usage);
}
示例2: runCommand
/**
* Runs the Shell with the provided argv.
*
* Delegates calls to Tasks and resolves methods inside the class. Commands are looked
* up with the following order:
*
* - Method on the shell.
* - Matching task name.
* - `main()` method.
*
* If a shell implements a `main()` method, all missing method calls will be sent to
* `main()` with the original method name in the argv.
*
* For tasks to be invoked they *must* be exposed as subcommands. If you define any subcommands,
* you must define all the subcommands your shell needs, whether they be methods on this class
* or methods on tasks.
*
* @param array $argv Array of arguments to run the shell with. This array should be missing the shell name.
* @param bool $autoMethod Set to true to allow any public method to be called even if it
* was not defined as a subcommand. This is used by ShellDispatcher to make building simple shells easy.
* @param array $extra Extra parameters that you can manually pass to the Shell
* to be dispatched.
* Built-in extra parameter is :
* - `requested` : if used, will prevent the Shell welcome message to be displayed
* @return mixed
* @link http://book.cakephp.org/3.0/en/console-and-shells.html#the-cakephp-console
*/
public function runCommand($argv, $autoMethod = false, $extra = [])
{
$command = isset($argv[0]) ? $argv[0] : null;
$this->OptionParser = $this->getOptionParser();
try {
list($this->params, $this->args) = $this->OptionParser->parse($argv);
} catch (ConsoleException $e) {
$this->err('<error>Error: ' . $e->getMessage() . '</error>');
$this->out($this->OptionParser->help($command));
return false;
}
if (!empty($extra) && is_array($extra)) {
$this->params = array_merge($this->params, $extra);
}
if (!empty($this->params['quiet'])) {
$this->_io->level(ConsoleIo::QUIET);
$this->_io->setLoggers(false);
}
if (!empty($this->params['verbose'])) {
$this->_io->level(ConsoleIo::VERBOSE);
}
if (!empty($this->params['plugin'])) {
Plugin::load($this->params['plugin']);
}
$this->command = $command;
if (!empty($this->params['help'])) {
return $this->_displayHelp($command);
}
$subcommands = $this->OptionParser->subcommands();
$method = Inflector::camelize($command);
$isMethod = $this->hasMethod($method);
if ($isMethod && $autoMethod && count($subcommands) === 0) {
array_shift($this->args);
$this->startup();
return call_user_func_array([$this, $method], $this->args);
}
if ($isMethod && isset($subcommands[$command])) {
$this->startup();
return call_user_func_array([$this, $method], $this->args);
}
if ($this->hasTask($command) && isset($subcommands[$command])) {
$this->startup();
array_shift($argv);
return $this->{$method}->runCommand($argv, false);
}
if ($this->hasMethod('main')) {
$this->startup();
return call_user_func_array([$this, 'main'], $this->args);
}
$this->out($this->OptionParser->help($command));
return false;
}
示例3: testRemoveSubcommand
/**
* test removeSubcommand with an object.
*
* @return void
*/
public function testRemoveSubcommand()
{
$parser = new ConsoleOptionParser('test', false);
$parser->addSubcommand(new ConsoleInputSubcommand('test'));
$result = $parser->subcommands();
$this->assertEquals(1, count($result));
$parser->removeSubcommand('test');
$result = $parser->subcommands();
$this->assertEquals(0, count($result), 'Remove a subcommand does not work');
}
示例4: testAddSubcommandObject
/**
* test addSubcommand with an object.
*
* @return void
*/
public function testAddSubcommandObject()
{
$parser = new ConsoleOptionParser('test', false);
$parser->addSubcommand(new ConsoleInputSubcommand('test'));
$result = $parser->subcommands();
$this->assertEquals(1, count($result));
$this->assertEquals('test', $result['test']->name());
}