承接 kentaro-a/phpcommander 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kentaro-a/phpcommander

Composer 安装命令:

composer require kentaro-a/phpcommander

包简介

PHP Command line interface. Easy to handle flags and options.

README 文档

README

Easy to handle flags and options which passed through by $argv. Flags and options have to be start with "-" or "--". If you've already have any classes and you'd like to make something like CLI batches with it, PHPCommander makes it easy.

Requirements

  • php >= 7.1

Install

$ composer require kentaro-a/phpcommander

How To Use

Make Flags for command

Use new Flg(string $flg, bool $must=false, string $help="", array $validators=[]) to make Flag. you can make custom validator class like preset validator ValidatorNotEmptyString.

param desc
$flg require, this must start with - or --
$must optional default=false, if true check for being passed flg in cli args.
$help optional default="", help text to be shown.
$validators optional default=[], an array of sub classes of Validator to validate flg.
$flg1 = new Flg("--param", true, "my parameter", [new ValidatorNotEmptyString("Flg --param doesn't permit blank.")]);

You can make Procedure which implements ProcedureInterface.

class EchoProcedure implements ProcedureInterface {
	public function __invoke(array $flgs=[]) {
		// Whatever you want.
		echo $flgs["param"] ."\n";
	}
}

Make Command

After making Procedure you can set it into Command. Using new Command(string $command_name, ProcedureInterface $procedure, Flgs $flgs=null, string $description="").

param desc
$command_name require, this must not start with - or --
$procedure require, procedure to be called when command passed.
$flgs optional default=null, Flgs class contains an array of Flg classes.
$description optional default="", description text to be shown in help.
$command_echo = new Command(
	"echo",
	new EchoProcedure(),
	new Flgs([$flg1]),
	"Simple echo parameter",
);

Add Commands into Commander and invoke registered command.

Commander::invoke obtains command name and options from $argv then invokes relevant Procedure::__invoke().

// Make instance.
$cli = new Commander($argv, [$command_echo]);

// you can also add other command by pushing into $commands or using add method like $cli->addCommand($command).

// Invoke procedure related on command name passed by $argv
$ret = $cli->invoke();

Show help.

You can show registered command list without any parameters.

$ php batch.php

You can also pass the reserved flg after command_name like -h or --help to show help.

$ php batch.php echo -h(--help)

Detail

See examples.

统计信息

  • 总下载量: 9
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-09-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固