Frameworks Aside — Writing a simple interactive command line script using php

Lawrence Agbani
4 min readOct 9, 2017

--

I have been working with php for a few years now and like a lot of people, I use frameworks for quite a lot of projects. Frameworks are great but for the purpose of this guide, we’ll be using bare php.

Well, command line scripts are mainly utility pieces of code (written mostly in scripting languages)that get a user’s input, perform certain operations based on the user’s input, and return a response on the command line (I know. Basic IO). Certain frameworks use them to automate certain things. Take Laravel for instance, with the artisan command line tool that takes care of creating models, controllers, etc. by simply running a few commands from the command line.

Awesome! now we have an idea of what command line scripts are, let’s go ahead and build ours! 💪🏾💪🏾. But before that, let’s quickly break down what we wish to achieve. We wish to;

  • Execute the script from the command line (This is obvious but it just had to be on the list somehow ¯\_(ツ)_/¯).
  • Pass arguments to the script.
  • Display responses based on the arguments passed in.

Quite a lengthy list there, which means we can quickly get our hands dirty.

Requirements?🤔

Don’t worry, nothing is actually that serious so just have at least php 5.5 installed on your machine and you’re good to go 🤗.

Show Me Code!!!👺

Easy now. First create a php file using your favorite IDE and call it, say, cliscript.php. Now to be sure the file works as it should let’s echo something from it (which will take care of the first goal on our super lengthy list above 👀).

<?phpecho "Hello Script!";//?> No point closing this since it's entirely a php script?🤔

Now run the script by simply doing php <scriptname> and see what shows up.

Script ran without any arguments

You should see something like the above image. You did? Great! now let’s clear out the remaining goals 🔥🔥. You didn’t? Go through everything from the top, you might have just missed something. For the second goal, php has a variable called $argv. It basically contains an array of all arguments passed from command line to a script. You can read more about it from the official documentation. First modify the script to puke the contents of $argv to the command line.

<?php var_dump($argv);//?> We might just have to leave this for the gods to decide🤔

Now let’s run the script with some arguments. Just do php <scriptname> arg1 arg2 arg3.

Script ran with some arguments

We got a var dump of everything in the $argv array, but wait, the first element is our script name so we might want to avoid calling that element moving forward. This means that our legit arguments from the user starts from the second element onward. Great! Now we’ve cleared our second goal, let us clear out the last one. Let our script accept two arguments from the user, one with their name and the other an action, let us use time and date as actions for now.

<?php// Skip element at index 0 since that only contains our script name
$username = $argv[1];
$action = strtolower($argv[2]);
$actionResponse;
// Let us check what action was sent and respond accordingly
switch($action){
case 'time':
$actionResponse = date('H:i:s');
break;
case 'date':
$actionResponse = date('Y-m-d');
break;
}
// Now echo a response to the command line
echo "Hello $username, the $action is $actionResponse";

Now let’s run our script. Just do php <scriptname> <your name> <action>.

Script ran with interactive responses

If you followed everything from the top, your script should give a response like the one in the image above. We are almost there, but what if the user inputs an action we don’t understand or want to deal with just yet?🤔. Now, let us quickly modify our script and add a check for that.

<?php// Skip element at index 0 since that only contains our script name
$username = $argv[1];
$action = strtolower($argv[2]);
$actionResponse;
// Let us check what action was sent and respond accordingly
switch($action){
case 'time':
$actionResponse = date('H:i:s');
break;
case 'date':
$actionResponse = date('Y-m-d');
break;
default:
$actionResponse = '';
break;
}
if(empty($actionResponse)){
// Let the user know we do not understand the action
echo "Hello $username, your action was not understood";
}else{
// At this point we do understand the action :)
echo "Hello $username, the $action is $actionResponse";
}

Now running the script with a wrong action should give a response accordingly.

Script ran with an error response

Great! we finally cleared our list of deliverables. Simple wasn’t it? Now that you know how to write cli scripts with php (If you didn’t before), go ye into the world and be awesome!!! 💪🏾. You can as well clap for yourself (and me) while you’re at it.

--

--