hasan-22/php-command

使用PHP创建自定义命令

v1.0.0 2023-02-02 19:58 UTC

This package is auto-updated.

Last update: 2024-09-30 01:46:44 UTC


README

使用PHP语言通过此包,您可以轻松创建命令行脚本

安装

composer require hasan-22/php-console

用法

创建命令

use App\Console;

Console::command('command_name',function (){
    
})->purpose('Command description');

// purpose method is optional

// Run it like this
// php [scriptName.php] [command_name] [args]

获取用户输入

Console::command('get_username',function (){
    $input = Console::input('Enter your name: ');
    echo $input;
})->purpose('This command gets the username');

// Run:
// php script.php get_username

隐藏输入

Console::command('get_password',function (){
    $input = Console::secretInput('Enter your password:');
    echo $input;
});

// Run:
// php script.php get_password

获取命令行参数

Console::command('get_password',function (){
    $input = Console::option('--pass');
    echo $input;
});
// Run:
// php script.php get_password --pass 123456

// Output: 123456

##################### OR ########################
Console::command('get_password',function (){
    $input = Console::option('pass');
    echo $input;
});
// Run:
// php script.php get_password pass 123456

// Output: 123456

##################### OR ########################
Console::command('get_password',function (){
    $input = Console::option('pass');
    echo $input;
});
// Run:
// php script.php get_password pass=123456

// Output: 123456

为文本添加颜色

Console::command('get_password',function (){
    $input = Console::option('--pass');
    echo Console::success($input);
    echo Console::error($input);
    echo Console::warning($input);
    echo Console::info($input);
    echo Console::none($input);
});

命令帮助

// script.php help
// or
//script.php --help
// or
//script.php -h
// or
//script.php h

函数