[league/climate]一个PHP功能健全的命令行功能操作库

2022-04-16 奥古斯宏

我们在使用Linux系统当中,会和命令行打交道,很多的命令行并不只有一个简单的运行参数,而是有各种交互操作。简单地比如输入密码、Y/N、[yes]等效果,那么PHP能不能做到这些呢?可以的。

基本用法

require_once('vendor/autoload.php');

$climate = new League\CLImate\CLImate;

$climate->out('打印到终端.');

单行打印

内置了一个inline方法,输出内容不会自动换行。

$climate->inline('Waiting');

for ($i = 0; $i < 10; $i++) {
    $climate->inline('.');
}

// Waiting..........

但是,上面两个东西太简单了,似乎还不如内置的echo、print方便。

不同的颜色输出

climate支持各种样式的输出,比如输出不同的颜色:

$climate->red('输出的这一行是红色');
$climate->blue('蓝色!');
$climate->lightGreen('淡淡的绿色');

以及输出各类背景色:

$climate->backgroundRed('输出的这一行背景是红色');
$climate->backgroundBlue()->out('现在是蓝色背景');
$climate->backgroundLightGreen()->out('现在是淡淡的绿色背景');

内置的颜色有:

版权声明:本文由phpreturn.com(PHP武器库官网)原创和首发,所有权利归phpreturn(PHP武器库)所有,本站允许任何形式的转载/引用文章,但必须同时注明出处。

  • Black
  • Red
  • Green
  • Yellow
  • Blue
  • Magenta
  • Cyan
  • Light Gray
  • Dark Gray
  • Light Red
  • Light Green
  • Light Yellow
  • Light Blue
  • Light Magenta
  • Light Cyan
  • White

设置命令参数

使用climate可以通过简单地数组实现一个完整的命令行参数命令:

$climate->arguments->add([
    'user' => [
        'prefix'       => 'u',
        'longPrefix'   => 'user',
        'description'  => 'Username',
        'defaultValue' => 'me_myself_i',
    ],
    'password' => [
        'prefix'      => 'p',
        'longPrefix'  => 'password',
        'description' => 'Password',
        'required'    => true,
    ],
    'iterations' => [
        'prefix'      => 'i',
        'longPrefix'  => 'iterations',
        'description' => 'Number of iterations',
        'castTo'      => 'int',
    ],
    'verbose' => [
        'prefix'      => 'v',
        'longPrefix'  => 'verbose',
        'description' => 'Verbose output',
        'noValue'     => true,
    ],
    'help' => [
        'longPrefix'  => 'help',
        'description' => 'Prints a usage statement',
        'noValue'     => true,
    ],
    'path' => [
        'description' => 'The path to push',
    ],
]);

添加命令的描述:

$climate->description('My CLI Script');

最终命令输出如下:

My CLI Script

Usage: functional/args.php [--help] [-i iterations, --iterations iterations] [-p password, --password password] [-u user, --user user (default: me_myself_i)] [-v, --verbose] [path]

Required Arguments:
    -p password, --password password
        Password

Optional Arguments:
    --help
        Prints a usage statement
    -i iterations, --iterations iterations
        Number of iterations
    -u user, --user user (default: me_myself_i)
        Username
    -v, --verbose
        Verbose output

高级交互操作

基本的输入

我们可以生成一个基本的输入:

$input = $climate->input('请输入您的姓名:');

$response = $input->prompt();

获取多行输出

$input = $climate->input('>>>');
$input->multiLine();

$response = $input->prompt();  // 通过ctrl+D终止输入

给出两个选项

$input = $climate->input('你觉得PHP怎么样?');
$input->accept(['Fine', 'Ok'], true);

$response = $input->prompt();
// 你觉得PHP怎么样?[Fine/Ok]

给出一个确认选择项

$input = $climate->confirm('确定要继续吗?');

// 确定要继续吗? [y/n]
if ($input->confirmed()) {
    // 确定继续
} else {
    // 不继续! 
}

让用户输入密码

$input    = $climate->password('亲输入:');
$password = $input->prompt();

一个动态多选项选择框

这是一个可以在命令行中进行操作的多选框,只能在Linux中正常运行:

$options  = ['Ice Cream', 'Mixtape', 'Teddy Bear', 'Pizza', 'Puppies'];
$input    = $climate->checkboxes('Please send me all of the following:', $options);
$response = $input->prompt();

Example of Checkboxes

版权声明:本文由phpreturn.com(PHP武器库官网)原创和首发,所有权利归phpreturn(PHP武器库)所有,本站允许任何形式的转载/引用文章,但必须同时注明出处。

一个动态单选框

$options  = ['Ice Cream', 'Mixtape', 'Teddy Bear', 'Pizza', 'Puppies'];
$input    = $climate->radio('Please send me one of the following:', $options);
$response = $input->prompt();

Example of Radio Buttons

强大的输出

输出一个表格:

$data = [
    [
  		'name'       => 'Walter White',
  		'role'       => 'Father',
  		'profession' => 'Teacher',
    ],
    [
  		'name'       => 'Skyler White',
  		'role'       => 'Mother',
  		'profession' => 'Accountant',
    ],
    [
  		'name'       => 'Walter White Jr.',
  		'role'       => 'Son',
  		'profession' => 'Student',
    ],
];

$climate->table($data);

最终输出如下:

