An object-oriented option parser library for PHP, which supports type constraints, flag, multiple flag, multiple values, required value checking

Overview

GetOptionKit

Code Quality

Build Status Coverage Status

Versions & Stats

Latest Stable Version Latest Unstable Version Total Downloads Monthly Downloads Daily Downloads License

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 https://github.com/phpbrew/phpbrew, CLIFramework https://github.com/c9s/CLIFramework and AssetKit https://github.com/c9s/AssetKit

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

  • PHP 5.3+

Install From Composer

composer require corneltek/getoptionkit

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

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

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

See more details in the documentation

Demo

Please check examples/demo.php.

Run:

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

Print:

* Available options:
      -f, --foo <value>    option requires a value.
     -b, --bar <value>+    option with multiple value.
    -z, --zoo [<value>]    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 <value>  desc:option requires a value.
    value => test

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

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

See https://github.com/c9s/GetOptionKit/wiki for more details.

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

$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

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

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

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

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

To list usage of all subcommands or the program itself:

$ prog help

To list the subcommand usage

$ prog help subcommand subcommand2 subcommand3

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

This project is released under MIT License.

Issues
  • [BUG] ContinuousOptionParser fail to parse multiple  --option=value

    [BUG] ContinuousOptionParser fail to parse multiple --option=value

    Please test using this command, it will fail

    "script.php subcommand --data=thedata --limit=30 --batch=10 --index=theindex argument1 argument2"
    

    The parser only parse data, limit, and batch, and ignoring the rest (index, argument1, argument2 is not parsed

    opened by jeffreycahyono 21
  • Can not enter negative numbers

    Can not enter negative numbers

    If number begins with -, it is treated as an option. Current workaround: use something like: -o " -1" because -o "-1" will not work.

    Bug 
    opened by vasek125 10
  • Passing multiple short options is not working

    Passing multiple short options is not working

    If I have multiple short options in my command and I pass them like -mv I'll just get the first one as a passed option. Using -l -v works.

    Also, using -vm4 does the same, while it should give me the v option as boolean, and m as 4. Doing -m4v gives me m without the value as well, and nothing else.

    Bug 
    opened by igorsantos07 9
  • Quoted string arguments and spaces

    Quoted string arguments and spaces

    The library doesn't appear to support arguments such as

    command --option="foo bar baz"
    

    This becomes

    ['"foo', 'bar', 'baz"']
    

    ...rather than

    ['foo bar baz']
    

    Is this something that you are planning to/want to implement?

    Bug 
    opened by mrchimp 8
  • No documentation on

    No documentation on "normal" arguments

    Read-me mentions ability to mix with normal arguments here:

    https://github.com/c9s/GetOptionKit#supported-option-formats

    However I can't find a way to implement this:

    myapp [ -s <system_id> ] file.txt
    

    I was expecting than parser would remove all arguments from parameter leaving normal arguments inside, but it's not happening.

    opened by romaninsh 7
  • Enable custom validator on Option

    Enable custom validator on Option

    The customer validator you could set on an Option wasn't triggered during option parsing, this required you to call $option->validate($option->value)) manually, which is cumbersome and counterintuitive.

    Sidenote: I noticed PHPUnit wasn't included as a dependency in composer.json, any reason for that? It's hard to run the unit tests that way ;-)

    opened by ErikBooij 7
  • Check for required value is now validates emptiness of next argument

    Check for required value is now validates emptiness of next argument

    Hi. There was an issue with checking required value:

    1. Write in your php script $GetOptionKit->add('t|test:', 'required value');
    2. Run it as follows: ./script --test
    3. There is no exception on parsing args.
    4. There, I fixed it!)

    P.S. If I've misunderstood concept of 'option requires a value.', I'm sorry.

    opened by dlussky 7
  • Add PHPUnit as a dev dependency

    Add PHPUnit as a dev dependency

    opened by ErikBooij 4
  • Arguments aren't consumed for

    Arguments aren't consumed for "multiple" options

    When defining a "single" option (e.g. foo:=string), that option's argument will be consumed and not returned with the remaining arguments from getArguments(), but a "multiple" option (e.g. bar+=string) does not consume its argument and leaves it with the rest of the arguments returned by getArguments().

    $specs = new \GetOptionKit\OptionCollection();
    $specs->add('test1+=string', 'Test 1');
    $specs->add('test2:=string', 'Test 2');
    
    $parser = new \GetOptionKit\OptionParser($specs);
    
    $result = $parser->parse(explode(' ', '--test1 opt1 --test2 opt2 arg1 arg2'));
    
    $result->getArguments(); // => ["opt1", "arg1", "arg2"]
    

    Ideally, at least to the best of my knowledge, the arguments to a "multiple" option should be consumed and removed from the list returned by getArguments() so only ["arg1", "arg2"] would be returned in the above example.

    Bug 
    opened by lillesvin 4
  • The supported PHP version is not clear

    The supported PHP version is not clear

    The supported PHP versions are not clear or consistent.

    opened by Sweetchuck 0
  • Support for PHP 8.1

    Support for PHP 8.1

    PHP Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /.../corneltek/getoptionkit/src/Option.php on line 130

    https://github.com/phpbrew/phpbrew/issues/1245

    opened by Sweetchuck 0
  • Please use a common GetOptionKit\Exception\Exception base class for all GetOptionKit exceptions.

    Please use a common GetOptionKit\Exception\Exception base class for all GetOptionKit exceptions.

    That makes catching and handling parse exceptions a lot easier.

    opened by cmanley 1
  • Cannot install with composer: circular deprecation error

    Cannot install with composer: circular deprecation error

    Firstly, after skipping over dozen of alternatives, i was very happy to find this project - seems like what i was looking for!

    Unfortunately, i could not find a way to install it with composer: composer require c9s/GetOptionKit

    exits with the following error:

    Package c9s/GetOptionKit is abandoned, you should avoid using it. Use corneltek/getoptionkit instead.

    But trying to do that and typing

    composer require corneltek/getoptionkit

    results in the same message. Moreover, it seems like there is no "getoptionkit" repo under https://github.com/corneltek/

    So, i'd like to know whether is this project considered as completely abandoned and so the suggestion would be to not use it in new projects? Or are there any plans to publish a replacement for it or a successor?

    opened by arcijsg 0
  • Support Option value prefilter and valiadator.

    Support Option value prefilter and valiadator.

    seems implemented? needs to be confirmed.

    opened by c9s 0
  • Changelog

    Changelog

    Hi. I just tried to update from 2.0.3 to 2.0.9 and noticed, that you removed a public method:

    Call to undefined method GetOptionKit\OptionCollection::printOptions()

    Could you please maintain a changelog for breaking changes like this?) I see, that new behavior is reflected in example.php, and i like the new OptionPrinter, but it would be nice to have a brief changelog, at least a one line at release comment)

    opened by dlussky 14
  • Markdown printer support

    Markdown printer support

    Printer 
    opened by c9s 0
  • Manpage printer support

    Manpage printer support

    Printer 
    opened by c9s 0
  • Move Result Container to Context class

    Move Result Container to Context class

    This is to separate the parsed option value and option spec

    opened by c9s 0
