siims / clp
命令行解析器
1.0.0
2023-04-21 13:31 UTC
Requires
- php: >=7.4
This package is not auto-updated.
Last update: 2024-09-21 18:57:11 UTC
README
php命令行解析器,用于composer中的$argv - siims/clp
使用时间 2023-04-21
Option1
# Scroll down for Example1.php
composer create-project siims/clp .
php Example1.php --help hello=world debug
php Example2.php --help hello="hello world" --try-run
Option2
composer require siims/clp
With create-project you get a copy of the original composer.json.
With require you get a new composer.json or the existing composer.json gets updated.
已知问题
该工具未设计用于处理单个撇号。
php Example1.php --help hello='her is my world'
--> may not work as expected
php Example1.php --help hello="'her is my world'"
--> may work as expected
The package also does not support something like
php Example1.php -file Example2.php
--> will not work as expected
替代PHP包
- https://php.ac.cn/manual/en/function.getopt.php
- https://pear.php.net/manual/en/package.php.php-compatinfo.command-line.php (有点过时)
- https://github.com/phalcon/cli-options-parser
- https://github.com/vanilla/garden-cli
Example1.php
<?php
/* License: OSL-3.0
To be launched in shell environment
php Example1.php
*/
require 'vendor/autoload.php';
use Siims\clp\clp;
$options = [
"actions" => [
"h|help" => "displayHelp",
"hello" => "callHello"
],
"flags" => [
"try-run","verbose","debug"
],
"values" => [
"hello" => "world"
],
"events" => [
"onAfterProcess" => "parsingCommandLineFinished",
"onNoOptions" => "displayHelp"
]
];
$hello = new clp($argv,$options);
function displayHelp() {
global $argv;
echo "$argv[0] hello=\"your_name\" | --h | -help | verbose | debug\n";
}
function parsingCommandLineFinished($config) {
echo "Finished parsing command line.\n";
print_r($config);
}
function callHello($config,$method) {
echo "Hello {$config["values"]["hello"]}\n";
echo "Implemented by $method\n";
}