xy2z/cliclass

Create a simple CLI tool from a PHP class

3.0.0 2020-03-27 06:45 UTC

This package is auto-updated.

Last update: 2024-10-29 05:41:15 UTC


README

(previously known as SlimConsole)

Create a simple CLI tool from a PHP class.

  • All public methods will be available to run from cli.
  • PHPdocs will be displayed as the description.
  • Method arguments are automatically validated.
  • Supports multiple classes.

Requires

  • PHP 7.0 or above

Install

composer require xy2z/cliclass

Usage

require '/path/to/vendor/autoload.php';

use xy2z\CliClass\CliClass;

class Router {
	/**
	 * Says hello world.
	 */
	public function hello_world() {
		echo 'Hello world.';
	}

	/**
	 * Says hello to $name.
	 */
	public function hello(string $name) {
		echo 'Hello ' . $name;
	}
}

CliClass::init($argv, [
	Router::class,
]);

Result

$ php cli.php
Usage:
 command [arguments]

Available commands:
 hello_world  Says hello world.
 hello        <string $name> Says hello to $name.
$ php cli.php hello_world
Hello world.
$ php cli.php hello
Usage: hello <string $name>
Error: Missing argument 2 for $name (no default value)
$ php cli.php hello Peter
Hello Peter