PHP Classes

Redbox PHP CLI Arguments: Extract command line parameter values

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 95 All time: 9,876 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  
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 (27)  
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

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:95
This week:0
All time:9,876
This week:571Up