当前位置:网站首页>[league/climate] A robust command-line function manipulation library
[league/climate] A robust command-line function manipulation library
2022-08-02 03:53:00 【phpreturn】
我们在使用Linux系统当中,Will work with the command line,Many command lines don't just have a simple run parameter,Instead, there are various interactions.As simple as entering a password、Y/N、[yes]等效果,那么PHPCan this be done?可以的.
基本用法
require_once('vendor/autoload.php');
$climate = new League\CLImate\CLImate;
$climate->out('打印到终端.');单行打印
内置了一个inline方法,The output will not wrap automatically.
$climate->inline('Waiting');
for ($i = 0; $i < 10; $i++) {
$climate->inline('.');
}
// Waiting..........但是,The above two things are too simple,Seems not as good as built-inecho、print方便.
Different color output
climateVarious styles of output are supported,Such as outputting different colors:
$climate->red('This line of output is red');
$climate->blue('蓝色!');
$climate->lightGreen('淡淡的绿色');And output various background colors:
$climate->backgroundRed('This line of output has a red background');
$climate->backgroundBlue()->out('Now it's a blue background');
$climate->backgroundLightGreen()->out('It's now a light green background');
There are built-in colors:
- Black
- Red
- Green
- Yellow
- Blue
- Magenta
- Cyan
- Light Gray
- Dark Gray
- Light Red
- Light Green
- Light Yellow
- Light Blue
- Light Magenta
- Light Cyan
- White
设置命令参数
使用climateA complete command line parameter command can be implemented simply by arrays:
$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');The final command output is as follows:
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 outputAdvanced Interaction
基本的输入
We can generate a basic input:
$input = $climate->input('请输入您的姓名:');
$response = $input->prompt();Get multi-line output
$input = $climate->input('>>>');
$input->multiLine();
$response = $input->prompt(); // 通过ctrl+D终止输入Two options are given
$input = $climate->input('你觉得PHP怎么样?');
$input->accept(['Fine', 'Ok'], true);
$response = $input->prompt();
// 你觉得PHP怎么样?[Fine/Ok]Gives a confirmation option
$input = $climate->confirm('确定要继续吗?');
// 确定要继续吗? [y/n]
if ($input->confirmed()) {
// 确定继续
} else {
// 不继续!
}让用户输入密码
$input = $climate->password('亲输入:');
$password = $input->prompt();A dynamic multi-option selection box
This is a checkbox that can be manipulated from the command line,只能在Linux中正常运行:
$options = ['Ice Cream', 'Mixtape', 'Teddy Bear', 'Pizza', 'Puppies'];
$input = $climate->checkboxes('Please send me all of the following:', $options);
$response = $input->prompt();
A dynamic radio button
$options = ['Ice Cream', 'Mixtape', 'Teddy Bear', 'Pizza', 'Puppies'];
$input = $climate->radio('Please send me one of the following:', $options);
$response = $input->prompt();
强大的输出
输出一个表格:
$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 |
------------------------------------------Make the list output in columns
$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 MoneyballSpecifies a two-dimensional array output
$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.4Output key-value pairs are automatically aligned
$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.99Output a dynamic progress bar
$progress = $climate->progress()->total(100);
for ($i = 0; $i <= 100; $i++) {
$progress->current($i);
// Simulate something happening
usleep(80000);
}效果如下:

command line effects
Various character drawings are output:
- passed
- failed
- bender
- fancy-bender
- 404
_____ _____ _____ ______ _____
| __ \ /\ / ____/ ____| ____| __ \
| |__) / \ | (___| (___ | |__ | | | |
| ___/ /\ \ \___ \\___ \| __| | | | |
| | / ____ \ ____) |___) | |____| |__| |
|_| /_/ \_\_____/_____/|______|_____/
______ _____ _ ______ _____
| ____/\ |_ _| | | ____| __ \
| |__ / \ | | | | | |__ | | | |
| __/ /\ \ | | | | | __| | | | |
| | / ____ \ _| |_| |____| |____| |__| |
|_|/_/ \_\_____|______|______|_____/
_ _ ___ _ _
| || | / _ \| || |
| || |_| | | | || |_
|__ _| | | |__ _|
| | | |_| | | |
|_| \___/ |_|
( )
H
H
_H_
.-'-.-'-.
/ \
| |
| .-------'._
| / / '.' '. \
| \ \ @ @ / /
| '---------'
| _______|
| .'-+-+-+|
| '.-+-+-+|
| """""" |
'-.__ __.-'
"""Command line animation:

边栏推荐
- 项目中遇到的问题
- canvas--pie chart
- Function hoisting and variable hoisting
- IP门禁:手把手教你用PHP实现一个IP防火墙
- vue3 访问数据库中的数据
- 批量替换文件字体,简体-&gt;繁体
- 16.JS事件, 字符串和运算符
- Living to detect the Adaptive Normalized Representation Learning for GeneralizableFace Anti - Spoofing reading notes
- 你的本地创建的项目库还在手动创建远端代码仓库再推送吗,该用它了
- (1)Thinkphp6入门、安装视图、模板渲染、变量赋值
猜你喜欢
随机推荐
Function hoisting and variable hoisting
三元判断再三元判断
多线程(实现多线程、线程同步、生产者消费者)
解决 Zlibrary 卡死/找不到域名/达到限额问题,Zlibrary最新地址
[sebastian/diff]一个比较两段文本的历史变化扩展库
逍遥多开模拟器ADB驱动连接
SQL: DDL, DML, DQL, DCL corresponding introduction and demonstration
AES加密的各种蛋疼方式方式
线程池(线程池介绍与使用)
正则笔记(1)- 正则表达式字符匹配攻略
The Error in the render: "TypeError: always read the properties of null '0' (reading)" Error solution
js eventLoop 事件循环机制
TypeScript 错误 error TS2469、error TS2731 解决办法
环形链表---------约瑟夫问题
6.24今日学习
js __proto__、prototype、constructor的关系
解决MySQL创建子视图并查看的时候,字符集报错问题
IP access control: teach you how to implement an IP firewall with PHP
1.8今日学习
数组的高级操作









