PHPackages                             shso/cli - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. shso/cli

ActiveLibrary[CLI &amp; Console](/categories/cli)

shso/cli
========

Usefull cli related functions

v1.1.1(6y ago)08MITPHPPHP &gt;7.1

Since May 22Pushed 6y ago1 watchersCompare

[ Source](https://github.com/ShahinSorkh/php-cli)[ Packagist](https://packagist.org/packages/shso/cli)[ Docs](https://github.com/ShahinSorkh/php-cli)[ RSS](/packages/shso-cli/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (1)Versions (5)Used By (0)

php-cli
=======

[](#php-cli)

Enhance php cli scripts experiance.

Install
-------

[](#install)

### Use Composer

[](#use-composer)

```
$ composer require shso/cli
```

Composer will load the library automatically.

### Manually

[](#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
------------

[](#script-usage)

Assuming following cli:

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

### Count arguments

[](#count-arguments)

```
cli\argc(); // returns 3
```

### Get arguments

[](#get-arguments)

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

### Get options

[](#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
------------

[](#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

[](#define-arguments)

Arguments can be optional.

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

### Define options

[](#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

[](#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
------------------

[](#command-line-usage)

This package supports following syntax:

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

### Passing arguments

[](#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 -
