参考 Node.js 的 Commander, 写了一个 PHP 版本的 Commander( Command Line Framework),不喜欢 symfony/console
当初使用 TJ 的 Commander 非常顺手,现在在写 PHP Command Line Tool
的时候,只找到了 symfony/console
,不喜欢 symfony/console
的 Help,不喜欢他的函数的调用方式,所以造了一个 Commander。
查看:https://github.com/lijinma/commander
还有一些功能细节功能没实现,但是已经可以用了,请大家轻喷。
实例一:
<?php
use Lijinma\Commander;
require __DIR__ . '/vendor/autoload.php';
$cmd = new Commander();
$cmd
->version('0.0.1')
->option('-p, --peppers', 'Add peppers')
->option('-P, --pineapple', 'Add pineapple')
->option('-b, --bbq', 'Add bbq sauce')
->option('-c, --cheese [type]', 'Add the specified type of cheese')
->parse($argv);
echo 'you ordered a pizza with:' . PHP_EOL;
if (isset($cmd->peppers)) {
echo ' - peppers' . PHP_EOL;
}
if (isset($cmd->pineapple)) {
echo ' - pineapple' . PHP_EOL;
}
if (isset($cmd->bbq)) {
echo ' - bbq' . PHP_EOL;
}
if (isset($cmd->cheese)) {
echo " - $cmd->cheese cheese" . PHP_EOL;
}
实例二
<?php
use Lijinma\Commander;
require __DIR__ . '/vendor/autoload.php';
$cmd = new Commander();
$cmd
->version('0.0.1')
->command('rmdir <dir> [otherDirs...]', 'Remove the directory')
->action(
function ($dir, $otherDirs) {
echo 'You will remove the following directory: ' . $dir . PHP_EOL;
if ($otherDirs) {
echo 'And other directories: ' . implode(', ', $otherDirs) . PHP_EOL;
}
}
);
$cmd->command('rm <file>', 'Remove a file')
->action(
function ($file) {
echo 'You will remove the following file: ' . $file . PHP_EOL;
}
);
$cmd->parse($argv);
本帖已被设为精华帖!
咋一看还以为是说这种东西 Commander , Laravel 5 为了避免混淆都把
command
改为console
了.