phalcon / cli-options-parser
Command line arguments/options parser.
v2.0.0
2023-11-24 16:04 UTC
Requires
- php: >=8.0
Requires (Dev)
- pds/skeleton: ^1.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: ^3.7
README
Command line arguments/options parser.
Requirements
- PHP >= 8.0
Installation
You can install the package using composer
composer require phalcon/cli-options-parser
Usage
use Phalcon\Cop\Parser; $parser = new Parser(); // Parse params from the $argv $params = $parser->parse($argv); // Parse params from the $_SERVER['argv'] $params = $parser->parse(); // After parsing input, Parser provides a way to get booleans: $parser->getBoolean('foo'); // Get param `foo` or return TRUE as a default value $parser->getBoolean('foo', true);
Examples
php test.php -az value1 -abc value2
[
'a' => 'value2',
'z' => 'value1',
'b' => 'value2',
'c' => 'value2',
]
php test.php -a value1 -abc value2
[
'a' => 'value2',
'b' => 'value2',
'c' => 'value2',
]
php test.php --az value1 --abc value2
[
'az' => 'value1',
'abc' => 'value2',
]
php test.php --foo --bar=baz --spam eggs
[
'foo' => true,
'bar' => 'baz',
'spam' => 'eggs',
]
php test.php -abc foo
[
'a' => 'foo',
'b' => 'foo',
'c' => 'foo',
]
php test.php arg1 arg2 arg3
[
0 => 'arg1',
1 => 'arg2',
2 => 'arg3',
]
Development
The repository ships a Docker setup for local development and testing. You only need Docker + Docker Compose; the PHP runtime is provided inside the container.
Quick start
docker compose up -d --build docker compose exec app composer install docker compose exec app composer test
appis the Compose service name; the running container iscli-options-parser-app. It stays up via asleep infinitykeepalive, so you candocker compose exec app <cmd>freely.
Choosing the PHP version
The image is built for one PHP version at a time via the PHP_VERSION build arg (default 8.4;
supported 8.0–8.4). Because it is a build arg, changing it requires a rebuild:
docker compose up -d --build # PHP 8.4 (default) PHP_VERSION=8.0 docker compose up -d --build # PHP 8.0
Composer scripts
| Script | Description |
|---|---|
composer cs |
PHP_CodeSniffer (PSR-12) |
composer cs-fix |
Auto-fix coding-standard issues (phpcbf) |
composer cs-fixer |
PHP CS Fixer (dry-run) |
composer cs-fixer-fix |
Apply PHP CS Fixer |
composer analyze |
PHPStan static analysis (level max) |
composer test |
Unit tests (PHPUnit) |
composer test-coverage |
Tests + Clover coverage (tests/_output/coverage.xml) |
License
CLI Options Parser is open source software licensed under the BSD-3-Clause License.
© Phalcon Team