📜 ⬆️ ⬇️

CodeIgniter Command Line library - a small assistant for working with CLI

As you probably know, php has an interesting function for processing data from the command line: getopt . But there is one small problem - it works incorrectly in CodeIgniter, and it’s not very convenient.

And since I had a bit of free time, the desire to write something open sourced, try github (sitting on hg + bitbucket), and finally do something useful for the crushing CodeIgniter and its community, I decided to write my crutch , designed to slightly brighten up the writing of cli scripts on this framework.
It knows the very minimum - to search for the specified arguments, validate them (if no mandatory ones are indicated) and display hints on the use of your script, and if desired, can be easily finished for use without reference to CodeIgniter:



Using


As a matter of fact, when creating the library, I tried to make its use familiar to people working with CodeIgniter, so its use is somewhat similar to the use of the standard Form_Validation library.
')
This is best seen by example:

1. First, download the library from the githaba and put it in the * / application / libraries / folder.

2. Then create a controller that will be used on the command line:
<?php if ( ! defined("BASEPATH")) exit("No direct script access allowed"); class Cli extends MY_Controller { public function __construct() { parent::__construct(); if(!$this->input->is_cli_request()) { show_404(); } } } 

Pay attention to the line where it is checked whether this controller is started from the command line. We do not want any villain to access our script through the website!

3. Now create a method that uses our library:
  public function hello() { // Add an argument for cli interaction $argumets = array( array('alias' => '-n', 'arg' => '--name', 'help' => 'the name, we say hello to', 'type' => 1) ); // Initialize library with arguments array $this->load->library("command_line", $argumets); // Validate input if (!$this->command_line->valid_input()) { // If not valid, print some help print $this->command_line->get_help(); } else { // Else do your code print("Hello " . $this->command_line->get_argument('-n') . "!" . PHP_EOL); } } 

Let's sort this code in more detail.
First, we created an array with the possible parameters for our script:

So, setting the config, we initialize our library and check the input parameters for validity, just like in Form_validation!
 if (!$this->command_line->valid_input()) 

If it is not valid - there are no parameters, or an error has occurred, we simply call a method that tells us what is wrong:
 print $this->command_line->get_help(); 

If everything went as it should, then we can execute our code by obtaining the parameters of the arguments using the get_argument () method. You can refer to them both by full name and by alias:
 $this->command_line->get_argument('-n') === $this->command_line->get_argument('--name') 


4. Now that everything is ready, open the command line and check our script:
 php index.php cli home 

Since you have not entered any arguments, he will kindly tell you how to use it:
Usage: php index.php cli hello [OPTIONS]

Options are:
-n, --name the person's name, we say hello to

So, you need to enter a name:
 php index.php cli home -n Habrahabr 

What will bring the following:
Hello Habrahabr!

UPD: in addition, the script can also accept as a parameter a long string in quotes, after clearing it from them:
 php index.php cli home -n "Petr Ivanovich" 
Hello Petr Ivanovich!

Conclusion


As you can see, the library is quite primitive, and implements only the very base of functions that may be required when working with the command line. However, it can save some time on developing system cli scripts used, say, for cron tasks. This is really convenient - you can use all the written code of your models and libraries to create system tasks, such as, for example, generating a site map or recalculating any indicators.

I also want to warn you in advance that while the library version is pretty raw, and there may be some bugs. I am pleased to hear your comments on this issue, as well as I will be glad if you invest your contribution by joining the development on github .

Source: https://habr.com/ru/post/141448/


All Articles