V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
lijinma
V2EX  ›  PHP

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

  •  
  •   lijinma · 2015-01-09 10:13:54 +08:00 · 2851 次点击
    这是一个创建于 3392 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    ruby commander -> commander.js -> php commander
    raincious
        13
    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
        14
    lijinma  
    OP
       2015-01-09 12:43:15 +08:00
    @raincious 谢谢告知。。。不行的话,我只能判断是 windows 就去掉颜色代码了。。
    lizheming
        15
    lizheming  
       2015-01-09 13:43:25 +08:00
    @lijinma 请记得额外的告知下我,因为我也找了好久...233333
    lijinma
        16
    lijinma  
    OP
       2015-01-09 14:02:02 +08:00
    @lizheming 好的。。。
    typcn
        17
    typcn  
       2015-01-09 14:10:32 +08:00
    @raincious 有两种方法:
    1 写一个 PHP 扩展,用 DLL 来做
    2 写一个 Console wrapper
    之前用 MSVC 写过一个 minecraft launcher wrapper 能显示 MC 的颜色代码,后来换 linux 的时候删了。。
    不然传上代码参考一下。。。
    raincious
        18
    raincious  
       2015-01-09 14:22:53 +08:00
    @typcn

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

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

    所以,得找个更好的方法来解决这个问题,不知道是不是有。
    Actrace
        19
    Actrace  
       2015-01-09 22:05:33 +08:00
    做啥都支持windows的话,cmd有意义么...
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1914 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 16:23 · PVG 00:23 · LAX 09:23 · JFK 12:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.