PHP Classes

How to Implement a PHP CLI Arguments Parser using Cli One: Extract argument values passed to PHP shell script

Recommend this page to a friend!
  Info   View files Example   View files View files (8)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 28 This week: 1All time: 11,069 This week: 571Up
Version License PHP version Categories
clione 1.0GNU Lesser Genera...5PHP 5, Console
Description 

Author

This package can extract argument values passed to the PHP shell script.

It can take a list of definitions of the parameters that a command-line shell can be pass to a PHP script that runs from the console.

The package can extract the parameter values from the current command line arguments and return them to objects that applications can inspect.

Picture of Jorge Castro
  Performance   Level  
Name: Jorge Castro <contact>
Classes: 30 packages by
Country: Chile Chile
Age: 48
All time rank: 12763 in Chile Chile
Week rank: 103 Up1 in Chile Chile Up
Innovation award
Innovation award
Nominee: 14x

Winner: 2x

Example

<?php

use Eftec\CliOne\CliOne;

include
__DIR__.'/../src/CliOne.php';
include
__DIR__.'/../src/CliOneParam.php';
$origin='example1.php';

$cli=new CliOne($origin);
if(
$cli->isCli()) {
   
$cli->createParam('param1')
        ->
setDescription('This field is called param1 and it is required')
        ->
setRequired(true)
        ->
setDefault('param1')
        ->
add();
   
$cli->createParam('param1','subparam0')
        ->
setDescription('This field is called subparam0 and it is required')
        ->
setRequired(true)
        ->
setInput(true,'options',['op1','op2','op3'])
        ->
add();
   
$cli->createParam('param1','subparam1')
        ->
setDescription('This field is called subparam1 and it is required')
        ->
setRequired(true)
        ->
setInput(true,'option',['op1','op2','op3'])
        ->
add();
   
$cli->createParam('param1','subparam2')
        ->
setDescription('This field is called subparam2 and it is required','subparam2 (optionshorts)')
        ->
setRequired(true)
        ->
setInput(true,'optionshort',['yes','no'])
        ->
add();
   
$cli->createParam('param1','subparam3')
        ->
setDescription('This field is called subparam3 and it is required','subparam3 number')
        ->
setRequired(true)
        ->
setInput(true,'number','')
        ->
add();
   
$cli->createParam('param1','subparam4')
        ->
setDescription('This field is called subparam4 and it is required','subparam4 (range)')
        ->
setRequired(true)
        ->
setDefault('0')
        ->
setInput(true,'range',[0,100])
        ->
add();
   
$cli->createParam('param2')
        ->
setDescription('This field is called para2 and it is required')
        ->
setDefault('hello')
        ->
setRequired(true)
        ->
add();

   
$param1=$cli->evalParam('*');
    if(
is_object($param1)) {
        if(
$param1->key==='param1' && $param1->subkey===null && $param1->value!==false) {
            echo
"running param1\n";
           
$param1_1=$cli->evalParam('param1','subparam0');
           
$param1_2=$cli->evalParam('param1','subparam1');
           
$param1_3=$cli->evalParam('param1','subparam2');
           
$param1_4=$cli->evalParam('param1','subparam3');
           
$param1_5=$cli->evalParam('param1','subparam4');
        }
    }
    foreach(
$cli->parameters as $v) {
        echo
"$v->key,$v->subkey = ".json_encode($v->value)."\n";
    }
   
//var_dump($param1);

/*
    $cli->addSubParameter('param1','subparam2',null,'1','This field is called subparam2 and it is required',true,true,'option',['op1','op2','op3']);
    $cli->addSubParameter('param1','subparam2',null,['yes','no'],'This field is called subparam2 and it is required',true,true,'optionshort',['yes','no']);
    $cli->addSubParameter('param1','subparam3',null,'hello','This field is called subparam3 and it is required (string)',true,true,'string');
    $cli->addSubParameter('param1','subparam4',null,'3','This field is called subparam4 and it is required (number)',true,true,'number');
    $cli->addSubParameter('param1','subparam5','subparam5 (range 0 to 100)','3','This field is called subparam4 and it is required',true,true,'range',[0,100]);
    $cli->addParameter('param2',null,'','This field is called param2 and it is not required',false,false);*/
  /* echo <<<EOF
   __ _
  /__\__ __ __ _ _ __ ___ _ __ | | ___
 /_\ \ \/ / / _` || '_ ` _ \ | '_ \ | | / _ \
//__ > < | (_| || | | | | || |_) || || __/
\__/ /_/\_\ \__,_||_| |_| |_|| .__/ |_| \___|
                              |_| version 1.2
Parameters:

EOF;*/
    //$cli->start();
    //$cli->showparams();
    //$cli->end();
} /** @noinspection PhpStatementHasEmptyBodyInspection */ else {
   
// 1) not cli
    // 2) composer is running
    // 3) it is not running the right file.
    // 4) it is running as web.
}


Details

CliOne

Packagist Total Downloads [Maintenance]() [composer]() [php]() [php]() [CocoaPods]()

Changelog

  • 0.3 (2022-01-26) new corrections to the reading of values
  • 0.2 some updates
  • 0.1 first version

  Files folder image Files  
File Role Description
Files folder imageexample (1 file)
Files folder imagesrc (2 files)
Files folder imageTest (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  example  
File Role Description
  Accessible without login Plain text file example1.php Example Example script

  Files folder image Files  /  src  
File Role Description
  Plain text file CliOne.php Class Class source
  Plain text file CliOneParam.php Class Class source

  Files folder image Files  /  Test  
File Role Description
  Plain text file CliOneTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:28
This week:1
All time:11,069
This week:571Up