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

Recommend this page to a friend!
  Info   Documentation   View files (5)   Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 235 All time: 7,935 This week: 389
Version License PHP version Categories
console-args 1.0.2MIT/X Consortium ...5.3PHP 5, Console
Description Author

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: 52
All time rank: 96220 in Spain Spain
Week rank: 136 5 in Spain Spain
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;



File Role Description
composer.json Data Auxiliary data
ConsoleArgs.php Class Class source
LICENSE Lic. License text Doc. Documentation
VERSION Data Documentation

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

For more information send a message to info at phpclasses dot org.