Releases(2.6.1)
Owner
Yo-An Lin
a developer who really loves crafting tools
Yo-An Lin
Termage provides a fluent and incredibly powerful object-oriented interface for customizing CLI output text color, background, formatting, theming and more.

Termage provides a fluent and incredibly powerful object-oriented interface for customizing CLI output text color, background, formatting, theming and

TERMAGE 63 Jan 11, 2022
Another Command Line Argument Parser

Optparse — Another Command Line Argument Parser Install 1. Get composer. 2. Put this into your local composer.json: { "require": { "chh/optparse

Christoph Hochstrasser 18 Nov 1, 2019
[ABANDONED] PHP library for executing commands on multiple remote machines, via SSH

#Shunt Inspired by Ruby's Capistrano, Shunt is PHP library for executing commands on multiple remote machines, via SSH. Specifically, this library was

The League of Extraordinary Packages 435 Nov 8, 2021
PHP library for executing commands on multiple remote machines, via SSH

#Shunt Inspired by Ruby's Capistrano, Shunt is PHP library for executing commands on multiple remote machines, via SSH. Specifically, this library was

The League of Extraordinary Packages 435 Nov 8, 2021
PHP CLI tool which allows publishing zipped MODX extra to modstore.pro marketplace

MODX Extra Publisher PHP CLI tool which allows publishing zipped MODX extra to modstore.pro marketplace. Installation global? local? To install packag

Ivan Klimchuk 3 Aug 6, 2021
unofficial cli built using php which can be used to upload and download files from anonfiles.com

Anonfiles CLI Table of Contents Introduction Features Screenshots Installation Contributing License Introduction Anon Files CLI can upload and downloa

Albin Varghese 4 Jan 1, 2022
An Elegant CLI Library for PHP

Commando An Elegant PHP CLI Library Commando is a PHP command line interface library that beautifies and simplifies writing PHP scripts intended for c

Nate Good 788 Jan 7, 2022
A PHP library for command-line argument processing

GetOpt.PHP GetOpt.PHP is a library for command-line argument processing. It supports PHP version 5.4 and above. Releases For an overview of the releas

null 312 Jan 4, 2022
Generic PHP command line flags parse library

PHP Flag Generic PHP command line flags parse library Features Generic CLI options and arguments parser. Support set value data type(int,string,bool,a

PHP Toolkit 13 Jan 10, 2022
A PHP library for command-line argument processing

GetOpt.PHP GetOpt.PHP is a library for command-line argument processing. It supports PHP version 7.1 and above. Releases For an overview of the releas

null 312 Jan 4, 2022
The Hoa\Console library.

Hoa is a modular, extensible and structured set of PHP libraries. Moreover, Hoa aims at being a bridge between industrial and research worlds. Hoa\Con

Hoa 364 Jan 7, 2022
Library for creating CLI commands or applications

Console Motivation: this library purpose is to provide a lighter and more robust API for console commands and/or applications to symfony/console. It c

Théo FIDRY 4 Oct 20, 2021
Simple but yet powerful library for running almost all artisan commands.

:artisan gui Simple but yet powerful library for running some artisan commands. Requirements Laravel 8.* php ^7.3 Installation Just install package: c

null 286 Jan 7, 2022
PHP Interminal is a command-line tool that gives you access to PHP Internals discussions in your terminal.

PHP Interminal is a command-line tool that gives you access to PHP Internals discussions in your terminal. ??

Nuno Maduro 32 Dec 12, 2021
PHP Reverse Shell > reverse-shell.php

PHP Reverse Shell > reverse-shell.php PHP Cmd Shell > cmd.php JPG cmd Shell > cmd.jpg /etc/passwd Pulling Shell > etc-passwd.php Configuration Pulling

Dark-Network 3 Oct 29, 2021
A REPL for PHP

PsySH PsySH is a runtime developer console, interactive debugger and REPL for PHP. Learn more at psysh.org and in the manual. PsySH manual ?? Installa

Justin Hileman 9.2k Jan 16, 2022
CRON for PHP: Calculate the next or previous run date and determine if a CRON expression is due

PHP Cron Expression Parser NOTE This fork has been deprecated and development moved to https://github.com/dragonmantank/cron-expression. More informat

Michael Dowling 4.9k Jan 11, 2022
A tiny REPL for PHP

Boris A tiny, but robust REPL for PHP. Announcement: I'm looking to add one or two additional collaborators with commit access. If you are actively in

null 2.2k Jan 6, 2022