Download the PHP package yeriomin/getopt without Composer
On this page you can find all versions of the php package yeriomin/getopt. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download yeriomin/getopt
More information about yeriomin/getopt
Files in yeriomin/getopt
Package getopt
Short Description A small PHP getopt helper/library. Provides a customizable input parameter interpretation and usage message generation.
License MIT
Informations about the package getopt
getopt
A small PHP getopt helper/library. Provides a customizable input parameter interpretation and usage message generation.
Usage
$ composer require yeriomin/getopt
Features
- Obviously it parses command line options. Uses $_SERVER['argv'].
- Short and long options, arguments.
- Short options clustering.
- Required options.
--
argument separator.- Usage message generation.
Detailed usage
Arguments and options
Lets see how getopt
deals with the following input:
$ php somescript.php arg1 --option1 value1 -o value2 -abc -- --option3 value3
Getting option value
getArguments()
Returns an array of arguments. For the above example returns:
getOptionsLong()
Returns an array of long options. For the above example returns:
getOptionsShort()
Returns an array of short options. For the above example returns:
Defining options
If you just want to get console arguments, you just need the three methods covered in the previous section. However giving getopt
definitions of options you expect lets you define required options and get a usage message.
addOptionDefinition()
Doing this defines -c|--config option. Providing -c populates --config and vice versa.
Required options
Forth argument in the OptionDefinition
constructor makes option required. You can do the same with $optionDefinition->setRequired()
. If any required option is not provided, getopt will throw an exception when getArguments
, getOptionsShort
or getOptionsLong
are called.
getUsageMessage()
This method lets you get a usage message based on the options you have defined. Is looks somewhat like a man page.
Trying to run this script with no arguments would give us the following message: