PHP.nl

getopt

getopt

Gets options from the command line argument list

 **getopt** string $short_options array $long_options int $rest_index

Parses options passed to the script.

short_options Each character in this string will be used as option characters and matched against options passed to the script starting with a single hyphen (). -

   For example, an option string  recognizes an
   option .
  `"x"``-x`

Only a-z, A-Z and 0-9 are allowed.

long_options An array of options. Each element in this array will be used as option strings and matched against options passed to the script starting with two hyphens (). --

   For example, an longopts element  recognizes an
   option .
  `"opt"``--opt`

rest_index If the parameter is present, then the index where argument parsing stopped will be written to this variable. rest_index

The parameter may contain the following elements:

Option values are the first argument after the string. If a value is required, it does not matter whether the value has leading white space or not. See note.

short_options> Opmerking: > Optional values do not accept (space) as a separator.

`" "`
The  array values may contain:

long_options

Opmerking: > The format for the and is almost the same, the only difference is that takes an array of options (where each element is the option) whereas takes a string (where each character is the option). short_options``long_options``long_options``short_options

This function will return an array of option / argument pairs, return.falseforfailure.

Opmerking: > The parsing of options will end at the first non-option found, anything that follows is discarded.

Voorbeeld: example: The basics

<?php
// Script example.php
$options = getopt("f:hp:");
var_dump($options);
?>
shell> php example.php -fvalue -h
array(2) {
  ["f"]=>
  string(5) "value"
  ["h"]=>
  bool(false)
}

Voorbeeld: example: Introducing long options

<?php
// Script example.php
$shortopts  = "";
$shortopts .= "f:";  // Required value
$shortopts .= "v::"; // Optional value
$shortopts .= "abc"; // These options do not accept values

$longopts  = array(
    "required:",     // Required value
    "optional::",    // Optional value
    "option",        // No value
    "opt",           // No value
);
$options = getopt($shortopts, $longopts);
var_dump($options);
?>
shell> php example.php -f "value for f" -v -a --required value --optional="optional value" --option
array(6) {
  ["f"]=>
  string(11) "value for f"
  ["v"]=>
  bool(false)
  ["a"]=>
  bool(false)
  ["required"]=>
  string(5) "value"
  ["optional"]=>
  string(14) "optional value"
  ["option"]=>
  bool(false)
}

Voorbeeld: example: Passing multiple options as one

<?php
// Script example.php
$options = getopt("abc");
var_dump($options);
?>
shell> php example.php -aaac
array(2) {
  ["a"]=>
  array(3) {
    [0]=>
    bool(false)
    [1]=>
    bool(false)
    [2]=>
    bool(false)
  }
  ["c"]=>
  bool(false)
}

**Voorbeeld: example: Using **

<?php
// Script example.php
$rest_index = null;
$opts = getopt('a:b:', [], $rest_index);
$pos_args = array_slice($argv, $rest_index);
var_dump($pos_args);
shell> php example.php -a 1 -b 2 -- test
array(1) {
  [0]=>
  string(4) "test"
}

$argv