Edge Cases of getopt
suggest changeThis example shows the behaviour of getopt when the user input is uncommon:
getopt.php
var_dump(
getopt("ab:c::", ["delta", "epsilon:", "zeta::"])
);
Shell command line
$ php getopt.php -a -a -bbeta -b beta -cgamma --delta --epsilon --zeta --zeta=f -c gamma
array(6) {
["a"]=>
array(2) {
[0]=>
bool(false)
[1]=>
bool(false)
}
["b"]=>
array(2) {
[0]=>
string(4) "beta"
[1]=>
string(4) "beta"
}
["c"]=>
array(2) {
[0]=>
string(5) "gamma"
[1]=>
bool(false)
}
["delta"]=>
bool(false)
["epsilon"]=>
string(6) "--zeta"
["zeta"]=>
string(1) "f"
}
From this example, it can be seen that:
- An option with no argument is
falsewhen it is present. - If an option is repeated,
getoptreturns an array of its values. - A required argument (one colon) may be attached (
-bbeta,--zeta=f) or given as the next token (-b beta). - An optional argument (two colons) is taken only when it is attached. The separate word
gammaafter the second-cis not used, so that-cisfalse. --epsilonrequires a value, so the next token,--zeta, is stored as that value. It is not parsed as thezetaoption. The later--zeta=fis thezetaoption.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents