【问题标题】:Symfony console single command with no arguments没有参数的 Symfony 控制台单一命令
【发布时间】:2014-02-10 15:11:18
【问题描述】:

我正在尝试使用 Symfony 2 控制台组件构建一个简单的命令行应用程序:它应该只有一个可用的命令,并且不需要任何参数,但它应该接受选项,如下所示:

$ my-command
$ my-command --config="config/path.json"
$ my-command --test
$ my-command --config="config/path.json" --test

我正在关注this guide 制作一个单命令应用程序。 Application类扩展基本和指南一样,自定义命令是这样的:

use \Symfony\Component\Console\Command\Command;
use \Symfony\Component\Console\Input\InputInterface;
use \Symfony\Component\Console\Input\InputOption;
use \Symfony\Component\Console\Output\OutputInterface;
use \Symfony\Component\Console\Input\InputArgument;

class MyCommand extends Command
{
    public function configure()
    {
        $this->setName('my-command')
                ->setDescription('My Command')
                ->addOption('config', null, InputOption::VALUE_OPTIONAL, 'Config path')
                ->addOption('test', null, InputOption::VALUE_NONE, 'Is Test?');
    }

    public function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('Nevermind...');
    }
}

但是,这些是前面每个案例的结果(除了第一个,没有选项,它工作正常):

$ my-command --test
Usage: php [options] [-f] <file> [--] [args...]
       php [options] -r <code> [--] [args...]
       php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]
       php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]
       php [options] -- [args...]
       php [options] -a

  -a               Run as interactive shell
  -c <path>|<file> Look for php.ini file in this directory
  -n               No php.ini file will be used
  -d foo[=bar]     Define INI entry foo with value 'bar'
  -e               Generate extended information for debugger/profiler
  -f <file>        Parse and execute <file>.
  -h               This help
  -i               PHP information
  -l               Syntax check only (lint)
  -m               Show compiled in modules
  -r <code>        Run PHP <code> without using script tags <?..?>
  -B <begin_code>  Run PHP <begin_code> before processing input lines
  -R <code>        Run PHP <code> for every input line
  -F <file>        Parse and execute <file> for every input line
  -E <end_code>    Run PHP <end_code> after processing all input lines
  -H               Hide any passed arguments from external tools.
  -s               Output HTML syntax highlighted source.
  -v               Version number
  -w               Output source with stripped comments and whitespace.
  -z <file>        Load Zend extension <file>.

  args...          Arguments passed to script. Use -- args when first argument
               starts with - or script is read from stdin

  --ini            Show configuration file names

  --rf <name>      Show information about function <name>.
  --rc <name>      Show information about class <name>.
  --re <name>      Show information about extension <name>.
  --ri <name>      Show configuration for extension <name>.

看起来,使它工作的唯一方法是定义至少一个参数,并调用将那个参数传递给它的命令(如$ my-command some-argument --test)。我无法让这个命令工作,只用选项调用它。

知道如何让它工作吗?

谢谢大家。

【问题讨论】:

  • 你是不是直接用php调用命令名?也许尝试调用包含命令的脚本,将命令名称作为参数传递
  • sf2 标准安装中的 php 应用程序/控制台...
  • 感谢您抽出宝贵时间来回答,但这确实是一件愚蠢的事情..我非常关心 Symfony 控制台的工作方式,以至于我没有发现问题出在我打电话的方式上命令:我正在使用带有类似php -f bootstrap.php $@ 的行的bash 脚本,其中bootstrap.php 是主php 脚本的名称,即构建Symfony 应用程序的位置。删除 -f 选项后,一切都像魅力一样。只要我能回答这个问题,我就会“关闭”它。
  • 对于那些因为寻找使用控制台 symfony 组件(虽然是组件,而不是框架)创建单命令的方法而来到这里的人,就在这里 > symfony.com/doc/current/components/console/…

标签: php symfony console-application


【解决方案1】:

正如我在上面的评论中所说,问题不是由 Symfony 控制台引起的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多