I am proud to announce the first release of AutoShell, a library for building CLI commands in PHP 8.1 and up.

Just as with AutoRoute for web routing, AutoShell automatically maps CLI command names to PHP command classes in a specified namespace. For example, given a namespace of Project\Cli\Command, the CLI command name create-article automatically maps to the Project\Cli\Command\CreateArticle command class.

AutoShell is low-maintenance. Merely adding a class to your source code, in the recognized namespace and with the recognized main method name, automatically makes it available as a command.

AutoShell will automatically map command-line arguments to the command class main method parameters. For example, given this command class ...

<?php
namespace Project\Cli\Command;

use AutoShell\Help;

#[Help("Says hello to a _name_ of your choice.")]
class Hello
{
    public function __invoke(

        #[Help("The _name_ to say hello to.")]
        string $name

    ) : int
    {
        echo "Hello {$name}" . PHP_EOL;
        return 0;
    }
}

... AutoShell will recognize php bin/console.php hello world and output Hello world.

And as you can see, help is defined via attributes. Options are easy to define, too -- as is combining common option sets with command-specific ones.

There's a lot more to AutoShell; take a look at the documentation to get started!


You can read the Reddit discussion about this post here.

Are you stuck with a legacy PHP application? You should buy my book because it gives you a step-by-step guide to improving you codebase, all while keeping it running the whole time.