cakephp控制台脚本Console/shell使用
在startup方法中设定参数
public function startup(){
// 配置log所需参数
$GLOBALS['_LOG']['name'] = strtolower($this->name);
if( ! isset($GLOBALS['_LOG']['calluuid']) ) {
$GLOBALS['_LOG']['calluuid'] = uniqid();
}
if( ! isset($_SERVER['REMOTE_ADDR']) ) {
$_SERVER['REMOTE_ADDR'] = 'localhost';
}
$GLOBALS['_LOG']['action'] = strtolower($this->command);
}
向终端输出信息
$tipStr = $index.':'.$succNum.' ';
$tipStrLen = strlen($tipStr);
echo $tipStr;
echo "\033[{$tipStrLen}D"; // \033[2D 表示光标往回退2格
从命令行获取参数
$args = $this->args;
$this->bankType = isset($args[0]) ? $args[0]: '';
$pageFrom = isset($args[1]) ? $args[1]: 1;
配置shell脚本的参数arg和选项opt
官网上的方式是在shell中重写 getOptionParser() 。
public function getOptionParser() {
$parser = parent::getOptionParser();
$parser->addArgument('type', array(
'help' => 'Either a full path or type of class.', 'required'=>true
))->addArgument('className', array(
'help' => 'A CakePHP core class name (e.g: Component, HtmlHelper).'
))->addOption('method', array(
'short' => 'm',
'help' => __('The specific method you want help on.')
))->description(__('Lookup doc block comments for classes in CakePHP.'));
return $parser;
}
使用 docker exec -it dnmp-php56 /apps/demo/app/Console/cake test --h
显示帮助,不执行脚本
/apps/demo/app/Console/cake 对当前用户需要有执行权限
运行结果如下:
Lookup doc block comments for classes in CakePHP.
Usage:
cake test [-h] [-v] [-q] [-h] [<type>] [<className>]
Options:
--help, -h Display this help.
--verbose, -v Enable verbose output.
--quiet, -q Enable quiet output.
--method, -m The specific method you want help on.
Arguments:
type Either a full path or type of class. (optional)
className A CakePHP core class name (e.g: Component, HtmlHelper). (optional)
Arguments 参数是指定位置顺序的,而 Options 和位置无关。
posted on 2020-04-03 10:15 aworkstory 阅读(341) 评论(0) 编辑 收藏 举报