xy2z/cliclass

从PHP类创建一个简单的CLI工具

3.0.0 2020-03-27 06:45 UTC

This package is auto-updated.

Last update: 2024-09-29 05:29:43 UTC


README

(以前称为SlimConsole)

从PHP类创建一个简单的CLI工具。

  • 所有公共方法都将从CLI中可用。
  • PHP文档将作为描述显示。
  • 方法参数将自动验证。
  • 支持多个类。

要求

  • PHP 7.0或更高版本

安装

composer require xy2z/cliclass

用法

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,
]);

结果

$ 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