alphayax/get_opt

Utility class for manage scripts arguments

3.1.0 2018-04-18 13:18 UTC

This package is not auto-updated.

Last update: 2024-04-13 16:45:49 UTC


README

A library to manage scripts arguments

Latest Stable Version Latest Unstable Version pakagist

Travis Coverage Status Codacy Badge

License Total Downloads

Features

  • Singleton pattern
  • Auto generate help
  • Manage short and long option (-a --abc)
  • Manage values and multiple values (-v /toto -v /tutu)
  • Manage required options

Examples

Check if a parameter (specified via a letter or a name) is set

$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();

Get the value of the --file option

$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();
}

Auto-generated Help

Example of help output (if the -h or --help flag is specified) :

   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