PHPackages                             corneltek/getoptionkit - 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. corneltek/getoptionkit

ActiveLibrary

corneltek/getoptionkit
======================

Powerful command-line option toolkit

2.7.3(1y ago)1491.9M—1.8%24[7 issues](https://github.com/c9s/GetOptionKit/issues)[1 PRs](https://github.com/c9s/GetOptionKit/pulls)20MITPHPPHP &gt;=7.2.0

Since Mar 22Pushed 1y ago6 watchersCompare

[ Source](https://github.com/c9s/GetOptionKit)[ Packagist](https://packagist.org/packages/corneltek/getoptionkit)[ Docs](http://github.com/c9s/GetOptionKit)[ RSS](/packages/corneltek-getoptionkit/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (1)Versions (46)Used By (20)

GetOptionKit
============

[](#getoptionkit)

Code Quality

[![Build Status](https://camo.githubusercontent.com/c970fd3cfe59839d077b965da3a4278e51928cbaa64a3cd2ea30e9f116a6978f/68747470733a2f2f7472617669732d63692e6f72672f6339732f4765744f7074696f6e4b69742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/c9s/GetOptionKit)[![Coverage Status](https://camo.githubusercontent.com/534a5ffe7d0d93d521101bb84a755390703ee686daeaf48b537fdd6ddfc54064/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6339732f4765744f7074696f6e4b69742f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/c9s/GetOptionKit?branch=master)

Versions &amp; Stats

[![Latest Stable Version](https://camo.githubusercontent.com/fca0aea50982489c11244dafdc0e0fc18136353ef13fe846919334e220fb4fa8/68747470733a2f2f706f7365722e707567782e6f72672f636f726e656c74656b2f6765746f7074696f6e6b69742f762f737461626c65)](https://packagist.org/packages/corneltek/getoptionkit)[![Latest Unstable Version](https://camo.githubusercontent.com/f1fc599a2159c4743416fa67067ec4f901ccae5b6a6e5a74dd9b381915855135/68747470733a2f2f706f7365722e707567782e6f72672f636f726e656c74656b2f6765746f7074696f6e6b69742f762f756e737461626c65)](https://packagist.org/packages/corneltek/getoptionkit)[![Total Downloads](https://camo.githubusercontent.com/3222ce300b9ed7614b507f056af9ff15e082c3a9e887cc593367c8ccbf0c7330/68747470733a2f2f706f7365722e707567782e6f72672f636f726e656c74656b2f6765746f7074696f6e6b69742f646f776e6c6f616473)](https://packagist.org/packages/corneltek/getoptionkit)[![Monthly Downloads](https://camo.githubusercontent.com/3fc711ed4bf0c186a0d7375b56d62809b83e51fc0f0f8b7e91213fc5d5774a5c/68747470733a2f2f706f7365722e707567782e6f72672f636f726e656c74656b2f6765746f7074696f6e6b69742f642f6d6f6e74686c79)](https://packagist.org/packages/corneltek/getoptionkit)[![Daily Downloads](https://camo.githubusercontent.com/743b12af8b78f44e9f421415c480e523df2fb376127ac2563bc433713e80363d/68747470733a2f2f706f7365722e707567782e6f72672f636f726e656c74656b2f6765746f7074696f6e6b69742f642f6461696c79)](https://packagist.org/packages/corneltek/getoptionkit)[![License](https://camo.githubusercontent.com/dd54e0813e773b543ffcaf19c2cdb2f4462918a72c8a034a3b61d740d969082e/68747470733a2f2f706f7365722e707567782e6f72672f636f726e656c74656b2f6765746f7074696f6e6b69742f6c6963656e7365)](https://packagist.org/packages/corneltek/getoptionkit)

A powerful option parser toolkit for PHP, supporting type constraints, flag, multiple flag, multiple values and required value checking.

GetOptionKit supports PHP5.3, with fine unit testing with PHPUnit testing framework.

GetOptionKit is object-oriented, it's flexible and extendable.

Powering PHPBrew , CLIFramework  and AssetKit

Features
--------

[](#features)

- Simple format.
- Type constrant.
- Multiple value, requried value, optional value checking.
- Auto-generated help text from defined options.
- Support app/subcommand option parsing.
- Option Value Validator
- Option Suggestions
- SPL library.
- HHVM support.

Requirements
------------

[](#requirements)

- PHP 5.3+

Install From Composer
---------------------

[](#install-from-composer)

```
composer require corneltek/getoptionkit
```

Supported Option Formats
------------------------

[](#supported-option-formats)

simple flags:

```
program.php -a -b -c
program.php -abc
program.php -vvv   # incremental flag v=3
program.php -a -bc
```

with multiple values:

```
program.php -a foo -a bar -a zoo -b -b -b
```

specify value with equal sign:

```
program.php -a=foo
program.php --long=foo
```

with normal arguments:

```
program.php -a=foo -b=bar arg1 arg2 arg3
program.php arg1 arg2 arg3 -a=foo -b=bar

```

Option SPEC
-----------

[](#option-spec)

```
v|verbose    flag option (with boolean value true)
d|dir:       option require a value (MUST require)
d|dir+       option with multiple values.
d|dir?       option with optional value
dir:=string  option with type constraint of string
dir:=number  option with type constraint of number
dir:=file    option with type constraint of file
dir:=date    option with type constraint of date
dir:=boolean option with type constraint of boolean
d            single character only option
dir          long option name

```

Command Line Forms
------------------

[](#command-line-forms)

```
app [app-opts] [app arguments]

app [app-opts] subcommand [subcommand-opts] [subcommand-args]

app [app-opts] subcmd1 [subcmd-opts1] subcmd2 [subcmd-opts] subcmd3 [subcmd-opts3] [subcommand arguments....]

```

Documentation
-------------

[](#documentation)

See more details in the [documentation](https://github.com/c9s/GetOptionKit/wiki)

Demo
----

[](#demo)

Please check `examples/demo.php`.

Run:

```
% php examples/demo.php -f test -b 123 -b 333
```

Print:

```
* Available options:
      -f, --foo     option requires a value.
     -b, --bar +    option with multiple value.
    -z, --zoo []    option with optional value.
          -v, --verbose    verbose message.
            -d, --debug    debug message.
                 --long    long option name only.
                     -s    short option name only.
Enabled options:
* key:foo      spec:-f, --foo   desc:option requires a value.
    value => test

* key:bar      spec:-b, --bar +  desc:option with multiple value.
    Array
    (
        [0] => 123
        [1] => 333
    )

```

Synopsis
--------

[](#synopsis)

```
use GetOptionKit\OptionCollection;
use GetOptionKit\OptionParser;
use GetOptionKit\OptionPrinter\ConsoleOptionPrinter;

$specs = new OptionCollection;
$specs->add('f|foo:', 'option requires a value.' )
    ->isa('String');

$specs->add('b|bar+', 'option with multiple value.' )
    ->isa('Number');

$specs->add('ip+', 'Ip constraint' )
    ->isa('Ip');

$specs->add('email+', 'Email address constraint' )
    ->isa('Email');

$specs->add('z|zoo?', 'option with optional value.' )
    ->isa('Boolean');

$specs->add('file:', 'option value should be a file.' )
    ->isa('File');

$specs->add('v|verbose', 'verbose message.' );
$specs->add('d|debug', 'debug message.' );
$specs->add('long', 'long option name only.' );
$specs->add('s', 'short option name only.' );

$printer = new ConsoleOptionPrinter();
echo $printer->render($specs);

$parser = new OptionParser($specs);

echo "Enabled options: \n";
try {
    $result = $parser->parse( $argv );
    foreach ($result->keys as $key => $spec) {
        print_r($spec);
    }

    $opt = $result->keys['foo']; // return the option object.
    $str = $result->keys['foo']->value; // return the option value

    print_r($opt);
    var_dump($str);

} catch( Exception $e ) {
    echo $e->getMessage();
}
```

Documentation
-------------

[](#documentation-1)

See  for more details.

### Option Value Type

[](#option-value-type)

The option value type help you validate the input, the following list is the current supported types:

- `string`
- `number`
- `boolean`
- `file`
- `date`
- `url`
- `email`
- `ip`
- `ipv4`
- `ipv6`
- `regex`

And here is the related sample code:

```
$opt->add( 'f|foo:' , 'with string type value' )
    ->isa('string');

$opt->add( 'b|bar+' , 'with number type value' )
    ->isa('number');

$opt->add( 'z|zoo?' , 'with boolean type value' )
    ->isa('boolean');

$opt->add( 'file:' , 'with file type value' )
    ->isa('file');

$opt->add( 'date:' , 'with date type value' )
    ->isa('date');

$opt->add( 'url:' , 'with url type value' )
    ->isa('url');

$opt->add( 'email:' , 'with email type value' )
    ->isa('email');

$opt->add( 'ip:' , 'with ip(v4/v6) type value' )
    ->isa('ip');

$opt->add( 'ipv4:' , 'with ipv4 type value' )
    ->isa('ipv4');

$opt->add( 'ipv6:' , 'with ipv6 type value' )
    ->isa('ipv6');

$specs->add('r|regex:', 'with custom regex type value')
      ->isa('Regex', '/^([a-z]+)$/');
```

> Please note that currently only `string`, `number`, `boolean` types can be validated.

### ContinuousOptionParser

[](#continuousoptionparser)

```
$specs = new OptionCollection;
$spec_verbose = $specs->add('v|verbose');
$spec_color = $specs->add('c|color');
$spec_debug = $specs->add('d|debug');
$spec_verbose->description = 'verbose flag';

// ContinuousOptionParser
$parser = new ContinuousOptionParser( $specs );
$result = $parser->parse(explode(' ','program -v -d test -a -b -c subcommand -e -f -g subcommand2'));
$result2 = $parser->continueParse();
```

### OptionPrinter

[](#optionprinter)

GetOptionKit\\OptionPrinter can print options for you:

```
* Available options:
              -f, --foo   option requires a value.
              -b, --bar   option with multiple value.
              -z, --zoo   option with optional value.
          -v, --verbose   verbose message.
            -d, --debug   debug message.
                 --long   long option name only.
                     -s   short option name only.

```

Command-line app with subcommands
---------------------------------

[](#command-line-app-with-subcommands)

For application with subcommands is designed by following form:

```
[app name] [app opts]
             [subcommand1] [subcommand-opts]
             [subcommand2] [subcommand-opts]
             [subcommand3] [subcommand-opts]
             [arguments]

```

You can check the `tests/GetOptionKit/ContinuousOptionParserTest.php` unit test file:

```
// subcommand stack
$subcommands = array('subcommand1','subcommand2','subcommand3');

// different command has its own options
$subcommandSpecs = array(
    'subcommand1' => $cmdspecs,
    'subcommand2' => $cmdspecs,
    'subcommand3' => $cmdspecs,
);

// for saved options
$subcommandOptions = array();

// command arguments
$arguments = array();

$argv = explode(' ','program -v -d -c subcommand1 -a -b -c subcommand2 -c subcommand3 arg1 arg2 arg3');

// parse application options first
$parser = new ContinuousOptionParser( $appspecs );
$app_options = $parser->parse( $argv );
while (! $parser->isEnd()) {
    if (@$subcommands[0] && $parser->getCurrentArgument() == $subcommands[0]) {
        $parser->advance();
        $subcommand = array_shift( $subcommands );
        $parser->setSpecs( $subcommandSpecs[$subcommand] );
        $subcommandOptions[ $subcommand ] = $parser->continueParse();
    } else {
        $arguments[] = $parser->advance();
    }
}
```

Todo
----

[](#todo)

- Option Spec group.
- option valid value checking.
- custom command mapping.

Command Line Utility Design Concept
-----------------------------------

[](#command-line-utility-design-concept)

- main program name should be easy to type, easy to remember.
- subcommand should be easy to type, easy to remember. length should be shorter than 7 characters.
- options should always have long descriptive name
- a program should be easy to check usage.

General command interface
-------------------------

[](#general-command-interface)

To list usage of all subcommands or the program itself:

```
$ prog help

```

To list the subcommand usage

```
$ prog help subcommand subcommand2 subcommand3

```

Hacking
-------

[](#hacking)

Fork this repository and clone it:

```
$ git clone git://github.com/c9s/GetOptionKit.git
$ cd GetOptionKit
$ composer install

```

Run PHPUnit to test:

```
$ phpunit

```

License
-------

[](#license)

This project is released under MIT License.

###  Health Score

53

—

FairBetter than 97% of packages

Maintenance39

Infrequent updates — may be unmaintained

Popularity56

Moderate usage in the ecosystem

Community36

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 89.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~99 days

Recently: every ~280 days

Total

44

Last Release

517d ago

Major Versions

1.2.8 → 2.0.02014-07-01

PHP version history (2 changes)1.2.1PHP &gt;=5.3.0

2.7.0PHP &gt;=7.2.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/3cc34cde233b660869ff329ed8e20df611f75dfb61aab3e30889ac153d3e5e61?d=identicon)[c9s](/maintainers/c9s)

---

Top Contributors

[![c9s](https://avatars.githubusercontent.com/u/50894?v=4)](https://github.com/c9s "c9s (449 commits)")[![1franck](https://avatars.githubusercontent.com/u/1209308?v=4)](https://github.com/1franck "1franck (34 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (4 commits)")[![Gasol](https://avatars.githubusercontent.com/u/108053?v=4)](https://github.com/Gasol "Gasol (3 commits)")[![igorsantos07](https://avatars.githubusercontent.com/u/532299?v=4)](https://github.com/igorsantos07 "igorsantos07 (2 commits)")[![davidcole1340](https://avatars.githubusercontent.com/u/991872?v=4)](https://github.com/davidcole1340 "davidcole1340 (2 commits)")[![dlussky](https://avatars.githubusercontent.com/u/616063?v=4)](https://github.com/dlussky "dlussky (2 commits)")[![ErikBooij](https://avatars.githubusercontent.com/u/5747956?v=4)](https://github.com/ErikBooij "ErikBooij (2 commits)")[![MartyIX](https://avatars.githubusercontent.com/u/203266?v=4)](https://github.com/MartyIX "MartyIX (1 commits)")[![bitdeli-chef](https://avatars.githubusercontent.com/u/3092978?v=4)](https://github.com/bitdeli-chef "bitdeli-chef (1 commits)")[![robbertkl](https://avatars.githubusercontent.com/u/5704510?v=4)](https://github.com/robbertkl "robbertkl (1 commits)")[![RouatbiH](https://avatars.githubusercontent.com/u/23016064?v=4)](https://github.com/RouatbiH "RouatbiH (1 commits)")[![soundasleep](https://avatars.githubusercontent.com/u/3889656?v=4)](https://github.com/soundasleep "soundasleep (1 commits)")[![Sweetchuck](https://avatars.githubusercontent.com/u/591103?v=4)](https://github.com/Sweetchuck "Sweetchuck (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/corneltek-getoptionkit/health.svg)

```
[![Health](https://phpackages.com/badges/corneltek-getoptionkit/health.svg)](https://phpackages.com/packages/corneltek-getoptionkit)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
