# clo-args-php **Repository Path**: SuperWindcloud/clo-args-php ## Basic Information - **Project Name**: clo-args-php - **Description**: php用于命令行参数处理的库 - **Primary Language**: PHP - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-12-02 - **Last Updated**: 2024-12-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # GetOpt.PHP GetOpt.PHP 是一个用于命令行参数处理的库。它支持 PHP 7.1 及更高版本。 ## 功能 * 支持 short (例如 '-v') 和long (例如 '--version') 选项 * 选项别名,即。选项可以同时具有 long 和 short 版本 * 累积空头期权(例如 '-vvv') * 带参数的长选项的两种替代表示法: '--option value' 和 '--option=value' * 折叠的短选项(例如 '-abc' 而不是 '-a -b -c'),也带有最后一个选项的参数 (例如,用 '-ab 1' 代替 '-a -b 1') * 带参数的短选项的两种替代表示法:“-o value”和“-ovalue” * 用于字符串处理的带引号的参数(例如 '--path “/some path/with spaces”') * 具有多个参数的选项(例如 '--domain example.org --domain example.com') * 操作数(位置参数)规范、验证和限制 * 具有指定选项和操作数的命令路由 * 帮助文本生成 * 默认参数值 * 参数验证 ## 快速开始 ```php #!/usr/bin/env php addOptions([ Option::create(null, 'version', GetOpt::NO_ARGUMENT) ->setDescription('Show version information and quit'), Option::create('?', 'help', GetOpt::NO_ARGUMENT) ->setDescription('Show this help and quit'), ]); // add simple commands $getOpt->addCommand(Command::create('test-setup', function () { echo 'When you see this message the setup works.' . PHP_EOL; })->setDescription('Check if setup works')); // add commands $getOpt->addCommand(new CopyCommand()); $getOpt->addCommand(new MoveCommand()); $getOpt->addCommand(new DeleteCommand()); // process arguments and catch user errors try { try { $getOpt->process(); } catch (Missing $exception) { // catch missing exceptions if help is requested if (!$getOpt->getOption('help')) { throw $exception; } } } catch (ArgumentException $exception) { file_put_contents('php://stderr', $exception->getMessage() . PHP_EOL); echo PHP_EOL . $getOpt->getHelpText(); exit; } // show version and quit if ($getOpt->getOption('version')) { echo sprintf('%s: %s' . PHP_EOL, NAME, VERSION); exit; } // show help and quit $command = $getOpt->getCommand(); if (!$command || $getOpt->getOption('help')) { echo $getOpt->getHelpText(); exit; } // call the requested command call_user_func($command->getHandler(), $getOpt); ```