PHP Classes

PHP Get Console Arguments: Read and parse arguments for command line scripts

Recommend this page to a friend!
  Info   View files Documentation   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-12-03 (3 days ago) RSS 2.0 feedNot enough user ratingsTotal: 246 This week: 1All time: 7,944 This week: 380Up
Version License PHP version Categories
console-args 1.0.10MIT/X Consortium ...5.3PHP 5, Console


This class can read and parse arguments for command line scripts.

It takes an array with the command line arguments passed from the console and parses them according to the definition of expected arguments passed as another array.

The class supports boolean options that are enabled by the presence of argument like --help, and string options that have associated values like --file=filename. other values that do not start with -- are ignored.

The class returns argument values using getter functions.

Picture of Joseluis Laso
  Performance   Level  
Name: Joseluis Laso <contact>
Classes: 16 packages by
Country: Spain Spain
Age: 55
All time rank: 92819 in Spain Spain
Week rank: 21 Up2 in Spain Spain Up
Innovation award
Innovation award
Nominee: 6x

Winner: 2x


Latest Stable Version Total Downloads Latest Unstable Version License


This is an wrapper for the argv global

The difference between arguments and options are: * arguments are strings like `--help` * options are strings like `--file=filename` * other arguments without `--` are not recognized because the magic of this wrapper is that doesn't matter the order of the arguments/options




Add the module by composer.json, adding in require clause:

    "jlaso/console-args": "1.0.0"

Sample of use

$consoleArgs = new ConsoleArgs($argv, array('help'), array('arg1', 'arg2'));

    print <<<EOD
        Please, use this arguments to invoke this command:

             --help       \tto see this help
             --arg1=filename \tthe first argument
             --arg2=filename\toptional, the second argument
$arg1 = $consoleArgs->getArg1;
$arg2 = $consoleArgs->getArg2;



  Files folder image Files  
File Role Description
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file ConsoleArgs.php Class Class source
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file Doc. Documentation
Accessible without login Plain text file VERSION Data Documentation

 Version Control Unique User Downloads Download Rankings  
This week:1
All time:7,944
This week:380Up
User Comments (1)
For command line lovers like me, thats a great class :-D
9 years ago (Josť Filipe Lopes Santos)