PHP Classes

Mini PHP Getopt: Extract command line parameter values using getopt

Recommend this page to a friend!
  Info   View files Example   View files View files (17)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 33 All time: 10,970 This week: 571Up
Version License PHP version Categories
mini-getopt 1.0Custom (specified...5PHP 5, Console
Description 

Author

This package can be used to extract command line parameter values using getopt.

It can take as parameter a list of options that the current script is accepting to take as command line parameter values.

The package can use the getopt extension to extract the values of parameters passed via the command line.

Picture of jawira
  Performance   Level  
Name: jawira <contact>
Classes: 11 packages by
Country: Belgium Belgium
Age: ???
All time rank: 302416 in Belgium Belgium
Week rank: 411 Up3 in Belgium Belgium Up
Innovation award
Innovation award
Nominee: 9x

Winner: 2x

Example

<?php /** @noinspection PhpUnhandledExceptionInspection */

require __DIR__ . '/../vendor/autoload.php';

use
Jawira\MiniGetopt\MiniGetopt;

// Setup
$mg = new 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');

// Calling getopt function
echo var_export($mg->getopt(), true) . PHP_EOL;


Details

Mini Getopt (a getopt wrapper)

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

<dl>

<dt><a href="https://packagist.org/packages/jawira/emoji-catalog">jawira/emoji-catalog</a> (library)</dt> <dd>Get access to +3000 emojis as class constants.</dd>

<dt><a href="https://packagist.org/packages/jawira/plantuml">jawira/plantuml</a> (library)</dt> <dd>Provides PlantUML integration: plantuml executable and plantuml.jar</dd>

<dt><a href="https://packagist.org/packages/jawira/plantuml-encoding">jawira/plantuml-encoding</a> (library)</dt> <dd>PlantUML encoding functions.</dd>

<dt><a href="https://packagist.org/packages/jawira/">more...</a></dt> </dl>

[Github]: https://github.com/jawira/mini-getopt


  Files folder image Files  
File Role Description
Files folder image.idea (1 file, 1 directory)
Files folder imageresources (1 file)
Files folder imagesrc (7 files)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file build.xml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
Accessible without login Plain text file LICENSE.md Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  .idea  
File Role Description
Files folder imagedictionaries (1 file)
  Accessible without login Plain text file phing.xml Data Auxiliary data

  Files folder image Files  /  .idea  /  dictionaries  
File Role Description
  Accessible without login Plain text file jawira.xml Data Auxiliary data

  Files folder image Files  /  resources  
File Role Description
  Accessible without login Plain text file example.php Example Example script

  Files folder image Files  /  src  
File Role Description
  Plain text file MiniGetopt.php Class Class source
  Plain text file MiniGetoptException.php Class Class source
  Plain text file NoValue.php Class Class source
  Plain text file OptionalValue.php Class Class source
  Plain text file RequiredValue.php Class Class source
  Plain text file Validator.php Class Class source
  Plain text file Value.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:33
This week:0
All time:10,970
This week:571Up