dev-master
9999999-dev https://github.com/pete-otaqui/ClipClopA PHP option parser based on getopt()
MIT
The Requires
- php >=5.2.0
by Pete Otaqui
clipclop option parser
A PHP option parser based on getopt()
A PHP option parser based on getopt()., (*1)
ClipClop allows you to easily create command line tools with options. ClipClop automatically generates nicely formatted usage instructions, and also gives a convenient API for accessing parameters and values., (*2)
ClipClop handles required and optional parameters, and values for them. So a given option such as "--verbose" can be required or optional in itself, and it can have no parameter value or an optional one, or a required one., (*3)
ClipClop manages multiple values, although enforces single values by default, can validate against regular expressions and can parse out certain types for you: integers, numbers, json and urls., (*4)
Create a script called "environment_test", with the following code, (*5)
#!/usr/bin/env php addOption(array( 'short' => 'e', // shortname, i.e. "-e" 'long' => 'environment', // longname, i.e. "--environment" 'value' => TRUE, // A value must be given such as "--environment=TEST" 'help' => 'Set the environment', // help text for the 'usage' text 'required' => TRUE, // Environment must be provided )); // as soon as we ask for an option, ClipClop will parse CLI arguments with getopt() $environment = $clipclop->getOption('e'); // returns the value set for 'e' OR 'environment' print "You ran this script with environment: $environment"; ?>
Make the script executable:, (*6)
$ chmod +x environment_test
Now you can run this from the command line as follows:, (*7)
$ ./environment_test --environment=TEST
You should see output like: You ran this script with environment: TEST, (*8)
When adding options all keys are optional, but many combinations will (fairly obviously) not be valid, i.e. an option without either a shortname or a longname. The keys can be set in the argument array:, (*9)
Sets/Gets the command name for the usage output, (*10)
Sets/Gets the command help for the usage output, (*11)
Use this method to supply a manual set of options (in the same format as returned by getopt). Useful for testing or other methods of overriding getopt()., (*12)
Returns a string of the usage text, (*13)
Prints out the usage text preceded by an optional extra message, and optionally exits the script with a supplied exit code (i.e. 0 for "ok", > 0 for "errors"), (*14)
Returns the parsed option value (or array of parsed values if the parameter was given multiple times). Will return the correct value regardless of whether the short name or long name version was supplied., (*15)
Returns an array of all options with names as keys. Where options have both long and short names, the values are duplicated across both keys, so you can still access from either the short or long name., (*16)
Sets / Gets the current width of the usage output, (*17)
Sets / Gets the current minimum width of the "help" part of the usage output, (*18)
Sets / Gets the class which prints out usage text (useful for testing), (*19)
Sets / Gets the class which quits the script (useful for testing), (*20)
A PHP option parser based on getopt()
MIT
clipclop option parser