pmjones/auto-shell
AutoShell maps CLI command names to PHP command classes in a namespace, reflecting on a main method to parse args/options (scalars or arrays). Add a class in the command directory and it becomes available automatically—no dependencies, minimal setup.
composer require pmjones/auto-shell
bin/console.php:
<?php
use AutoShell\Console;
require dirname(__DIR__) . '/vendor/autoload.php';
$console = Console::new(
namespace: 'App\\Cli\\Command',
directory: __DIR__ . '/../src/Cli/Command',
);
exit($console($_SERVER['argv']));
src/Cli/Command/Greet.php):
<?php
namespace App\Cli\Command;
class Greet
{
public function __invoke(string $name): int
{
echo "Hello, {$name}!\n";
return 0;
}
}
php bin/console.php greet Alice
__invoke() (or custom method) in the configured namespace.string $name).php bin/console.php <command> <args>.src/Cli/Command/ (or configured directory).UserCreate → user:create).__invoke(), but customizable via method in Console::new().Options interface + #[Option] attributes.
class UserCreateOptions implements Options
{
public function __construct(
#[Option('f,force')]
public readonly ?bool $force = false,
) {}
}
Options as a parameter to __invoke().
public function __invoke(UserCreateOptions $options, string $name): int
{
if ($options->force) { ... }
}
Console::new() to resolve dependencies.
$console = Console::new(
namespace: 'App\\Cli\\Command',
directory: __DIR__ . '/../src/Cli/Command',
factory: fn(string $class) => app()->make($class),
);
#[Help] on the class.
#[Help("Creates a new user.")]
class UserCreate {}
help to #[Option] or #[Help] on parameters.
#[Help("The user's name.", "The name of the user to create.")]
string $name,
array type-hint with CSV input (e.g., --tags=tag1,tag2).y/n, 1/0, etc....string $args for catch-all arguments.Options classes.
public function __invoke(
GlobalOptions $global,
UserCreateOptions $local
): int { ... }
Options classes.namespace in Console::new() matches your command classes.__invoke() or explicitly set method.-v in both GlobalOptions and UserCreateOptions) throw OptionAlreadyDefined.user:create ≠ User:Create).Console::new(..., debug: true).php bin/console.php help <command>.#[Option] values match parameter types (e.g., bool for flags)..php files in the configured directory are scanned.suffix in Console::new() if commands don’t follow PascalCase (e.g., suffix: 'Command').Console to modify help output (e.g., add colors).$console($_SERVER['argv']) in logic for logging or metrics.__invoke() and return non-zero exit codes for errors.Shell instance.null or empty strings in __invoke().null if the option isn’t provided (e.g., ?bool $verbose).php bin/console.php greet "Alice Bob").How can I help you explore Laravel packages today?