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
2024-01-10 (3 days ago) RSS 2.0 feedNot enough user ratingsTotal: 247 This week: 2All time: 7,937 This week: 220Up
Version License PHP version Categories
console-args 1.0.11MIT/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
Innovation award
Innovation award
Nominee: 6x

Winner: 2x

Documentation

Latest Stable Version Total Downloads Latest Unstable Version License

console-args

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

Version

1.0.0

Installation

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'));

if($consoleArgs->hasHelp){
    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
EOD;
   exit();
}
$arg1 = $consoleArgs->getArg1;
$arg2 = $consoleArgs->getArg2;

License

MIT


  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 README.md Doc. Documentation
Accessible without login Plain text file VERSION Data Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:247
This week:2
All time:7,937
This week:220Up
User Comments (1)
For command line lovers like me, thats a great class :-D
9 years ago (José Filipe Lopes Santos)
70%StarStarStarStar