PHP Classes

PHP Command Line Parser: Invoke functions to process command-line arguments

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 63 All time: 10,443 This week: 54Up
Version License PHP version Categories
clp 1.0Free For Educatio...7Console, PHP 7
Description 

Author

This package can invoke functions to process command-line arguments.

It can take as a parameter the $argv array passed to PHP as the array of command line arguments and an array that defines options on how to process those arguments.

The package can process the arguments by invoking a callback function that applications pass to process the command line argument values.

It can also call a given callback function to process when all arguments are processed and when no arguments are passed to the package.

Innovation Award
PHP Programming Innovation award nominee
April 2023
Number 11
PHP can start running scripts from the command line console shell using the PHP CLI version.

Those scripts may take arguments passed by the users in front of the PHP script name.

PHP can create an array of arguments to make it easier for the scripts to process the parameters.

This package simplifies the processing of arguments by providing a means to call given functions to help developers process each type of argument separately.

Manuel Lemos
Picture of Mat Jung
  Performance   Level  
Name: Mat Jung <contact>
Classes: 4 packages by
Country: Switzerland Switzerland
Innovation award
Innovation award
Nominee: 2x

Example

<?php
/* License: OSL-3.0
   To be launched in shell environment
   php Example1.php
*/

require 'vendor/autoload.php';
use
Siims\clp\clp;

$options = [
   
"actions" => [
       
"h|help" => "displayHelp",
       
"hello" => "callHello"
   
],
   
"flags" => [
       
"try-run","verbose","debug"
   
],
   
"values" => [
       
"hello" => "world"
   
],
   
"events" => [
       
"onAfterProcess" => "parsingCommandLineFinished",
       
"onNoOptions" => "displayHelp"
   
]
    ];

$hello = new clp($argv,$options);

function
displayHelp() {
    global
$argv;
    echo
"$argv[0] hello=\"your_name\" | --h | -help | verbose | debug\n";
}

function
parsingCommandLineFinished($config) {
    echo
"Finished parsing command line.\n";
   
print_r($config);
}

function
callHello($config,$method) {
    echo
"Hello {$config["values"]["hello"]}\n";
    echo
"Implemented by $method\n";
}


  Files folder image Files (7)  
File Role Description
Files folder imagesrc (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file Example1.php Example Example script
Plain text file Example2.php Class Class source
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files (7)  /  src  
File Role Description
  Plain text file clp.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 Download Rankings  
 100%
Total:63
This week:0
All time:10,443
This week:54Up