定制 amsify42/php-command-line 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

amsify42/php-command-line

Composer 安装命令:

composer require amsify42/php-command-line

包简介

This is a php package for dealing with command line parameters

README 文档

README

This is a php package for dealing with command line parameters.

Installation

$ composer require amsify42/php-command-line

Table of Contents

  1. Introduction
  2. Getting Parameters
  3. Double Hyphen Param
  4. To String
  5. CLI Task

1. Introduction

The class Amsify42\CommandLine\CommandLine class helps get the cli parameter passed in any of the format.

php file.php 42 amsify "some description"

or

php file.php -id 42 -name amsify -desc "some description"

or

php file.php -id=42 -name=amsify -desc="some description"

2. Getting Parameters

To get all the parameters passed you can call this method

use \Amsify42\CommandLine\CommandLine;
CommandLine::getParams();

or with helper method

cli_get_params();

To get the specific key param

CommandLine::getParam('id');

or

cli_get_param('id');

To check the whether the cli param exist

CommandLine::isParam('id');

or

cli_is_param('id');

It will return true if exist else false

3. Double Hyphen Param

The helper class can also detect the param name passed with double hyphen

php file.php --global

We can use the same isParam method to check whether global param passed or not.

4. To String

We can also convert all the cli params passed back to the string with this method.

echo CommandLine::toString();

or

echo cli_to_string();

5. CLI Task

Create a class under any directory, Example: /app/Task/

<?php

namespace App\Task;

use Amsify42\CommandLine\Task\BaseTask;

class Test extends BaseTask
{
    public function init()
    {
     	printMsg("Doing something");   
    }
}

You can run this script from console from other file like test.php

<?php
require_once __DIR__.'/vendor/autoload.php';
\Amsify42\CommandLine\Task::run(\App\Task\Test::class);

and run this file

php test.php

If script file name is passed directly from command line

require_once __DIR__.'/vendor/autoload.php';
$task = new \Amsify42\CommandLine\Task();
$task->process();

and run the file with task file name

php test.php App\Task\Test

For programmatically running the script, you can use this method

\Amsify42\CommandLine\Task::run(\App\Task\Test::class);

For running script in background asynchronously, you need to pass 3th param as true

\App\Console::run(\App\Task\Test::class, [], true);

Params/Validations

You can also pass cli params along with script like this

php test.php App\Task\Test -id 1 -name Kyro

Through method

\Amsify42\CommandLine\Task::run(\App\Task\Test::class, ['id' => 1, 'name' => 'Kyro']);

and can collect these params in the script

<?php

namespace App\Task;

use Amsify42\CommandLine\Task\BaseTask;

class Test extends BaseTask
{
    public function init()
    {
    	$id 	= $this->input('id');
    	$name 	= $this->input('name');
     	// do the remaining
    }
}

To validate and check whether the params exist, you can do this

public function init()
{
	$this->validate(['id', 'name']);
 	// do the remaining
}

Further script will not be executed if validation fails.

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固