PHP Classes

How to Create a PHP Console Command Using the Package Parex Commander: Run console programs using callback functions

Recommend this page to a friend!
     
  Info   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-08 (3 days ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
parexcommander 1.0MIT/X Consortium ...8Console, Parsers, PHP 8
Description 

Author

This package can run console programs using callback functions.

It provides a class that can take parameters passed from the command line console and executes a function to process the parameter values to execute the actions of the command.

The package also provides classes that can validate the command line console parameters to verify if they comply with a syntax and types that the command expects.

The package also provides classes to process the command input and output.

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

Instructions

Details

Parex Commander

Latest Version License

Parex Commander is a lightweight PHP library designed to simplify the creation of interactive CLI applications. It provides a structured and intuitive way to define commands, handle input/output, and manage the overall flow of your application.

Built on top of the Parex library.

Key Features

  • Command-Based Structure: Organize your CLI application into logical commands, each with its own set of arguments and options.
  • Simplified Input/Output: Easily handle user input and format output using the built-in IO system.
  • Extensible: Easily extend the core functionality with your own custom commands and features.
  • Clean and Readable Code: Designed with a focus on code clarity and maintainability.

Installation

You can install Parex Commander via Composer:

bash composer require lawondyss/parex-commander

Core Components

ParexCommander

The ParexCommander class is the heart of your CLI application. It's responsible for:

  • Registering and managing commands.
  • Parsing user input and routing it to the appropriate command.
  • Handling the overall execution flow.

Example:

// Handlers
function day(DynamicResult $result, IO $io): void { $io->writeLn('??'); }
function night(DynamicResult $result, IO $io): void { $io->writeLn('?'); }

// Commands of Day & Night application
$commander = new ParexCommander('Day & Night', 'Something small and simple');
$commander->addCommand('day', day(...));
$commander->addCommand('night', night(...));

$commander->run();

See example for more.

Command

The Command class represents a single command within your application. It allows you to:

  • Define the command's name, description, and version.
  • Specify the arguments and options that the command accepts.
  • Implement the command's logic in a handler function or invokable class.

Example:

$commander->addCommand('migrate', migrate(...), 'Run missing migrations.')
          ->addOptional('id', help: 'Specifies a particular migration.', multiple: true)
          ->addFlag('dry-run', help: 'It simulates running the migration, but does not execute SQL.');

Or you can use the class on its own, see example for more.

IO

The IO class provides a simple and consistent way to interact with the user. It handles:

  • Writing output to the console.
  • Asking questions.
  • Requires confirmation.
  • Offers a selection of.

Better see example.

Note

All examples are executable, just try them out ?


  Files folder image Files (26)  
File Role Description
Files folder imageexamples (3 files)
Files folder imagesrc (6 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 (26)  /  examples  
File Role Description
  Plain text file application.php Class Class source
  Accessible without login Plain text file command.php Example Example script
  Accessible without login Plain text file io.php Example Example script

  Files folder image Files (26)  /  src  
File Role Description
Files folder imageConsole (5 files, 1 directory)
Files folder imageException (5 files)
  Plain text file Assert.php Class Class source
  Plain text file Command.php Class Class source
  Plain text file IO.php Class Class source
  Plain text file ParexCommander.php Class Class source
  Plain text file Synopsis.php Class Class source
  Plain text file Type.php Class Class source

  Files folder image Files (26)  /  src  /  Console  
File Role Description
Files folder imageUtils (3 files)
  Plain text file Confirmation.php Class Class source
  Plain text file Monitor.php Class Class source
  Plain text file Question.php Class Class source
  Plain text file Selection.php Class Class source
  Plain text file Writer.php Class Class source

  Files folder image Files (26)  /  src  /  Console  /  Utils  
File Role Description
  Plain text file Ansi.php Class Class source
  Plain text file Color.php Class Class source
  Plain text file Key.php Class Class source

  Files folder image Files (26)  /  src  /  Exception  
File Role Description
  Plain text file InvalidArgumentException.php Class Class source
  Plain text file InvalidOptionException.php Class Class source
  Plain text file InvalidValueException.php Class Class source
  Plain text file MissingException.php Class Class source
  Plain text file ParexCommanderException.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