PHP code example of jawira / mini-getopt

1. Go to this page and download the library: Download jawira/mini-getopt library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jawira / mini-getopt example snippets


// resources/example.php
// Preparing options
$mg = new \Jawira\MiniGetopt\MiniGetopt();
$mg->addRequired('f', 'format');    // value is verbose');    // no value
$mg->addNoValue('', 'version');     // only long option

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

// Setup
$mg = new \Jawira\MiniGetopt\MiniGetopt();
$mg->addRequired('f', 'format');
$mg->addNoValue('v', 'verbose');

// Calling getopt function with `optind` parameter
$optind = null;
$options = $mg->getopt($optind);
echo "optind: $optind" . PHP_EOL;

$mg = new \Jawira\MiniGetopt\MiniGetopt();
$mg->addRequired('f', 'format', 'Format to export', 'png|gif|svg');
$mg->addOptional('r', 'retry', 'Retry on error', 'count');
$mg->addOptional('q', '', 'Quiet mode', 'yes|no');
$mg->addNoValue('v', 'verbose', 'Display verbose messages');
$mg->addNoValue('', 'version', 'Show version');
echo $mg->doc();
console
$ php resources/example.php

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

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

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

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

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

array (
  'version' => false,
)
console
$ php resources/example.php --format=pdf -vv
optind: 3