php 控制台脚本,PHP Simple Console:单文件控制台框架,以帮助您编写构建脚本

这篇博客介绍了Asika/Simple-Console,一个PHP的单文件命令行框架,用于编写构建脚本。通过Composer安装,然后使用闭包或自定义类来创建命令。框架提供了错误处理、参数检查、命令委托等功能,支持子命令和自定义帮助信息。示例代码展示了如何使用该框架输出信息、处理输入和执行不同命令。
摘要由CSDN通过智能技术生成

PHP Simple Console

Single file console framework to help you write build scripts.

Installation

Use composer:

composer require asika/simple-console

Or downlaod single file to use: Download Here

Getting Started

Use closure

#!/bin/sh php

// Include single file

include_once __DIR__ . '/Console.php';

// Or use composer

include_once __DIR__ . '/vendor/autolod.php';

$app = new \Asika\SimpleConsole\Console;

// Use closure

$app->execute(function (\Asika\SimpleConsole\Console $app)

{

// PHP 5.3

$app->out('Hello');

// PHP 5.4 or higher use $this

$this->out('Hello');

// Return TRUE will auto convert to 0 exitcode.

return true;

});

Or Create your own class.

class Build extends \Asika\SimpleConsole\Console

{

protected $help =<<

[Usage] php build.php

[Options]

h | help Show help information

v Show more debug information.

HELP;

protected function doExecute ()

{

$this->out('Hello');

// Return TRUE will auto convert to 0 exitcode.

return true;

}

}

$app = new Build;

$app->execute();

Show HELP

Add -h or --help to show usage, you can add custom usage to $this->help, or override $this->getHelp().

If you want to change h and help option, override $this->helpOptions = array('...').

Handle Error

Just throw Exception in doExecute(), Console will auto catch error.

throw new \RuntimeException('...');

Add -v to show backtrace if error.

Handle Wrong Arguments

Wrong Argument use \Asika\SimpleConsole\CommandArgsException

$arg = $this->getArgument(0);

if (!$arg)

{

throw new \Asika\SimpleConsole\CommandArgsException('Please enter a name.');

}

Console will auto show help information.

[Warning] Please enter a name.

[Usage] console.php

[Options]

h | help Show help info.

v Show more debug information.

Multiple Commands

Use delegate() to delegate to different methods.

//...

protected function doExecute()

{

return $this->delegate($this->getArgument(0));

}

protected function foo()

{

$this->getArgument(1); // bar

}

protected function baz()

{

// ...

}

Now you can add sub commands

php console.php foo bar

php console.php baz

If you want to strip first argument after delgated, you can follow this code:

$this->delegate(array_shift($this->args));

Now can use getArgument(0) in sub method and ignore the first command name.

The is another way:

$command = array_shift($this->args);

$this->delegate($command, ...$this->args);

protected function foo($first, $second = null)

{

}

API

getArgument($order[, $default = null])

$first = $this->getArgument(0, 'default value');

setArgument($order, $$value)

$this->setArgument(1, 'value');

getOption($name: array|string[, $default = null])

Get option --foo

$this->getOption('foo');

Get option -f or --foo, first match will return.

$this->getOption(array('f', 'foo'));

NOTE: -abc will convert to a => 1, b => 1, c => 1 And -vvv will convert to v => 3

setOption($name, $value)

Set otpion to toption list. $name also support array.

out($string[, $newline: bool = false])

Write to STDOUT,

$this->out('Hello')->out('World');

err($string[, $newline: bool = false])

Write to STDERR

$this->err('Hello')->err('World');

in($string[$default = null, $bool = false)

Ask a question, read from STDIN

$un = $this->in('Please enter username: ', 'default_name');

Read as boolean, add true to third argument:

$bool = $this->in('Are you sure? [Y/n]', [default true/false], true);

yes, y, 1, true will convert to TRUE

no, n, 0, false will convert to FALSE

exec($cmd)

A proxy to execute a cmd by exec() and return value.

It will add a title >> {your command} before exec so you will know what has been executed.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值