Skip to main content
Version: Current

Command Line Interface (CLI)

In addition to building web pages, PHP code can also be used to run programs through the command line. For this purpose, PHP supports the CLI, which stands for Command Line Interface.

In Jalno, the packages\base\CLI class is provided for working with the command line.

The following methods are defined in the CLI class. For ease of use, these methods are defined as static.

MethodDescription
set()This method prepares the class for use
This method is called automatically by the framework
getParameter(string $name): ?stringReturns the value of the received parameter
getParameters(array $params): arrayConverts the received parameters into a key-value array
readLine(string $message): stringReads an input value from the command line

Running a Program from the Command Line

To run a program, you need to navigate to the project's root directory through the terminal and run the index.php file. Pass the address of the process you want to run in the process argument along with the command.

php index.php --process=packages/packagename/processes/Main@index
info

For more information, refer to the process page.

In PHP, the arguments passed are received as strings. In Jalno, for better processing, the arguments are converted into a key-value array. When the above command is executed, the set method runs first and stores the input arguments as an array in the class's $request['parameters'] variable. The operation of converting the arguments into an array is performed by the getParameters method. The process ID is also stored in the class's $process['pid'] variable.

Getting the Value of an Input Argument

By calling the getParameter method, you can access an input argument. The input to this method is the name of the argument.

Input arguments must be entered in the form --key=value. The framework converts the arguments into an array with key as the key and value as the value. If an input argument is in the form --arg, it is converted into an array with the key arg and the value 1.

php index.php --process=packages/packagename/processes/Main@index --name=ali --password=123456
processes/Main.php
<?php
namespace packages\packagename\processes;

use packages\base\{Process, CLI};

class Main extends Process {

public function index($data) {

echo CLI::getParameter("name"); //output: ali
echo CLI::getParameter("password"); //output: 123456
/**
* Or
* echo $data["name"];
* echo $data["password"];
*/
}
}

Reading an Input Value from the Command Line

By calling the readLine method, you can receive data from the command line. You can also pass a string as input to the readLine method to display text before receiving input from the user. The method's input argument is optional.

The output of the readLine method is the value received from the input.

processes/Main.php
<?php
namespace packages\packagename\processes;

use packages\base\{Process, CLI};

class Main extends Process {

public function index($data) {
if (!isset($data["email"])) {
$data["email"] = CLI::readLine("please enter your email:\n");
}
echo "your email is : " . $data["email"] . "\n";
}
}

The execution flow of the above code is as follows:

$ php index.php --process=packages/jalno/processes/Main@index
please enter your email:
your email is : [email protected]