Download the PHP package deweller/cliopts without Composer
On this page you can find all versions of the php package deweller/cliopts. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download deweller/cliopts
More information about deweller/cliopts
Files in deweller/cliopts
Package cliopts
Short Description A no-nonsense command-line options parser and help generator for PHP CLI apps.
License MIT
Homepage https://github.com/deweller/php-cliopts
Informations about the package cliopts
php-cliopts
A no-nonsense command-line options parser and help generator for PHP CLI apps.
Features
- Simple one-line usage with a human readable configuration format
- Parses $argv data into an associative array similar to getopt()
- Adds error validation for missing or malformed arguments or options
- Nicely formatted help generation
- Supports options (e.g. -i 100) and named arguments (./script.php /tmp/myfile.txt)
Usage
Code
In its simplest form, the parser can be used with one line of php code:
CLI Input
The interpretation of flags are somewhat flexible. The following lines are all handled in the same way by cliopts:
-
./script.php -v -i 101 -o /tmp/myfile.txt /tmp/infile.txt
-
./script.php -vi 101 -o /tmp/myfile.txt /tmp/infile.txt
-
./script.php -v --id 101 -o /tmp/myfile.txt /tmp/infile.txt
./script.php -v --id="101" -o /tmp/myfile.txt /tmp/infile.txt
All of the above will show this output:
The Human Readable Text Specification
Let's look at the specification in the following bit of code:
The Usage Line
The spec begins with a usage line. This line is optional. But if it is provided, here is a breakdown of how the usage line is interpreted:
In this example, 1 argument is expected and the value provided will be assigned to the key "in_file1" in the values object. An optional second argument will be assed to the key "in_file2" if it is provided. And that's all. If a 3rd argument is provided it will not be assigned to a value and validation will fail.
The Option Lines
Here is how the first option line is interpreted: