DOCOPT-PHP PHP Port of Command Line Options Parser Library

What is DOCOPT-PHP?

DOCOPT-PHP is the PHP version of Vladimir Keleshevs original DOCOPT which is wrote in Python. This little gem of an open source library is built to do ONE task and one task only! Read Parse Command Line Input Options (by the way, it does an excellent job of it!)

You simply include the single file docopt.php into your command line PHP project file. You define the available Options that a user can pass to your script in the command line in a variable and then pass that variable to the docopt PHP Class Method handle(). This is where the beauty of it comes in DOCOPT then parses your options and reads the Command Line Input that is passed to your script. It shows the user the available options and when they pass in options it returns a PHP Array in your script which will tell you the value passed in for each available option. If an option is empty then that PHP Array value will be false. If the option is passed in and matches an available option you defined, then it will give you the value or TRUE.

This article is simple an introduction to the library. You should view the GitHub page here DOCOPT-PHP which has the up to date Documentation. THe Docs also show all the awesome parsing capabilities and the format that the Options can be set so be sure to check that page out as well.

Enough with the talk, lets see some example code to better understand

Example Usage of DOCOPT-PHP

Now in the command line I pass in this value:

The DOCOPT library parses my input and returns this to the Users command prompt:

Back in my test.php file I receive an a simple associative array value returned from

with options, arguments and commands as keys, spelled exactly like in your help message.

Learn more about DOCOPT and DOCOPT-PHP

It really is as simple as that! Go check out the GitHub page and read the Docs for more information on this nice little tool. https://github.com/docopt/docopt.php

Here is the Python version which is the original version that the PHP version was ported from. http://docopt.org/

There is a very useful online Tool to test what DocOpts will return using just you web browser here http://try.docopt.org

Do you find this library useful? Let me know in the comments!

PS) Ignore the weird characters at the end of my code snippetsmy syntax highlighter is acting up!