= 4.3.0, PHP 5, PHP 7, PHP 8)getopt — 從命令行參數(shù)列表中獲取選項(xiàng)說(shuō)明getopt(string $short_options, array $long_options = [], int &$rest_index = null): a">

getopt

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

getopt從命令行參數(shù)列表中獲取選項(xiàng)

說(shuō)明

getopt(string $short_options, array $long_options = [], int &$rest_index = null): array|false

解析傳入腳本的選項(xiàng)。

參數(shù)

short_options
該字符串中的每個(gè)字符會(huì)被當(dāng)做選項(xiàng)字符,匹配傳入腳本的選項(xiàng)以單個(gè)連字符 (-) 開頭。 比如,一個(gè)選項(xiàng)字符串 "x" 識(shí)別了一個(gè)選項(xiàng) -x。 只允許 a-z、A-Z 和 0-9。
long_options
選項(xiàng)數(shù)組。此數(shù)組中的每個(gè)元素會(huì)被作為選項(xiàng)字符串,匹配了以兩個(gè)連字符 (--) 傳入到腳本的選項(xiàng)。 例如,長(zhǎng)選項(xiàng)元素 "opt" 識(shí)別了一個(gè)選項(xiàng) --opt。
rest_index
如果傳遞了 rest_index 參數(shù),那么參數(shù)解析停止時(shí)的索引,將被賦值給此變量。

short_options 可能包含了以下元素:

  • 單獨(dú)的字符(不接受值)
  • 后面跟隨冒號(hào)的字符(此選項(xiàng)需要值)
  • 后面跟隨兩個(gè)冒號(hào)的字符(此選項(xiàng)的值可選)
選項(xiàng)的值是字符串后的第一個(gè)參數(shù)。如果需要一個(gè)值,它不介意值之前是否有前置的空格,參見以下內(nèi)容。

注意: 選項(xiàng)的值不接受空格(" ")作為分隔符。

long_options 數(shù)組可能包含了以下元素:

  • 字符串(參數(shù)不接受任何值)
  • 后面跟隨冒號(hào)的字符串(此選項(xiàng)需要值)
  • 后面跟隨兩個(gè)冒號(hào)的字符串(此選項(xiàng)的值可選)

注意:

short_optionslong_options 的格式幾乎是一樣的,唯一的不同之處是 long_options 需要是選項(xiàng)的數(shù)組(每個(gè)元素為一個(gè)選項(xiàng)),而 short_options 需要一個(gè)字符串(每個(gè)字符是個(gè)選項(xiàng))。

返回值

此函數(shù)會(huì)返回選項(xiàng)/參數(shù)對(duì), 或者在失敗時(shí)返回 false。

注意:

選項(xiàng)的解析會(huì)終止于找到的第一個(gè)非選項(xiàng),之后的任何東西都會(huì)被丟棄。

更新日志

版本 說(shuō)明
7.1.0 添加 rest_index 參數(shù)。

范例

示例 #1 getopt() 例子:基本用法

<?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 -fvalue -h

以上例程會(huì)輸出:

array(2) {
  ["f"]=>
  string(5) "value"
  ["h"]=>
  bool(false)
}

示例 #2 getopt() 例子:引入長(zhǎng)選項(xiàng)

<?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

以上例程會(huì)輸出:

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)
}

示例 #3 getopt() 例子:傳遞同一多個(gè)選項(xiàng)

<?php
// Script example.php
$options getopt("abc");
var_dump($options);
?>
shell> php example.php -aaac

以上例程會(huì)輸出:

array(2) {
  ["a"]=>
  array(3) {
    [0]=>
    bool(false)
    [1]=>
    bool(false)
    [2]=>
    bool(false)
  }
  ["c"]=>
  bool(false)
}

示例 #4 getopt() 例子:使用 rest_index

<?php
// Script example.php
$optind null;
$opts getopt('a:b:', [], $optind);
$pos_args array_slice($argv$optind);
var_dump($pos_args);
shell> php example.php -a 1 -b 2 -- test

以上例程會(huì)輸出:

array(1) {
  [0]=>
  string(4) "test"
}

參見