PHP BOOTCAMP: Develop Your Own CLI Scripts
Developers run CLI Scripts every now and then and I bet you have used them severally. I mean Command line/ Shell Scripts.
The Question is:
Have you been able to create yours?
Have you wondered how these things are created?
As a Laravel Developer, have you wondered how php artisan
, php artisan <a-type-of-argument>
works?
It’s actually very simple. You can write those scripts in virtually any language.
Create a file named cab
. Now it shouldn’t have any extension like .txt or .php or any type at all.
cab
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#!/usr/bin/env php <?php /** * Message displayed to the console if the user supplies no command * @return void */ function helpMessage() { echo " ______ ___ _______ __ _____ _____ / ____/___ ____ _/_/ ___ / _______/ / / ___/ /____/ / / / __ / __ / / / / / ______ / /_ / / / /____ / /___/ /_/ / /_/ / /____/ / / /_____/ / ____ / /__/ /___/ ____/____/____/______/ /_/ /__/ _/___/____/ Usage: command [options] [arguments] Options: --drive Drive the cab install Install all the tools needed to drive the cab "; } /** * Checks if the arguments are valid and exist * @param $arg * @return void */ function validateArgs($arg) { if ( count($arg) == 1){ helpMessage(); return; } grabCommands($arg[1]); } /** * Gets commands passed as arguments to the command line * @param string $command * @return void */ function grabCommands($command) { if( $command === 'install') { echo " ......Installing ......gearbox, ......car handle, ......motor engine and spareparts Done. Installed Successfully"; } if( $command === '--drive') { echo "....The car is in motion now. Please Drive Carefully"; } } validateArgs($argv); |
#!/usr/bin/env
is actually what makes the file a script that can be executed straight from the terminal even without any file extension.
#!/usr/bin/env php
specifies the language that the script would be written in.
$argv
is PHP’s way of accepting arguments passed to the terminal
$argv
is an array
e.g php artisan migrate
$argv[0]
is artisan
$argv[1]
is migrate
e.g php prosper.php
$argv[o]
is prosper.php
$argv[1]
will give you a PHP Notice Error about an undefined offset because it does not exist.
From the code we have above, if we run:
1 |
php cab |
We’ll get:
because we didn’t pass in any command.
1 |
php cab install |
1 |
php cab --drive |
Awesome!!!
Now, you can see it’s that simple.
You have the knowledge now, So feel free to code up something much more functional and complex.
Check out the source code here
You can check out cool-ascii-faces php package I developed to have fun in the commandline and also learn how to abstract your code into modular components.
Please if you have any questions or observations, feel free to drop it in the comments section below.
- How to build your own Youtube – Part 10 - August 1, 2016
- How to build your own Youtube – Part 9 - July 25, 2016
- How to build your own Youtube – Part 8 - July 23, 2016
- How to build your own Youtube – Part 6 - July 6, 2016
- Introducing Laravel Password v1.0 - July 3, 2016
- How to build your own Youtube – Part 5 - June 28, 2016
- How to build your own Youtube – Part 4 - June 23, 2016
- How to build your own Youtube – Part 3 - June 15, 2016
- How to build your own Youtube – Part 2 - June 8, 2016
- How to build your own Youtube – Part 1 - June 1, 2016