jawira/mini-getopt

Very simple wrapper for getopt() function

v1.0.0 2019-10-10 20:09 UTC

This package is auto-updated.

Last update: 2020-06-15 22:19:32 UTC


README

Very simple wrapper for getopt() function.

Latest Stable Version Total Downloads Monthly Downloads License composer.lock PHPPackages Referenced By Average time to resolve an issue Percentage of issues still open PDS Skeleton Issues

Usage

This is only a wrapper, therefore the output from mini-getopt is going to be the same as getopt() function.

  1. First you have to configure options you want to use. To do so use the following methods:

    • \Jawira\MiniGetopt\MiniGetopt::addRequired
    • \Jawira\MiniGetopt\MiniGetopt::addOptional
    • \Jawira\MiniGetopt\MiniGetopt::addNoValue
  2. To retrieve values you have to call one of the following method:

    • \Jawira\MiniGetopt\MiniGetopt::getopt returns the same as getopt()
    • \Jawira\MiniGetopt\MiniGetopt::getOption to get only one value

Examples

PHP code:

// Preparing options
$mg = new \Jawira\MiniGetopt\MiniGetopt();
$mg->addRequired('f', 'format');    // value is required
$mg->addOptional('r', 'retry');     // value is optional
$mg->addOptional('q', '');          // only short option
$mg->addNoValue('v', 'verbose');    // no value
$mg->addNoValue('', 'version');     // only long option

// Calling getopt
var_export($mg->getopt());

Executing code:

$ php resources/example.php

array (
)
$ php resources/example.php -f=xml

array (
   'f' => 'xml',
)
$ php resources/example.php --format=xml -r -v

array (
  'format' => 'xml',
  'r' => false,
  'v' => false,
)
$ php resources/example.php -f=json -r=yes -v

array (
    'f' => 'json',
    'r' => 'yes',
    'v' => false,
)
$ php resources/example.php --retry -vvv

array (
  'retry' => false,
  'v' => 
  array (
    0 => false,
    1 => false,
    2 => false,
  ),
)
$ php resources/example.php --version=banana --invalid

array (
  'version' => false,
)

How to install

$ composer install jawira/mini-getopt

Contributing

If you liked this project, ⭐ star it on GitHub.

License

This library is licensed under the MIT license.

Packages from jawira

jawira/emoji-catalog (library)
Get access to +3000 emojis as class constants.
jawira/plantuml (library)
Provides PlantUML integration: plantuml executable and plantuml.jar
jawira/plantuml-encoding (library)
PlantUML encoding functions.
more...