参考 Node.js 的 Commander, 写了一个 PHP 版本的 Commander( Command Line Framework),不喜欢 symfony/console

2015-01-09 10:13:54 +08:00
 lijinma

当初使用 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);

2857 次点击
所在节点    PHP
19 条回复
coolicer
2015-01-09 10:20:58 +08:00
感觉js和php可以相互抄
lizheming
2015-01-09 10:23:03 +08:00
好像不支持 CMD.....=_=!
lijinma
2015-01-09 10:27:08 +08:00
@lizheming 嘛意思。。
lijinma
2015-01-09 10:27:25 +08:00
@coolicer 妥妥的可以互抄。
lizheming
2015-01-09 10:34:27 +08:00
@lijinma 唔..我的意思是好像不支持 Windows ?
xuwenmang
2015-01-09 10:34:31 +08:00
不错。。感觉不爽自己造个,学习下。
lijinma
2015-01-09 10:40:34 +08:00
@lizheming 为什么不支持。。只要你装了 windows php cli 就可以用啊。。
coolicer
2015-01-09 10:55:50 +08:00
前不久才抄了一个PHP的类
boom11235
2015-01-09 11:33:54 +08:00
hahah,其实node的commander是来自ruby的~
lizheming
2015-01-09 11:59:40 +08:00
@lijinma 看来楼主并不了解这些东西嘛,我就做个实例给你看好叻....我所谓的不支持就是图中的这个意思
lijinma
2015-01-09 12:31:18 +08:00
@lizheming 哈哈,抱歉没理解你的意思。。。。非常感谢,我得改下颜色的代码。。
lijinma
2015-01-09 12:31:58 +08:00
@boom11235 哈哈哈,其实我发现了。。。

ruby commander -> commander.js -> php commander
raincious
2015-01-09 12:41:13 +08:00
@lijinma

很抱歉的告诉你……

如果你要做Windows的兼容,*貌似*得去掉颜色代码的相关想法。

至少Windows 7下面的Command Prompt是做不了的(其实各个平台下的界面都可以是非标准的,但是Windows更自立门户)。因为你需要使用Kernel32.dll里的GetStdHandle(STD_OUTPUT_HANDLE)拿到Console的句柄,然后调用SetConsoleTextAttribute来修改输出属性,借此来修改颜色。

PHP能拿到Kernel32.dll里的内容么?我记得不行?如果能的话请告知一下。

如果谁能用其他方式更兼容的实现,也请告知下……
lijinma
2015-01-09 12:43:15 +08:00
@raincious 谢谢告知。。。不行的话,我只能判断是 windows 就去掉颜色代码了。。
lizheming
2015-01-09 13:43:25 +08:00
@lijinma 请记得额外的告知下我,因为我也找了好久...233333
lijinma
2015-01-09 14:02:02 +08:00
@lizheming 好的。。。
typcn
2015-01-09 14:10:32 +08:00
@raincious 有两种方法:
1 写一个 PHP 扩展,用 DLL 来做
2 写一个 Console wrapper
之前用 MSVC 写过一个 minecraft launcher wrapper 能显示 MC 的颜色代码,后来换 linux 的时候删了。。
不然传上代码参考一下。。。
raincious
2015-01-09 14:22:53 +08:00
@typcn

第一个这其实跟我说的是一种方法,到最后还是得调用SetConsoleTextAttribute来做,只是稍微转换了下。

第二个:
1、比第一个更麻烦,装一个POSIX标准的终端比装个PHP全局多了;
2、其实不用自己写,已经有人写好了。地址: https://github.com/adoxa/ansicon (而且猛然发现这个人我还认识……,而且还帮过我……,我马上去发Fans帖求脸熟)

所以,得找个更好的方法来解决这个问题,不知道是不是有。
Actrace
2015-01-09 22:05:33 +08:00
做啥都支持windows的话,cmd有意义么...

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/160531

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX