alphayax/get_opt

用于管理脚本参数的工具类

3.1.0 2018-04-18 13:18 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:09:25 UTC


README

管理脚本参数的库

Latest Stable Version Latest Unstable Version pakagist

Travis Coverage Status Codacy Badge

License Total Downloads

功能

  • 单例模式
  • 自动生成帮助信息
  • 管理短选项和长选项(-a --abc)
  • 管理值和多个值(-v /toto -v /tutu)
  • 管理必选选项

示例

检查是否设置了参数(通过字母或名称指定)

$Args = \alphayax\utils\cli\GetOpt::getInstance();
$Args->setDescription('This script is a tiny example to show library features');
$verboseOption = $Args->addOpt('v', 'verbose', 'Verbose Mode');

$Args->parse();

$isVerboseMode = $verboseOption->isPresent();

获取 --file 选项的值

$Args = \alphayax\utils\cli\GetOpt::getInstance();
$Args->setDescription('This script is a tiny example to show library features');
$fileOption = $Args->addOpt('f', 'file', 'File name', true);

$Args->parse();

// Check if file option is specified (via -f or --file)
if( $fileOption->isPresent()){
    $fileName = $fileOption->getValue();
}

自动生成的帮助信息

帮助输出示例(如果指定了 -h 或 --help 标志)

   Description
   	This script is a tiny example to show library features
   
   Usage
   	/usr/bin/php a.php [OPTIONS]
   
   Options
	-d        	              	Debug mode
	          	--dry-run     	Dry Run mode
	          	--file <value>	Specify the file name
	-h        	--help        	Display help
	-n <value>	              	[REQUIRED] Number of lines
	-v        	--verbose     	Verbose Mode