1. Go to this page and download the library: Download eftec/clione library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
eftec / clione example snippets
$cli=new CliOne(); // instance of the library
$cli=new CliOne();
$cli->createParam('read',[],'first')->add(); // a positional argument (the first one) value-less
$cli->createParam('o',[],'flag')->add(); // a simple flag "-o"
$cli->evalParam('read');
$cli->evalParam('o');
// So we could obtain our values as:
var_dump($cli->getParameter('read')->value);
var_dump($cli->getParameter('o')->value);
// or as
var_dump($cli->getValue('read'));
var_dump($cli->getValue('o'));
$cli->createParam('pwd',[],'flag') // we create the parameter
->setInput(true,'password') // and we ask (if the parameter is not entered as flag)
//for user input of the type password (however the password is not hidden visually)
->add();
$cli->evalParam('pwd'); // and don't forget to evaluate the parameter
$cli->createParam('type',[],'flag')
->setDescription('it is the type of output','what is the option?',['it is the help1','example: -option xml'])
->setInput(true,'option',['json','csv','xml','html'])
->add();
// program.php -f hello
// or
// program.php -f=hello
// or
// program.php -f "hello world"
$cli->createParam('f','flag')->add(); // add() is important otherwise the parameter will not be create.
$result=$cli->evalParam('name'); // $result->value will return "hello"
// program.php -h
// or
// program.php --help
$cli->createParam('h','flag',['help'])->add(); // it adds an alias longflag called "help"
$cli=new CliOne();
$cli->createParam('p1',[],'none')
->setInput()
->add(); // we create the param
$cli->evalParam('p1'); // and we evaluated the parameter
$cli=new CliOne();
$cli->createParam('p1',[],'none')
->setInput(true,'option2',['key1'=>'value1','key2'=>'value2','key3'=>'value3','key4'=>'value4'])
->add(); // we create the param
$cli->evalParam('p1');
$cli->showLine("value :".$cli->getValue('p1'));
$cli->showLine("valuekey :".$cli->getValueKey('p1'));
$cli=new CliOne();
$cli->createParam('p1',[],'none')
->setInput(true,'option2',['key1'=>'value1','key2'=>'value2','key3'=>'value3','key4'=>'value4'])
->add();
$cli->evalParam('p1'); // value returns an associative array with the values selected, example: ["value1","value3"]
$cli=new CliOne();
$cli->createParam('p1',[],'none')
->setInput()
->setDescription('it is for help','what is the value of p1?',['help line1','help line 2'],'the argument is called p1')
->add();
$cli->evalParam('p1');
class ClassService {
public function menuHeader(CliOne $cli) { /* todo: add header code */ }
public function menuFooter(CliOne $cli) { /* todo: add footer code */ }
public function menuOption1(CliOne $cli) { /* todo: add menu option 1 code */ }
public function menuOption2(CliOne $cli) { /* todo: add menu option 2 code */ }
}
$obj=new ClassService();
$cli = new CliOne();
$cli->addMenu('menu1', 'header','footer');
$cli->addMenuItem('menu1','option1', 'option #1'
,function($cli) {$cli->showLine('calling action1');$this->assertTrue(true, true);});
$cli->addMenuItem('menu1','option2', 'option #2');
$cli->addMenuItem('menu1','option3', 'option #3','navigate:menu1.1');
$cli->addMenuItems('menu1',['option4'=>'option #4','option5'=> 'option #5']); // adding multiples options
$cli->addMenu('menu1.1', 'header2','footer2');
$cli->addMenuItem('menu1.1','option1', 'option #1.1');
$cli->addMenuItem('menu1.1','option2', 'option #2.1');
$cli->addMenuItem('menu1.1','option3', 'option #3.1');
$cli->addMenuItem('menu1.1','option4', 'option #4.1');
$cli->addMenuItem('menu1.1','option5', 'option #5.1');
$cli->evalMenu('menu1',$obj); // runs the menu.
$cli->showLine('exit ok');
$cli->clearMenu();
// example1.php
// don't forget to add autoloader, namespace, etc.
$cli=new CliOne(); // instance of the library
if($cli->isCli()) { // we validate if we are running a CLI or not.
$cli->createParam('param1') // the name of the parameter
->setDescription('Some description','question?') // description and question
->setRequired(true) // if the field is
$cli=new CliOne();
if($cli->isCli()) {
$cli->createParam('param1')
->setDescription('This field is called param1 and it is Param('param1');
var_dump($param1->value);
}
shell
cliprogram.php # "onlyinput/none" could not be obtained via command line
//"fnheader" call to $this->menuHeader(CliOne $cli);
$this->addMenu('idmenu','fnheader',null,'What do you want to do?','option3');
// you can use a callable argument, the first argument is of type CliOne.
$this->addMenu('idmenu',function($cli) { echo "header";},function($cli) { echo "footer;"});
$this->addMenu('menu1');
$this->addMenuItems('menu1',[
'op1'=>['operation #1','action1'], // with description & action
'op2'=>'operation #2']); // the action is "op2"
$this->addMenu('menu1');
// pending: add items to the menu
$this->evalMenu('menu1',$myService);
// or also
$this->>addMenu('menu1')->addMenuService('menu1',$myService)->evalMenu('menu1');
$t->addVariableCallBack('call1', function(CliOne $cli) {
$cli->setVariable('v2', 'world',false); // the false is important if you don't want recursivity
});
$this->createParam('k1','first'); // php program.php thissubcommand
$this->createParam('k1','flag',['flag2','flag3']); // php program.php -k1 <val> or --flag2 <val> or --flag3
<val>
// shell:
php mycode.php -argument1 hello -argument2 world
// php code:
$t=new CliOne('mycode.php');
$t->createParam('argument1')->add();
$result=$t->evalParam('argument1'); // an object ClieOneParam where value is "hello"
// page.php:
$inst=new CliOne('page.php'); // this security avoid calling the cli when this file is called by others.
if($inst->isCli()) {
echo "Is CLI and the current page is page.php";
}
$this->clearScreen(); // clear the screen
$this->clearScreen()->cursorHome(); // clear the screen and put the cursor at the home position
$this->cursorHome(); // put the cursor at the home position
$this->hideCursor()->showWaitCursor(true);
$this->showWaitCursor(); // inside a loop.
$this->hideWaitCursor()->showCursor(); // at the end of the loop
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.