TP5 custom command detailed command line usage

Many times, you need to use the command line to execute php scripts. There is no big problem in executing individual php scripts. If you want to execute a file that uses the TP5 framework project, you may not be able to execute because the relevant files cannot be loaded.

Fortunately, there is command line function in TP5 framework. We can use TP5's command line to call our customized commands to operate the code in TP5 project.

Define command

First, simply define a command and create a command line test class:

namespace app\base\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class Test extends Command
{
    protected function configure()
    {
        $this->setName('test');//Define the name of the command
    }

    protected function execute(Input $input, Output $output)
    {
        $output->writeln('Hello World');//Output content in the command line interface
    }
}

Now let's talk about the functions of these two methods:

 configure()
                     

 execute()
                      

Configuration command

  after setting the custom command, you need to configure it in application/command.php.

	return [
    	'app\base\command\Test'
	];

A command corresponds to a command class and a configuration. In other words, to define multiple commands, you need to create multiple class files. Each defined command must be configured here to take effect.

Use command

  first enter the root directory of the project, and then open cmd. First, use the php think command to see:

In the Available commands column, see the test command we just defined.

Now execute this command:

	php think test

You can see the return:

	Hello World

In this way, the command we defined is used successfully.

Command parameter

It seems that the above command can only perform some simple operations. This time, we add several parameters to the command to increase its functionality.

    protected function configure()
    {
        $this->setName('test')              		//Define the name of the command
        ->setDescription('This is my command')     //Define the description of the command
        ->addArgument('name')                      //Add a name parameter
        ->addArgument('age');                      //Add an age parameter
    }

    protected function execute(Input $input, Output $output)
    {
        //Get input parameters
        $name = $input->getArgument('name');
        $age = $input->getArgument('age');

        //Output obtained parameters
        $output->writeln("My name is $name ,age is $age");
    }

Enter at the command line:

	php think test  wuhen  20

You can see the return:

	My name is wuhen,age is 20

Command options
  although our command can pass in parameters, we can add options to further enrich the function of our command.

	protected function configure()
    {
        $this->setName('calculate')              	    //Define the name of the command
            ->setDescription('This is my command')      //Define the description of the command
            ->addArgument('number1')                    //Parameter 1
            ->addArgument('number2')                    //Parameter 2
            ->addOption('add')                          //Define options for adding
            ->addOption('sub');                         //Define options for subtraction
    }

    protected function execute(Input $input, Output $output)
    {
        //Get 2 parameters of input
        $number1 = $input->getArgument('number1');
        $number2 = $input->getArgument('number2');
		
		//Addition operation
        if($input->hasOption('add')){
            $result = $number1 + $number2;
            $output->writeln("$number1 + $number2 = $result");
        }
		
		//Subtraction operation
        if($input->hasOption('sub')){
            $result = $number1 - $number2;
            $output->writeln("$number1 - $number2 = $result");
        }
    }

At the command line, enter:

	php think calculate 20 30 --add

  you can see the return:

  	20 + 30 = 50

  at the command line, enter:

	 php think calculate 20 30 --sub
  •  

  you can see the return:

	20 - 30 = -10

 

Keywords: PHP

Added by Christian B. on Fri, 25 Oct 2019 09:22:04 +0300