I used the getopt parser for this, supported short, long option names, type restrictions, print options, etc., and this library has been supported for over 6 years.
Description:
<?php use GetOptionKit\OptionCollection; use GetOptionKit\OptionParser; $options = new OptionCollection; $spec = $options->add( 'f|foo:' , 'option require value' );
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.
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 )
GetOptionKit is on GitHub: https://github.com/c9s/GetOptionKit
c9s
source share