PHPackages                             nerou/cli-parser - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. nerou/cli-parser

ActiveLibrary[CLI &amp; Console](/categories/cli)

nerou/cli-parser
================

Parser for CLI arguments

v0.3.0(4w ago)03.2k↑33.3%1MITPHPPHP &gt;=8.0CI passing

Since Dec 18Pushed 5d ago1 watchersCompare

[ Source](https://github.com/nerou42/CLIParser)[ Packagist](https://packagist.org/packages/nerou/cli-parser)[ RSS](/packages/nerou-cli-parser/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (7)Dependencies (4)Versions (8)Used By (1)

CLI Parser
==========

[](#cli-parser)

[![License](https://camo.githubusercontent.com/90360977a387fa6c1d24d94c6debbfd82f9d12d7b868cab762ded247ad65659d/68747470733a2f2f706f7365722e707567782e6f72672f6e65726f752f636c692d7061727365722f6c6963656e7365)](https://packagist.org/packages/nerou/cli-parser)[![PHP Version Require](https://camo.githubusercontent.com/13b2186d8c4cd91a714d3d47a88ecb35b1f72d6a5f441d1548ca3ae5ad3bb409/68747470733a2f2f706f7365722e707567782e6f72672f6e65726f752f636c692d7061727365722f726571756972652f706870)](https://packagist.org/packages/nerou/cli-parser)[![Version](https://camo.githubusercontent.com/bfb2006f6e15d73b0b3cad3803f3fc1b116d9480473e4428a4141d3464cdb06c/68747470733a2f2f706f7365722e707567782e6f72672f6e65726f752f636c692d7061727365722f76657273696f6e)](https://packagist.org/packages/nerou/cli-parser)[![Psalm Type Coverage](https://camo.githubusercontent.com/3f2137387d054f93a06c7dcd45a37fbe3052dd7649a622712468b2e932916c95/68747470733a2f2f73686570686572642e6465762f6769746875622f6e65726f7534322f434c495061727365722f636f7665726167652e737667)](https://packagist.org/packages/nerou/cli-parser)

This project is build around a slightly modified version of [this fairly old comment on php.net](https://www.php.net/manual/en/features.commandline.php#83843).

This is for you, if...
----------------------

[](#this-is-for-you-if)

...you are creating CLI applications with PHP, that take some command line arguments, options, commands and/or flags.

Install
-------

[](#install)

Note: This library requires PHP 8.0+!

Use composer to install this library:

`composer require nerou/cli-parser`

There are no dependencies.

Usage
-----

[](#usage)

### Wording

[](#wording)

**Command** is just some value, e.g. `myscript.php hello`

**Option** starts with `--` and can have a value, e.g. `myscript.php --foo` or `myscript.php --foo=bar` or `myscript.php --foo bar`

**Flag** starts with `-` and is a short form of an option, e.g. `myscript.php -f` or `myscript.php -f bar`

**Argument** is everything following a standalone `--`

### Validation

[](#validation)

You can use [PHP's Filter Extension](https://www.php.net/manual/en/book.filter.php) to validate provided option values (see examples below). There is a strict mode which can be enabled to abort parsing if there is an option provided for which the validation fails. Otherwise, this specific option is ignored. Either way, an error will be present when calling `getErrors()`.

When you want to add arrays to your CLI by allowing a single option to be used multiple times, note the following validation behavior:

- The `FILTER_REQUEST_SCALAR` flag does not allow multiple values for a single option, meaning only the last value is parsed.
- The `FILTER_FORCE_ARRAY` flag works as [documented on php.net](https://www.php.net/manual/en/filter.constants.php#constant.filter-force-array), meaning even if an option is provided only once, the value will be wrapped in an array.
- The `FILTER_REQUIRE_ARRAY` flag is basically ignored.

### Examples

[](#examples)

Minimal example with options `--foo` and `--bar` as well as the flag `-f` which is a short form of `--foo`:

```
if(PHP_SAPI !== 'cli' || !isset($_SERVER['argv'])){
  exit(1);          // exit if not run via CLI
}

$cliArgs = new CLIParser($_SERVER['argv'], '[--foo] [--bar]');
$cliArgs->setAllowedOptions(['foo', 'bar']);    // list of supported options
$cliArgs->setAllowedFlags(['f' => 'foo']);      // maps flags to options
$cliArgs->setStrictMode(true);                  // parse() will return `false` if there are options/flags that are not allowed
if(!$cliArgs->parse()){
  $cliArgs->printUsage();     // show them how to use this script
  exit(1);
}
```

Use value validation (see [PHP filters](https://www.php.net/manual/en/filter.filters.php)):

```
$cliArgs->setAllowedOptions([
    'foo' => [
        'filter' => FILTER_VALIDATE_FLOAT,
        'flags' => FILTER_FLAG_ALLOW_THOUSAND,
        'options' => [
            'min_range' => 0,
            'default' => 42                   // default value which will also be added to usage documentation
        ],
        'value_label' => 'my-val',            // for usage documentation
        'description' => 'pass some float'    // for usage documentation
    ],
    'bar' => []     // defaults to `['filter' => FILTER_DEFAULT]`
]);
```

License
-------

[](#license)

This library is licensed under the MIT License. Please see [LICENSE](LICENSE) for more information.

###  Health Score

45

—

FairBetter than 91% of packages

Maintenance97

Actively maintained with recent releases

Popularity21

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~157 days

Recently: every ~64 days

Total

7

Last Release

28d ago

PHP version history (2 changes)0.1PHP &gt;=8.0 &lt;8.4

0.1.1PHP &gt;=8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/28577589?v=4)[cracksalad](/maintainers/cracksalad)[@cracksalad](https://github.com/cracksalad)

---

Top Contributors

[![cracksalad](https://avatars.githubusercontent.com/u/28577589?v=4)](https://github.com/cracksalad "cracksalad (27 commits)")

---

Tags

cli-parserphp

###  Code Quality

Static AnalysisRector

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/nerou-cli-parser/health.svg)

```
[![Health](https://phpackages.com/badges/nerou-cli-parser/health.svg)](https://phpackages.com/packages/nerou-cli-parser)
```

###  Alternatives

[illuminate/console

The Illuminate Console package.

13046.6M7.3k](/packages/illuminate-console)[styleci/cli

The CLI tool for StyleCI

71477.7k9](/packages/styleci-cli)[winbox/args

Windows command-line formatter

20722.5k21](/packages/winbox-args)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
