PHPackages                             lawondyss/parex - 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. lawondyss/parex

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

lawondyss/parex
===============

Lightweight PHP library for parsing terminal commands using PHP native options processing

1.3.1(1y ago)6121MITPHPPHP ^8.2

Since Mar 27Pushed 1y ago2 watchersCompare

[ Source](https://github.com/Lawondyss/Parex)[ Packagist](https://packagist.org/packages/lawondyss/parex)[ RSS](/packages/lawondyss-parex/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (3)Versions (6)Used By (1)

Parex: Command-Line Parser
==========================

[](#parex-command-line-parser)

Parex is a lightweight and flexible PHP library for parsing command-line arguments. It simplifies the process of defining required, optional, and flag options, and provides a clean and intuitive way to access the parsed values.

Features
--------

[](#features)

- **Easy Definition:** Define required, optional, and flag options with a simple fluent interface.
- **Default Values:** Set default values for optional arguments.
- **Multiple Values:** Handle options that can accept multiple values.
- **Flexible Result Handling:** Supports both dynamic and statically defined result classes.
- **No Undefined Properties:** Prevents access to undefined properties on result objects.

Installation
------------

[](#installation)

You can install Parex via Composer.

```
composer require lawondyss/parex
```

Usage
-----

[](#usage)

Parex allows you to define required, optional, and flag options. You can then parse the command-line arguments and access the parsed values through a result object.

### Basic example (DynamicResult)

[](#basic-example-dynamicresult)

Basic definition:

```
$dynamicResult = (new Parex())
    ->addRequire(name: 'env', short: 'e')
    ->addOptional(name: 'scopes', short: 's', multiple: true)
    ->addOptional(name: 'currency', default: 'CZK')
    ->addOptional(name: 'onlyAccount')
    ->addFlag(name: 'sandbox')
    ->parse();
```

Command: `php examples/simple.php -e "./.env" --sandbox --scopes=last:month --env="../.env" -s="last:week" -stoday`

Dump:

```
Lawondyss\Parex\Result\DynamicResult
    env: './.env'
    scopes: array (3)
       0 => 'last:month'
       1 => 'last:week'
       2 => 'today'
    currency: 'CZK'
    onlyAccount: null
    sandbox: true
    POSITIONAL: array (0)

```

Try [simple.php](./examples/simple.php) example with dynamic result for command arguments.

### Advanced example (TypedResult)

[](#advanced-example-typedresult)

Definition with own class of result.

```
$scriptResult = (new Parex())
    ->addRequire('env', 'e')
    ->addOptional('scopes', 's', multiple: true)
    ->addOptional('currency', default: 'CZK')
    ->addOptional('onlyAccount')
    ->addFlag('sandbox')
    ->parse(ScriptResult::class);
```

Command: `php examples/typed.php -e "./.env" --sandbox --scopes=last:month --env="../.env" -s="last:week" -stoday`

Dump:

```
ScriptResult
    env: SplFileInfo
       path: './.env'
    scopes: array (3)
       0 => Scope::LastMonth
          value: 'last:month'
       1 => Scope::LastWeek
          value: 'last:week'
       2 => Scope::Today
          value: 'today'
    currency: Currency::CZK
       value: 'CZK'
    onlyAccount: null
    sandbox: true

```

Try the example [typed.php](./examples/typed.php), which shows how to define own result class (`ScriptResult`) with types and enums.

### ArgvParser

[](#argvparser)

Default GetOptParser does not support positional arguments suitable for custom commands and ignores unknown options. ArgvParser can be used to support these things.

```
$dynamicResult = (new Parex(new ArgvParser()))
    ->addRequire(name: 'env', short: 'e')
    ->addOptional(name: 'scopes', short: 's', multiple: true)
    ->addOptional(name: 'currency', default: 'CZK')
    ->addOptional(name: 'onlyAccount')
    ->addFlag(name: 'sandbox')
    ->parse();
```

Command: `php examples/argv.php command sub -e "./.env" --sandbox --scopes=last:month --env="../.env" -s="last:week" -stoday`

Dump:

```
Lawondyss\Parex\Result\DynamicResult
   env: '../.env'
   scopes: array (3)
      0 => 'last:month'
      1 => 'last:week'
      2 => 'today'
   currency: null
   onlyAccount: null
   sandbox: true
   POSITIONAL: array (2)
      0 => 'command'
      1 => 'sub'

```

Result classes
--------------

[](#result-classes)

Parex provides two base result classes:

- **`DynamicResult`:** A dynamic result class that allows you to access parsed values as properties. It's suitable for simple use cases where you don't need to define a specific result structure.
- **`DefinedResult`:** An abstract class that you can extend to create your own statically defined result classes. This allows you to define specific properties with types and add custom logic.

Contributing
------------

[](#contributing)

Contributions are welcome! Please feel free to submit a pull request or open an issue.

License
-------

[](#license)

This library is open-sourced software licensed under the MIT license.

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance43

Moderate activity, may be stable

Popularity10

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity54

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 ~2 days

Total

5

Last Release

451d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1f642f8a219fdaad6b73c302bfc3a623148b8eefd58ea4078cdfae1f24c47fc5?d=identicon)[Lawondyss](/maintainers/Lawondyss)

---

Top Contributors

[![Lawondyss](https://avatars.githubusercontent.com/u/272130?v=4)](https://github.com/Lawondyss "Lawondyss (16 commits)")

---

Tags

phpcliterminalparsershellcommand-parser

###  Code Quality

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/lawondyss-parex/health.svg)

```
[![Health](https://phpackages.com/badges/lawondyss-parex/health.svg)](https://phpackages.com/packages/lawondyss-parex)
```

###  Alternatives

[league/climate

PHP's best friend for the terminal. CLImate allows you to easily output colored text, special formats, and more.

1.9k14.7M297](/packages/league-climate)[seregazhuk/php-watcher

Automatically restart PHP application once the source code changes

395138.9k6](/packages/seregazhuk-php-watcher)[alecrabbit/php-console-spinner

Extremely flexible spinner for \[async\] php cli applications

24038.0k2](/packages/alecrabbit-php-console-spinner)[phalcon/cli-options-parser

Command line arguments/options parser.

181.1M10](/packages/phalcon-cli-options-parser)[php-school/learn-you-php

An introduction to PHP's core features: i/o, http, arrays, exceptions and so on.

3202.0k](/packages/php-school-learn-you-php)[yoeunes/regex-parser

A powerful PCRE regex parser with lexer, AST builder, validation, ReDoS analysis, and syntax highlighting. Zero dependencies, blazing fast, and production-ready.

30101.0k8](/packages/yoeunes-regex-parser)

PHPackages © 2026

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