Member-only story
Building a command line app in PHP
The complete package from option handling to writing a man page
If you’re like me, you’re more comfortable writing simple scripts in PHP than C or Go or whatever language people are currently recommending. Fortunately, PHP is more than capable of producing command line apps for use in Linux or macOS.
In this tutorial, I’ll explain:
- How to implement things like option handling and the environment in PHP.
- Components of a command line app including things like a man page, config files, and so on.
As a minimum, you should be able to use these concepts and techniques to develop your own simple command line applications, in your language of choice.
I’ll be using excerpts from my program, lingth, to demonstrate these concepts. (You might want to have the source code open alongside this article as you read it.)
About the program, lingth
Lingth reports on lines that are longer than a certain number of characters. It’s useful for keeping to coding standards which, for example, specify an 80-character maximum.