Input object

The input object is an instance of Swoft\Console\Input\Input that is used to get information such as command parameter options entered by the user. In the command logic, input and output objects can be obtained through function parameters and global functions.

Get the input object

Method parameters

If you need to use input and output objects, you can define input and output objects on the operation command function, and the underlying framework will automatically inject the object.

 /**
 * Test command
 *
 * @Command(coroutine=true)
 */
class TestCommand
{
    /**
     * @param Input  $input
     * @param Output $output
     *
     * @CommandMapping("test2")
     */
    public function test(Input $input, Output $output)
    {
        // ......
    }
} 

Using global functions

 /**
 * Test command
 *
 * @Command(coroutine=true)
 */
class TestCommand
{
    /**
     * @CommandMapping()
     */
    public function demo()
    {
        $input = \input();
        $output = \output();
        // ......
    }
} 

start using

Through the previous definition of the command article, we already know what is the command parameter, command options.

Now, execute the following command in the terminal to demonstrate the parsing of information such as parameter options:

 $ php bin/swoft demo:test status=2 name=john arg0 -s=test --page 23 --id=154 -e dev -v vvv -d -rf --debug --test=false 

Note: The string entered below will be considered a boolean

  • on|yes|true -- true
  • off|no|false -- false

Get basic information

 echo $input->getScriptFile();  // 'bin/swoft' 执行的入口脚本文件
echo $input->getCommand(); // 'http:start' 命令名称 解析到的第一个参数将会被认为是命令名称,并且不会再存入到 参数列表中
echo $input->getPwd(); // 当前工作目录 

Command parameter information

The usual parameters such as arg0 can only get values based on the index key. But provide the parameter name with the equal sign ( = ) connection (eg: status=2 )

Print all parameter information:

 var_dump($input->getArgs()); 

Output:

 array(3) {
  'status' => string(1) "2"
  'name' => string(4) "john"
  [0] => string(4) "arg0"
} 

Get command parameter values

 // argument
$first = $input->getFirstArg(); // 'arg0'
$status = $input->getArg('status', 'default value'); // '2'
$status = $input->getInt('status'); // 2
// 获取一个必须的参数,若用户没有输入值,将会抛出错误信息
$id = $input->getRequiredArg('id'); 

Command option information

Get parsed option information

  • An option with no value will set the default value to bool(true)
  • Not just with a short option - at the beginning, and the name is only one character
  • Multiple (default) short options can be combined to be written together. For example, -rf will be parsed into two short options 'r' => bool(true) 'f' => bool(true)

Print all option information:

 var_dump($input->getOpts());
// var_dump($input->getLOpts()); // 只打印长选项信息
// var_dump($input->getSOpts()); // 只打印短选项信息 

Output:

 array(10) {          
  's' => string(4) "test"   
  'e' => string(3) "dev"    
  'v' => string(3) "vvv"    
  'd' => bool(true)         
  'r' => bool(true)         
  'f' => bool(true)         
  'page' => string(2) "23"     
  'id' =>   string(3) "154"    
  'debug' => bool(true)         
  'test' => bool(false)        
} 

Get option value

A large number of option value acquisition methods are provided in the input object to facilitate quick and easy access to the required information.

 // option
$page = $input->getOpt('page') // '23'
$page = $input->getIntOpt('page') // 23
$debug = $input->getBoolOpt('debug') // True
$test = $input->getBoolOpt('test') // False

$d = $input->getBoolOpt('d') // True
// 获取到一个值就返回,对同一个含义的选项选项非常有用
$showHelp = $input->sameOpt(['h','help']);
// 获取一个必须的选项,若用户没有输入值,将会抛出错误信息
$id = $input->getRequiredOpt('id'); 

Read user input

 echo "Your name:";

$name = $input->read(); 
echo 'input is ' . $name; // 'inhere' 

In terminal:

 $ Your name: inhere
$ input is inhere 

You can also put the message text directly into the parameter $name = $input->read("Your name:");

/docs/2.x/en/console/input.html
progress-bar