記錄一下通過 Symfony 自組 Console 工具
使用方法
1 2 3 4
| php cmd php cmd help php cmd test:name php cmd test2:name
|
file: composer.json
1 2 3 4 5
| { "require": { "symfony/console": "@stable" } }
|
file: cmd
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| <?php require_once dirname(__FILE__).'/vendor/autoload.php';
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption;
class Test extends Command {
protected function configure() { $this->setName('test:name')->setDescription('The test:name command')->setAliases(array('test-name')); }
protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('Called'); }
}
class Test2 extends Command {
protected function configure() { $this->setName('test2:name') ->addArgument('name', InputArgument::REQUIRED) ->addOption('nickname', 'nn', InputOption::VALUE_REQUIRED); }
protected function execute(InputInterface $input, OutputInterface $output) { $name = $input->getArgument('name'); $nickname = $input->getOption('nickname');
$output->writeln(sprintf('Your name: <info>%s</info>', $name)); $output->writeln(sprintf('Your nickname: <info>%s</info>', $nickname)); }
}
$console = new Application('HandHand Console', 'v0.1.0'); $console->add(new Test); $console->add(new Test2); $console->run();
|