PHP Classes

Parex: Parse command line values with a few lines of code

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-04-06 (4 days ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
parex 1.0MIT/X Consortium ...8Console, Parsers, PHP 8
Description 

Author

This package can parse parameters passed as command line arguments

It provides a class that can take as parameters a list of command line arguments options.

Currently, the class supports argument options like:

- The long and short names

- Whether the arguments are required, optional or flags

- Whether the arguments can appear multiple times

- The argument default value

The class parse the arguments passed from the command line and can throw an exception if the arguments are invalid, or return an array with the validated arguments.

Picture of Ladislav Vondracek
Name: Ladislav Vondracek <contact>
Classes: 8 packages by
Country: Czech Republic Czech Republic

Instructions

Example

<?php

use Lawondyss\Parex\Parex;
use
Lawondyss\Parex\ParexException;
use
Lawondyss\Parex\Result\DynamicResult;

require_once
__DIR__ . '/../vendor/autoload.php';

try {
 
/** @var DynamicResult{env: string, scopes: string[], currency: string, onlyAccount: string|null, sandbox: bool} $result */
 
$result = (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();

 
dump($result);

} catch (
ParexException $exc) {
  echo
"\n[ERROR] {$exc->getMessage()}\n";
  exit(
1);
}


Details

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

  • 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

You can install Parex via Composer.

composer require lawondyss/parex

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 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 example with dynamic result for command arguments.

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, which shows how to define own result class (ScriptResult) with types and enums.

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

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

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

License

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


  Files folder image Files (17)  
File Role Description
Files folder imageexamples (3 files)
Files folder imagesrc (3 files, 2 directories)
Accessible without login Plain text file .php-cs-fixer.php Example Example script
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENCE.md Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (17)  /  examples  
File Role Description
  Accessible without login Plain text file argv.php Example Example script
  Accessible without login Plain text file simple.php Example Example script
  Plain text file typed.php Class Class source

  Files folder image Files (17)  /  src  
File Role Description
Files folder imageParser (4 files)
Files folder imageResult (3 files)
  Plain text file Option.php Class Class source
  Plain text file Parex.php Class Class source
  Plain text file ParexException.php Class Class source

  Files folder image Files (17)  /  src  /  Parser  
File Role Description
  Plain text file ArgvParser.php Class Class source
  Plain text file GetOptParser.php Class Class source
  Plain text file ParexParser.php Class Class source
  Plain text file Parser.php Class Class source

  Files folder image Files (17)  /  src  /  Result  
File Role Description
  Plain text file DefinedResult.php Class Class source
  Plain text file DynamicResult.php Class Class source
  Plain text file Result.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0