PHP code example of fostam / getopts

1. Go to this page and download the library: Download fostam/getopts 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/ */

    

fostam / getopts example snippets




include "vendor/autoload.php";

$getopts = new Fostam\GetOpts\Handler();

$getopts->addOption('verboseLevel')
    ->short('v')
    ->long('verbose')
    ->description('increase verbosity')
    ->incrementable();

$getopts->addArgument('outputFile')
    ->name('output-file')
    ->

$option = $handler->addOption('inputFile');

$option->short('f');

$option->long('filename');

$option->description('input file with data to be processed');

$option->short('f')->argument('input-file');

$option->short('f')->argument('input-file')->multiple();

array(2) {
  [0]=>
  string(9) "file1.txt"
  [1]=>
  string(9) "file2.txt"
}

$handler->addOption('verboseLevel')->short('v')->incrementable();

$handler->addOption('verboseLevel')->short('v')->incrementable(5);

$option->

$handler->addOption('inputFile')->short('f')->defaultValue('file1.txt');
$handler->addOption('maxValue')->short('m')->defaultValue(5);

$option->long('test')->negatable();

$option->validator(function($value) {
    if ($value < 0 || $value > 50) {
        return false;
    }
    return true;
});

$option = $handler->addArgument('outputFile');

$arg->name('output-file');

$arg->name('output-file')->multiple();

$arg->name('output-file')->defaultValue('file.txt');

$arg->name('output-file')->

$arg->validator(function($value) {
    if ($value < 0 || $value > 50) {
        return false;
    }
    return true;
});

$result = $handler->get();

$verbosity = $handler->get('verbosity');

$optionArray = $handler->getOptions();

$argumentArray = $handler->getArguments();

$scriptName = $handler->getScriptName();  // would return "myscript.php"

$handler->parse([
    'myscript.php',
    '-v',
    '-f',
    'file.txt'
]);

$handler->disableErrorHandling();

$handler->getUsageString();

$handler->setExtraHelpText($txt);

$handler->enableTerseUsage();

$handler->setHelpOptionLong('help');
$handler->setHelpOptionShort('h');

$handler->getHelpText();

$> php test.php -vvv hello
array(2) {
  ["verboseLevel"]=>
  int(3)
  ["outputFile"]=>
  string(5) "hello"
}

$> php test.php -h
Usage: test.php [-v] OUTPUT-FILE
  -v    increase verbosity

$> php test.php
test.php: missing arguments
Usage: test.php [-v] OUTPUT-FILE
Try 'test.php --help' for more information

$> php myscript.php -v file.txt