shso/cli 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

shso/cli

Composer 安装命令:

composer require shso/cli

包简介

Usefull cli related functions

关键字:

README 文档

README

Enhance php cli scripts experiance.

Install

Use Composer

$ composer require shso/cli

Composer will load the library automatically.

Manually

Download the script:

$ curl -fL -O https://github.com/ShahinSorkh/php-cli/raw/master/src/cli.php
# Or using wget
$ wget https://github.com/ShahinSorkh/php-cli/raw/master/src/cli.php

Import in your scripts:

require_once './cli.php';

Script Usage

Assuming following cli:

$ php script.php -f --action ACTION -- ARG1 ARG2 ARG3

Count arguments

cli\argc(); // returns 3

Get arguments

cli\arg(); // returns ['script.php', 'ARG1', 'ARG2', 'ARG3']
cli\arg(2); // returns 'ARG2'
cli\arg(4, 'foo'); // returns 'foo'

Get options

cli\opt(); // returns ['f' => true, 'action' => 'ACTION']
cli\opt('f'); // returns true
cli\opt('action'); // returns 'ACTION'
cli\opt('q'); // returns null
cli\opt('foo', 'bar'); // returns 'bar'

Define usage

You can define a strict usage with fixed options and arguments. To do so, you should use cli\usage() method which takes an array as argument with possible keys of args, opts, desc.

Define arguments

Arguments can be optional.

cli\usage([
    'args' => [ 'REQUIRED_ARG', '[OPTIONAL_ARG]' ],
]);

Define options

Options can be optional and can accept arguments. Also options can be short or long.

cli\usage([
    'opts' => [
        '-f' => null, // optional, without arg, short
        '*c' => null, // required, without arg, short
        '-o' => 'OPT_ARG', // optional, with arg, short
        '-no-dev' => null, // optional, without arg, long
        '*config' => 'CONFIG', // required, with arg, long
    ],
]);

Define additional descriptions

If you need to describe about options or arguments, you can do so.

cli\usage([
    'args' => [ 'CONFUSING_ARG' ],
    'opts' => [
        '-f' => null,
    ],
    'desc' => [
        'CONFUSING_ARG' => 'Some descriptions that make the arg not confusing!',
        '-f' => 'Describe -f option can force the operation or whatever.',
    ],
]);

Command Line Usage

This package supports following syntax:

$ php script.php --opt --opt-with-arg OPT_ARG -- ARG [OPTIONAL_ARG]

Passing arguments

  • Arguments has to be the last values. This means the following syntax is not acceptable:
$ php script.php ARG1 --opt1 ARG2 --opt2 OPT2_ARG ARG3 ARG4

Whenever an argument detected, every value after that is considered arguments. Means all of ARG1, --opt1, ARG2, --opt2, OPT2_ARG, ARG3, ARG4, are separate arguments.

  • Every non-option value after an option, is considered the argument of the option. This means in the following syntax, ARG1 would be available as the value of the --op1 option (see below):
$ php script.php --op1 ARG1

If you need a boolean option and some arguments, use -- before arguments. like:

$ php script.php --op1 -- ARG1
  • If you need input from stdin, use -. like:
$ php script.php - < somefile.txt
$ cat somefile.txt | php script.php -

It is possible to pass other arguments and options alongside.

Passing options

Every passed value starting with - before the first detected argument, is an option. Options can get additional value as their argument. like:

$ php script.php -f --file SOME_FILE
$ php customzipcompress.php --rm --compression best -f FILE1,FILE2 -d DIR1

If you are going to pass arguments, options has to be terminated with --. like:

$ php script.php -t -g OPT_ARG -- ARG1

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-05-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固