------------------------------------------
| name             | role   | profession |
==========================================
| Walter White     | Father | Teacher    |
------------------------------------------
| Skyler White     | Mother | Accountant |
------------------------------------------
| Walter White Jr. | Son    | Student    |
------------------------------------------

让列表按列输出

$data = [
    '12 Monkeys',
    '12 Years a Slave',
    'A River Runs Through It',
    'Across the Tracks',
    'Babel',
    'Being John Malkovich',
    'Burn After Reading',
    'By the Sea',
    'Confessions of a Dangerous Mind',
    'Contact',
    'Cool World',
    'Cutting Class',
    'Fight Club',
    'Fury',
    'Happy Feet Two',
    'Happy Together',
    'Hunk',
    'Inglourious Basterds',
    'Interview with the Vampire',
    'Johnny Suede',
    'Kalifornia',
    'Killing Them Softly',
    'Legends of the Fall',
    'Less Than Zero',
    'Meet Joe Black',
    'Megamind',
    'Moneyball',
];

$climate->columns($data);

输出效果如下:

12 Monkeys                          Contact                  Interview with the Vampire
12 Years a Slave                    Cool World               Johnny Suede
A River Runs Through It             Cutting Class            Kalifornia
Across the Tracks                   Fight Club               Killing Them Softly
Babel                               Fury                     Legends of the Fall
Being John Malkovich                Happy Feet Two           Less Than Zero
Burn After Reading                  Happy Together           Meet Joe Black
By the Sea                          Hunk                     Megamind
Confessions of a Dangerous Mind     Inglourious Basterds     Moneyball

指定二维数组输出

$data = [
    ['Gary', 'Mary', 'Larry', 'Terry'],
    [1.2, 4.3, 0.1, 3.0],
    [6.6, 4.4, 5.5, 3.3],
    [9.1, 8.2, 7.3, 6.4],
];

$climate->columns($data);

输出效果如下:

Gary     Mary     Larry     Terry
1.2      4.3      0.1       3
6.6      4.4      5.5       3.3
9.1      8.2      7.3       6.4

输出键值对自动对齐

$padding = $climate->padding(10);

$padding->label('Eggs')->result('$1.99');
$padding->label('Oatmeal')->result('$4.99');
$padding->label('Bacon')->result('$2.99');
// 效果如下
// Eggs...... $1.99
// Oatmeal... $4.99
// Bacon..... $2.99

输出一个动态进度条

$progress = $climate->progress()->total(100);

for ($i = 0; $i <= 100; $i++) {
  $progress->current($i);

  // Simulate something happening
  usleep(80000);
}

效果如下:

命令行特效

输出各式各样的字符图画:

版权声明:本文由phpreturn.com(PHP武器库官网)原创和首发,所有权利归phpreturn(PHP武器库)所有,本站允许任何形式的转载/引用文章,但必须同时注明出处。

  • passed
  • failed
  • bender
  • fancy-bender
  • 404
  _____         _____ _____ ______ _____
 |  __ \ /\    / ____/ ____|  ____|  __ \
 | |__) /  \  | (___| (___ | |__  | |  | |
 |  ___/ /\ \  \___ \\___ \|  __| | |  | |
 | |  / ____ \ ____) |___) | |____| |__| |
 |_| /_/    \_\_____/_____/|______|_____/
  ______      _____ _      ______ _____
 |  ____/\   |_   _| |    |  ____|  __ \
 | |__ /  \    | | | |    | |__  | |  | |
 |  __/ /\ \   | | | |    |  __| | |  | |
 | | / ____ \ _| |_| |____| |____| |__| |
 |_|/_/    \_\_____|______|______|_____/
  _  _    ___  _  _
 | || |  / _ \| || |
 | || |_| | | | || |_
 |__   _| | | |__   _|
    | | | |_| |  | |
    |_|  \___/   |_|
     ( )
      H
      H
     _H_
  .-'-.-'-.
 /         \
|           |
|   .-------'._
|  / /  '.' '. \
|  \ \ @   @ / /
|   '---------'
|    _______|
|  .'-+-+-+|
|  '.-+-+-+|
|    """""" |
'-.__   __.-'
     """

命令行动画:

最近浏览
IP用户:116.179.*.*
17 分钟前 Baidu Spider
IP用户:116.179.*.*
13 小时前 Baidu Spider
IP用户:220.181.*.*
15 小时前 Baidu Spider
IP用户:116.179.*.*
2 天前 Baidu Spider
IP用户:220.181.*.*
2 天前 Baidu Spider
IP用户:116.179.*.*
2 天前 Baidu Spider
IP用户:85.208.*.*
3 天前 Semrush Bot
IP用户:40.77.*.*
4 天前 BingBot
IP用户:54.36.*.*
5 天前 aHrefs Bot
IP用户:43.156.*.*
6 天前 Microsoft Edge
IP用户:95.108.*.*
6 天前 Yandex Bot
IP用户:196.242.*.*
6 天前 Vivaldi GNU/Linux
累计浏览次数:1417
评论
点击登录
phpreturn,PHP武器库,专注PHP领域的项目和资讯,收录和介绍PHP相关项目。
最近浏览 点击登录
累计浏览次数:93957
一周浏览次数:1134
今日浏览次数:85

本站所有权利归 phpreturn.com 所有

举报/反馈/投稿邮箱:phpreturn@ulthon.com

鲁ICP备19027671号-2