nanjingboy / commander
命令行界面使得构建命令行应用程序变得简单
v0.1.0
2014-10-28 00:39 UTC
Requires
- php: >=5.4.0
- symfony/console: 2.*
This package is not auto-updated.
Last update: 2024-09-24 03:23:28 UTC
README
Commander
Commander 是命令行界面,它包装了 symfony/Console,这使得构建命令行应用程序变得简单,因为开发者不需要创建任何命令类文件。
入门指南
- 在应用程序的根目录中创建 composer.json 文件
{ "require": { "php": ">=5.4.0", "nanjingboy/commander": "*" } }
- 通过 composer 安装它
使用示例
<?php require __DIR__ . '/vendor/autoload.php'; use Symfony\Component\Console\Command\Command; class RenameCommand extends Command { public function configure() { $this->setName('rename') ->setDescription('rename database') ->addArgument('new-name', Commander::ARGUMENT_REQUIRED, 'new name of the database') ->addArgument('current-name', Commander::ARGUMENT_REQUIRED, 'current name of the database'); } public function execute($input, $output) { $output->writeln( '<info>rename database: </info>' . $input->getArgument('current-name') . '<info> with </info>' . $input->getArgument('new-name') ); } } $commander = new Commander(); $commander->name('db') ->version('0.1.0') ->command( array( 'name' => 'create', 'description' => 'create database if not exists', 'options' => array( array( 'name' => "dbName", 'mode' => Commander::OPTION_VALUE_REQUIRED ) ), 'callback' => function($input, $output) { $output->writeln('<info>create database: </info>' . $input->getOption('dbName')); } ) ) ->command( array( 'name' => 'drop', 'description' => 'drop database if exists', 'arguments' => array( array( 'name' => 'dbName', 'mode' => Commander::ARGUMENT_REQUIRED ) ), 'callback' => function($input, $output) { $output->writeln('<info>drop database: </info>' . $input->getArgument('dbName')); } ) )->command(new RenameCommand()) ->run();
Commander#command 的参数
- 定义为 Symfony\Component\Console\Command\Command 的子类
- 定义为数组
array( 'name' => string, 'description' => string, // can ignore 'help' => string, // can ignore 'arguments' = array( array( 'name' => string, 'description' => string, // can ignore 'mode' => Commander::ARGUMENT_REQUIRED | Commander::ARGUMENT_OPTIONAL, // default Commander::CARGUMENT_OPTIONAL 'default' => mixed // for Commander::ARGUMENT_OPTIONAL mode only ) ), // can ignore 'options' => array( array( 'name' => string, 'shortcut' => string, // can ignore 'description' => string, // can ignore 'mode' => Commander::OPTION_VALUE_NONE | Commander::OPTION_VALUE_REQUIRED | Commander::OPTION_VALUE_OPTIONAL | Commander::OPTION_VALUE_IS_ARRAY, // default Commander::OPTION_VALUE_OPTIONAL 'default' => mixed // must be null for Commander::OPTION_VALUE_REQUIRED or Commander::OPTION_VALUE_NONE ) ), // can ignore 'callback' => function($input, $output) { } )
许可证
MIT