Hướng dẫn dùng man getopt trong PHP
(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8) getopt — Gets options from the command line argument list Descriptiongetopt(string Parametersshort_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 "x" recognizes an option -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 "opt" recognizes an option --opt . rest_index If the rest_index parameter is present, then the index where argument parsing stopped will be written to this variable. The
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.
The
Return Values This function will return an array of option / argument pairs, or
Changelog
ExamplesExample #1 getopt() example: The basics
shell> php example.php -fvalue -h The above example will output: array(2) { ["f"]=> string(5) "value" ["h"]=> bool(false) } Example #2 getopt() example: Introducing long options
shell> php example.php -f "value for f" -v -a --required value --optional="optional value" --option The above example will output: 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) } Example #3 getopt() example: Passing multiple options as one
shell> php example.php -aaac The above example will output: array(2) { ["a"]=> array(3) { [0]=> bool(false) [1]=> bool(false) [2]=> bool(false) } ["c"]=> bool(false) } Example #4 getopt() example: Using
shell> php example.php -a 1 -b 2 -- test The above example will output: array(1) { [0]=> string(4) "test" } Sane Guy ¶ 3 years ago
chris at tiny dot net ¶ 18 years ago
ch2902 ¶ 9 years ago
$opts as $opt) Matt ¶ 4 years ago
Anonymous ¶ 11 years ago
housni dot yakoob at NOSPAM dot gmail dot com ¶ 7 years ago
uberlinuxguy at tulg dot org ¶ 14 years ago
Francois Hill ¶ 14 years ago
joey at alegria dot co dot jp ¶ 16 years ago
mbirth at webwriters dot de ¶ 14 years ago
mpartap at gmx dot net ¶ 11 years ago
taka8aru at gmail dot com ¶ 2 years ago
Damien B. ¶ 14 years ago
= getopt('hs:');// Handle command line arguments koenbollen at gnospamail dot com ¶ 15 years ago
takingsides at gmail dot com ¶ 8 years ago
= getopt('hl::m:v:a', [ geoff at gosquared dot com ¶ 12 years ago
Anonymous ¶ 15 years ago
S2 ¶ 1 month ago
Dominik Deobald ¶ 7 months ago
gingko at gingko dot ovh ¶ 1 year ago
edg at Greenberg dot org ¶ 1 year ago
micropresident at gmail dot com ¶ 6 years ago
rastyazhenko dot anton at gmail dot com ¶ 2 years ago
salimzadehsajad at gmail dot com ¶ 5 years ago
wouter dot berben at phpro dot be ¶ 5 years ago
|