PHP Classes

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

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-10 (8 months ago) RSS 2.0 feedStarStarStar 52%Total: 250 All time: 7,923 This week: 47Up
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
Age: 56
All time rank: 92219 in Spain Spain
Week rank: 164 Up5 in Spain Spain Up
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 (5)  
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

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:250
This week:0
All time:7,923
This week:47Up
User Ratings User Comments (1)
 All time
Utility:75%StarStarStarStar
Consistency:75%StarStarStarStar
Documentation:75%StarStarStarStar
Examples:-
Tests:-
Videos:-
Overall:52%StarStarStar
Rank:2398
 
For command line lovers like me, thats a great class :-D
9 years ago (José Filipe Lopes Santos)
70%StarStarStarStar