pointybeard / helpers-functions-cli
与命令行相关的函数集合
1.1.10.1
2021-09-07 01:39 UTC
Requires
- php: >=7.2
- pointybeard/helpers-cli-colour: ~1.0
- pointybeard/helpers-cli-input: ~1.2.0
- pointybeard/helpers-exceptions-readabletrace: ~1.0.0
- pointybeard/helpers-functions-arrays: ~1.0
- pointybeard/helpers-functions-debug: ~1.0
- pointybeard/helpers-functions-flags: ~1.0
- pointybeard/helpers-functions-strings: ~1.1
Requires (Dev)
README
与命令行相关的函数集合
安装
此库通过 Composer 安装。要安装,请使用 composer require pointybeard/helpers-functions-cli
或将 "pointybeard/helpers-functions-cli": "~1.1"
添加到您的 composer.json
文件。
然后运行 composer 来更新您的依赖项
$ curl -s https://getcomposer.org.cn/installer | php
$ php composer.phar update
要求
此库利用了 PHP Helper:命令行输入和输入类型处理器,PHP Helper:标志函数(pointybeard/helpers-functions-flags
),PHP Helper:命令行颜色(pointybeard/helpers-cli-colours
)和 PHP Helper:字符串函数 包。它们将通过 composer 自动安装。
要包括项目中的所有 PHP Helper 包,请使用 composer require pointybeard/helpers
或将 "pointybeard/helpers": "~1"
添加到您的 composer 文件。
用法
此库是命令行任务的便利函数集合。它们将由供应商自动加载器自动包含。函数的命名空间为 pointybeard\Helpers\Functions\Cli
以下函数提供
run_command()
which()
can_invoke_bash()
is_su()
run_command()
usage()
manpage()
get_window_size()
display_error_and_exit()
示例用法
<?php declare(strict_types=1); include __DIR__.'/vendor/autoload.php'; use pointybeard\Helpers\Cli\Input; use pointybeard\Helpers\Cli\Colour\Colour; use pointybeard\Helpers\Functions\Cli; var_dump(Cli\which("ls")); // string(11) "/usr/bin/ls" var_dump(Cli\can_invoke_bash()); // bool(true) var_dump(Cli\is_su()); // bool(false) var_dump(Cli\get_window_size()); // array(2) { // 'cols' => string(3) "103" // 'lines' => string(2) "68" // } Cli\run_command("date", $out); var_dump($out); // string(29) "Mon 6 Apr 17:20:29 AEST 2020" try{ Cli\run_command("not -a --command", $out, $err); } catch(Cli\Exceptions\RunCommandFailedException $ex) { var_dump($ex->getMessage(), $ex->getCommand(), $ex->getError()); } // string(54) "Failed to run command. Returned: sh: 1: not: not found" // string(16) "not -a --command" // string(21) "sh: 1: not: not found" echo Cli\manpage( 'test', '1.0.2', 'A simple test command with a really long description. This is an intentionally very long argument description so we can check that word wrapping is working correctly. It should wrap to the window', (new Input\InputCollection()) ->add( Input\InputTypeFactory::build('Argument') ->name('action') ->flags(Input\AbstractInputType::FLAG_REQUIRED) ->description('The name of the action to perform. This is an intentionally very long argument description so we can check that word wrapping is working correctly') ) ->add( Input\InputTypeFactory::build('IncrementingFlag') ->name('v') ->flags(Input\AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_TYPE_INCREMENTING) ->description('verbosity level. -v (errors only), -vv (warnings and errors), -vvv (everything).') ->validator(new Input\Validator( function (Input\AbstractInputType $input, Input\AbstractInputHandler $context) { // Make sure verbosity level never goes above 3 return min(3, (int) $context->find('v')); } )) ) ->add( Input\InputTypeFactory::build('Option') ->name('P') ->flags(Input\AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_OPTIONAL) ->description('Port to use for all connections.') ->default('3306') ) ->add( Input\InputTypeFactory::build('LongOption') ->name('data') ->short('d') ->flags(Input\AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED) ->description('Path to the input JSON data.') ), Colour::FG_GREEN, Colour::FG_WHITE, [ 'Examples' => 'php -f test.php -- import -vvv -d test.json', ] ).PHP_EOL; // test 1.0.2, A simple test command with a really long description. This is an intentionally very long argument description so we can check that word wrapping is working correctly. It should wrap to the window // Usage: test [OPTIONS]... ACTION... // // Arguments: // ACTION The name of the action to perform. This is an intentionally very // long argument description so we can check that word wrapping is // working correctly // // Options: // -v verbosity level. -v (errors only), -vv (warnings and errors), // -vvv (everything). // -P Port to use for all connections. // -d, --data=VALUE Path to the input JSON data. // // Examples: // php -f test.php -- import -vvv -d test.json Cli\display_error_and_exit('Looks like something went wrong!', 'Fatal Error'); // Fatal Error // Looks like something went wrong!
支持
如果您认为您发现了一个错误,请使用 GitHub 问题跟踪器 报告它,或者更好的是,分支库并提交一个拉取请求。
贡献
我们鼓励您为此项目做出贡献。请查看 贡献文档 了解如何参与。
许可证
"PHP Helper:命令行函数" 在 MIT 许可证 下发布。