PHP Classes

Redbox PHP CLI Arguments: Extract command line parameter values

Recommend this page to a friend!
  Info   View files Example   View files View files (27)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 95 All time: 9,842 This week: 571Up
Version License PHP version Categories
redbox-cli 1.2MIT/X Consortium ...5.4.0PHP 5, Console
Description 

Author

This class can extract command line parameter values.

It takes as parameters a list of types of arguments that the command line script expects including the the short and long name of the parameter, the description, and whether it is required.

If the parameters do not satisfy the requirements, the class throws an except that may be caught and used to show the expected usage of the command script to the user.

Picture of Johnny Mast
  Performance   Level  
Name: Johnny Mast <contact>
Classes: 10 packages by
Country: The Netherlands The Netherlands
Age: 41
All time rank: 121834 in The Netherlands The Netherlands
Week rank: 109 Up2 in The Netherlands The Netherlands Up
Innovation award
Innovation award
Nominee: 5x

Example

<?php
/**
 * Basic.php
 *
 * Run this script like
 *
 * $ php ./basic.php -p=abc --user=abcd
 *
 * PHP version 7.3 and up.
 *
 * @category Core
 * @package Redbox_Cli
 * @author Johnny Mast <mastjohnny@gmail.com>
 * @license https://opensource.org/licenses/MIT MIT
 * @link https://github.com/johnnymast/redbox-cli
 * @since 1.0
 */

require 'autoload.php';

use
Redbox\Cli\Cli as CLI;

/**
 * Run this script like
 *
 * $ php ./basic.php -p=abc --user=abcd
 */
try {
   
$cli = new CLI;

   
/**
     * Setup the rules of engagement
     */
   
$cli->arguments->add(
        [
       
'user' => [
           
'prefix' => 'u',
           
'longPrefix' => 'user',
           
'description' => 'Username',
           
'defaultValue' => 'me_myself_i',
           
'required' => true,
        ],
       
'password' => [
           
'prefix' => 'p',
           
'longPrefix' => 'password',
           
'description' => 'Password',
           
'required' => true,
        ],
       
'iterations' => [
           
'prefix' => 'i',
           
'longPrefix' => 'iterations',
           
'description' => 'Number of iterations',
        ],
       
'verbose' => [
           
'prefix' => 'v',
           
'longPrefix' => 'verbose',
           
'description' => 'Verbose output',
           
'noValue' => true,
        ],
       
'help' => [
           
'longPrefix' => 'help',
           
'description' => 'Prints a usage statement',
           
'noValue' => true,
        ],
       
'path' => [/* NOT YET SUPPORTED */
           
'description' => 'The path to push',
        ],
        ]
    );

   
/**
     * We need to tell the parser to start.
     */
   
$cli->arguments->parse();

   
/**
     * If we don't get an exception of us missing things we can handle stuff.
     */
   
echo "You entered password: ".$cli->arguments->get('password')."\n";
    echo
"You entered username: ".$cli->arguments->get('user')."\n";

} catch (
Exception $e) {
   
/**
     * Print how to use the script
     */
   
$cli->arguments->usage();
}


Details

redbox-logo-klein

Build Status Scrutinizer Code Quality Code Coverage Twitter URL

redbox-cli

This is a command line parser based on Climate by Joe Tannenbaum. We share a lot of similarities but i desided to take the getopt route and downgrade the number of lines in the code by 1/4 of the original size.

Howto use the package

Look in the examples folder for basic examples of how to use the package.

Installation

Using composer:

$ composer require redbox-cli

Requirements

The following versions of PHP are supported by this version.

  • PHP >= 7.3

Unit Testing

With the new 1.2 release we increased the test ratio from 64% in previous versions to 95% and that is making me proud. Also with the new release i have found a way to unit test the core component of this package (getopt(). While doing this we can make sure your implementation of Redbox-cli will run like a sunshine with any of your projects. If you want a little of what our new tests cover that was not possible before, checkout our examples directory for some great examples on how to use this great package.

Author

This package is created and maintained by Johnny Mast but it was based on Climate by Joe Tannenbaum. For feature requests and suggestions you could consider sending me an e-mail or fill out an issue.

License

Redbox-cli is released under the MIT public license.

LICENSE


  Files folder image Files  
File Role Description
Files folder image.github (1 directory)
Files folder imageexamples (4 files)
Files folder imagesrc (1 file, 2 directories)
Files folder imagetests (3 files, 1 directory)
Accessible without login Plain text file .gitignore Data Auxiliary data
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file CONTRIBUTING.md Data NEW
Accessible without login Plain text file LICENSE.md Data Auxiliary data
Accessible without login Plain text file phpcs.xml.dist Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Data Auxiliary data

  Files folder image Files  /  .github  
File Role Description
Files folder imageworkflows (2 files)

  Files folder image Files  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file Php.yml Data Auxiliary data
  Accessible without login Plain text file Phpcs.yaml Data Auxiliary data

  Files folder image Files  /  examples  
File Role Description
  Accessible without login Plain text file autoload.php Aux. NEW
  Accessible without login Plain text file basic.php Example Example script
  Accessible without login Plain text file defaultvalue.php Example Example script
  Accessible without login Plain text file hasdefaultvalue.php Example Example script

  Files folder image Files  /  src  
File Role Description
Files folder imageArguments (4 files)
Files folder imageObject (1 file)
  Plain text file Cli.php Class Class source

  Files folder image Files  /  src  /  Arguments  
File Role Description
  Plain text file Argument.php Class Class source
  Plain text file Filter.php Class Class source
  Plain text file Manager.php Class Class source
  Plain text file Parser.php Class Class source

  Files folder image Files  /  src  /  Object  
File Role Description
  Plain text file ArgumentObject.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imagemisc (2 files)
  Accessible without login Plain text file ArgumentObjectTest.php Test NEW
  Accessible without login Plain text file ArgumentsTest.php Test Unit test script
  Accessible without login Plain text file ManagerTest.php Test Unit test script

  Files folder image Files  /  tests  /  misc  
File Role Description
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script
  Plain text file overwriteGetOpt.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:95
This week:0
All time:9,842
This week:571Up