PHPackages                             cheprasov/php-cli-args - 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. cheprasov/php-cli-args

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

cheprasov/php-cli-args
======================

Easy way to gets options from the command line argument list

3.0.0(7y ago)14101.2k↑76.1%2[2 issues](https://github.com/cheprasov/php-cli-args/issues)2MITPHPPHP &gt;=5.5CI failing

Since Nov 12Pushed 4y ago2 watchersCompare

[ Source](https://github.com/cheprasov/php-cli-args)[ Packagist](https://packagist.org/packages/cheprasov/php-cli-args)[ Docs](http://github.com/cheprasov/php-cli-args)[ RSS](/packages/cheprasov-php-cli-args/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (1)Versions (5)Used By (2)

[![MIT license](https://camo.githubusercontent.com/4661abfe916186acde514558e7f040833cb63ba7098401a51ce339cbb2b4cf9e/687474703a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](http://opensource.org/licenses/MIT)

CliArgs v3.0.0 for PHP &gt;= 5.5
================================

[](#cliargs-v300-for-php--55)

About
-----

[](#about)

Class **CliArgs** helps to get options from the command line argument list easy.

Features
--------

[](#features)

- CliArgs uses **$GLOBAL\['argv'\]** as base, and it does not use function **getopt()**.
- It does not implement logic for 'required', it should be in your side.
- It helps to get options easy from command line argument list.
- It generates help info about options based on config.
- Flexible configuration and data filtering.

Note
----

[](#note)

This class is not workable when [register\_argc\_argv](http://php.net/manual/en/ini.core.php#ini.register-argc-argv) is disabled.

Usage
-----

[](#usage)

### Example

[](#example)

run

```
> example.php --name=Alexander --age=32 --sex=m
or
> example.php -n Alexander -a 32 -s m

```

example.php

```
$config = [
    'name' => 'n',
    'age' => 'a',
    'sex' => 's'
];
$CliArgs = new CliArgs($config);

echo $CliArgs->getArg('name'); // Alexander
echo $CliArgs->getArg('n'); // Alexander

echo $CliArgs->getArg('age'); // 42
echo $CliArgs->getArg('a'); // 42

echo $CliArgs->getArg('sex'); // m
echo $CliArgs->getArg('s'); // m
```

### Config

[](#config)

Note: all params from cli that you want to use should be specified in config, otherwise they will be ignored.

```
$config = [
    // You should specify key as name of option from the command line argument list.
    // Example, name  for --param-name option
    'param-name' => [

        'alias' => 'p',
            // [optional], [string]
            // Alias helps to have short or long name for this key.
            // Example, name  for -p option

        'default' => false,
            // [optional], [mixed], [default = null]
            // Default value will returned if param is not setted
            // or params has not value.

        'help' => 'Some description about param',
            // [optional], [string]
            // Text that will returned, if you request help

        'filter' => 'int',
            // [optional], [string | array | callable]
            // Filter for the return value.
            // You can use next filters: flag, bool, int, float, help, json, ,

            // 'int' - cast to integer before return.
            // 'float' - cast to float before return.
            // 'bool' - cast to bool before return. Yes, true, 1 = TRUE, other = FALSE
            // 'json' - decode JSON data before return.
            // 'flag' - will return TRUE, if key is exists in command line argument list, otherwise - FALSE
            //  - use array for enums. Example use ['a', 'b', 'c'] to get only one of these.
            //  - use function($value, $default) { ... } to process value by yourself
    ]
];

$CliArgs = new CliArgs($config);
```

Examples of config:

Example 1

```
// Simple configs

// The config1 and config2 are equal
$config1 = ['foo', 'bar', 'a'];
$config2 = [
    'foo' => [],
    'bar' => [],
    'a' => [],
];

// The config3 and config4 are equal
$config3 = ['foo' => 'f', 'bar' => 'b', 'a'];
$config4 = [
    'foo' => [
        'alias' => 'f',
    ],
    'bar' => [
        'alias' => 'b',
    ],
    'a' => [],
];
```

Example 2

```
$config = [
    'help' => [
        'alias' => 'h',
        'help' => 'Show help about all options',
    ],
    'data' => [
        'alias' => 'd',
        'filter' => 'json',
        'help' => 'Some description about this param',
    ],
    'user-id' => [
        'alias' => 'u',
        'filter' => 'int',
        'help' => 'Some description about this param',
    ]
];
$CliArgs = new CliArgs($config);
```

```
Show help
> some-script.php --help

Show help only for param data
> some-script.php --help data

Show help for all params data
> some-script.php --help data

All the same:
> some-script.php --data='{"foo":"bar"}' --user-id=42
or
> some-script.php --data '{"foo":"bar"}' --user-id 42
or
> some-script.php -d '{"foo":"bar"}' --user-id 42
or
> some-script.php -d '{"foo":"bar"}' -u 42
