An excerpt from http://www.sitepoint.com/console-wars-php-cli-libraries/, by Nicola Pietroluongo
I have always been a big fan of console commands and I try to provide a command line interface (CLI) as much as possible in most of my PHP projects.
In this article, I’ll briefly compare three PHP console command libraries:
- The Symfony console component (symfony/Console)
- The Hoa console (hoa/console)
- The Webmozart console (webmozart/console)
Origin Stories
The Symfony console is the oldest and the most popular one, used in many projects (and obviously part of the Symfony framework). With dozens of contributors, it became the first choice for many developers.
Hoa is a modular, extensible and structured set of PHP libraries that includes the Hoa console. It aims to be a bridge between industrial and research worlds, and this makes that project quite interesting.
The Webmozart console is the newest project, wants to be easier, test friendly and add new functionality on top of the Symfony console.
Dependencies, Size, and Complexity
The Symfony console has only suggested dependencies, as opposed to the Hoa console library that depends on some Hoa project libraries. The Webmozart project, too, directly depends on the Symfony console.
The Hoa console has the smallest number of LOC (Logical Lines of Code) ~1397, followed by the Symfony console ~2226 and the Webmozart ~3126 (without dependencies).
In order to have a rough indicator of the complexity of these projects, below is some data from their PHPLOC analysis*:
Description Symfony Hoa Webmozart
Cyclomatic Complexity
Average Complexity per LLOC 0.37 0.36 0.26
Average Complexity per Class 14.73 25.14 8.84
Average Complexity per Method 2.55 3.38 1.99
Dependencies
Global Accesses 3 20 1
Attribute Accesses 807 217 1285
Method Calls 1103 324 1320
*The analysis is performed only in the main source directory, excluding the test folder when present.
Practical example
Description
To have an overview of each library’s functionality and see the code in action, let’s write a business feature to describe a usage example:
Feature: I want to output a message to several people.
The message should be passed via the `--message` option and should be optional (default="Hello"),
the message should be followed by two or more names,
the message should be coloured with `--color=` (default="white") and/or in uppercase with `--up` (default=lowercase).
The final console call should be something like:
somemsg --message='Good Morning' Nicola Bruno --color=green --up
and the output should be:
GOOD MORNING NICOLA AND BRUNO
Implementation
First, we need to define a PHP Message, used in every console implementation, to handle the example.
Below is some pretty straightforward code:
class Message
{
/**
* Construct the class and initializes the properties.
*
* @param $names
* @param string $message
* @param bool $uppercase
*/
public function __construct($names, $message="Hello", $uppercase=false)
{
$this->names = implode(' and ', $names);
$this->message = $message;
$this->uppercase = $uppercase;
}
/**
* Generates the output.
*
* @return string
*/
public function getMessage()
{
$output = $this->message . ' ' . $this->names;
if ($this->uppercase) {
$output = strtoupper($output);
}
return $output;
}
}