getopt - コマンドオプションを解析する(機能強化)

SYNOPSIS

      getopt optstring parameters
      getopt [options] [--] optstring parameters
      getopt [options] -o|--options optstring [options] [--] parameters

DESCRIPTION

getoptは、コマンドラインのオプションを分割(パース)して、シェル手続きで簡単にパースできるようにしたり、有効なオプションかどうかをチェックしたりするのに使われる。 そのためにGNU getopt(3)ルーチンを使用する。

getopt が呼び出されるパラメータは 2 つの部分に分けられる。 getopt がパースする方法を変更するオプション (SYNOPSIS 内のオプションと optstring) と、パースされるパラメータ (SYNOPSIS 内のパラメータ) である。 2番目の部分は、オプション引数ではない最初のパラメータ、 または '--' が最初に出現した後のパラメータから始まる。 最初の部分で'-o'または'--options'オプションが見つからない場合、 2番目の部分の最初のパラメーターが短いオプション文字列として使われる。

環境変数 GETOPT_COMPATIBLE が設定されている場合、 または最初のパラメータがオプションでない場合 (SYNOPSIS の最初の書式である '-' で始まらない場合)、 getopt は他のバージョンの getopt(1) と互換性のある出力を生成する。 パラメータシャッフルを行い、オプション引数を認識する (詳細については COMPATIBILITY を参照のこと)。

従来の getopt(1) の実装は、引数やオプション以外のパラメータに含まれる空白文字やその他の (シェル固有の) 特殊文字に対処できない。 この問題を解決するために、この実装では引用符で囲まれた出力を生成することができる。 これはこれらの文字を保持する効果があるが、他のバージョンと互換性のない方法(SYNOPSISの2番目か3番目の形式)でgetoptを呼び出さなければならない。 この拡張バージョンの getopt(1) がインストールされているかどうかを調べるには、特別なテストオプション (-T) を使用する。

OPTIONS

-a, --alternative
長いオプションは'-'で始まるようにする。
-h, --help
ヘルプテキストを表示して終了する。 それ以外の出力は生成されない。
-l, --longoptions longopts
認識する長い(複数文字の)オプション。 カンマで区切ることで、一度に複数のオプション名を指定できる。 このオプションは複数回指定でき、longoptsは累積される。 longopts の各長いオプション名の後には、必須引数を持つことを示すコロン 1 つと、 オプション引数を持つことを示すコロン 2 つを続けることができる。
-n, --name progname
getopt(3) ルーチンがエラーを報告するときに使われる名前。 getopt(1) のエラーは getopt から来たものとして報告されることに注意。
-o, --options shortopts
認識される短い(1文字の)オプション。 このオプションが見つからない場合、 getopt の最初のパラメータのうち、 '-' で始まらないもの(オプション引数でないもの)が、 短いオプション文字列として使われる。 shortopts の各ショートオプション文字の後には、必須引数であることを示すコロンを 1 つ、 オプション引数であることを示すコロンを 2 つ続けることができる。 shortoptsの最初の文字を'+'または'-'にすることで、オプションの解析方法と出力の生成方法に影響を与えることができる(詳細は#SCANNING MODESセクションを参照)。
-q, --quiet
getopt(3)によるエラー報告を無効化。
-Q, --quiet-output
通常の出力を生成しない。 -q も使用しない限り、エラーは getopt'(3) によって報告される。
-s, --shell shell
シェルの引用規約を設定する。 -sオプションが与えられない場合は、BASHの規約が使用される。 現在有効な引数は 'sh'、'bash'、'csh'、'tcsh' である。
-T, --test
getopt(1) がこの拡張バージョンか古いバージョンかをテストする。 これは出力を生成せず、エラーステータスを 4 に設定する。 getopt(1) の他の実装、および環境変数 GETOPT_COMPATIBLE が設定されている場合のこのバージョンは、 '--' とエラーステータス 0 を返す。
-u, --unquoted
出力を引用符で囲まない。 このモードでは (他の getopt(1) の実装でそうであるように) 空白文字や特殊な (シェル依存の) 文字が大混乱を引き起こす可能性があることに注意。
-V, --version
バージョン情報を表示して終了する。 他の出力は生成されない。

PARSING

      This  section specifies the format of the second part of the parameters of getopt (the parameters in
      the SYNOPSIS).  The next section (OUTPUT) describes the output that is generated.  These  parameters
      were typically the parameters a shell function was called with.  Care must be taken that each param‐
      eter the shell function was called with corresponds to exactly one parameter in the  parameter  list
      of getopt (see the EXAMPLES).  All parsing is done by the GNU getopt(3) routines.
      The  parameters  are  parsed  from left to right.  Each parameter is classified as a short option, a
      long option, an argument to an option, or a non-option parameter.
      A simple short option is a '-' followed by a short option character.  If the option has  a  required
      argument, it may be written directly after the option character or as the next parameter (i.e., sep‐
      arated by whitespace on the command line).  If the option has an optional argument, it must be writ‐
      ten directly after the option character if present.
      It  is  possible to specify several short options after one '-', as long as all (except possibly the
      last) do not have required or optional arguments.
      A long option normally begins with '--' followed by the long option name.  If the option has  a  re‐
      quired  argument, it may be written directly after the long option name, separated by '=', or as the
      next argument (i.e., separated by whitespace on the command line).  If the option  has  an  optional
      argument,  it  must be written directly after the long option name, separated by '=', if present (if
      you add the '=' but nothing behind it, it is interpreted as if no argument was present;  this  is  a
      slight  bug, see the BUGS).  Long options may be abbreviated, as long as the abbreviation is not am‐
      biguous.
      Each parameter not starting with a '-', and not a required argument  of  a  previous  option,  is  a
      non-option  parameter.   Each parameter after a '--' parameter is always interpreted as a non-option
      parameter.  If the environment variable POSIXLY_CORRECT is  set,  or  if  the  short  option  string
      started with a '+', all remaining parameters are interpreted as non-option parameters as soon as the
      first non-option parameter is found.

OUTPUT

      Output is generated for each element described in the previous section.  Output is done in the  same
      order  as  the elements are specified in the input, except for non-option parameters.  Output can be
      done in compatible (unquoted) mode, or in such way that  whitespace  and  other  special  characters
      within  arguments  and  non-option  parameters are preserved (see QUOTING).  When the output is pro‐
      cessed in the shell script, it will seem to be composed of distinct elements that can  be  processed
      one  by  one  (by  using  the shift command in most shell languages).  This is imperfect in unquoted
      mode, as elements can be split at unexpected places if they contain whitespace  or  special  charac‐
      ters.
      If  there  are problems parsing the parameters, for example because a required argument is not found
      or an option is not recognized, an error will be reported on stderr, there will be no output for the
      offending element, and a non-zero error status is returned.
      For  a  short  option, a single '-' and the option character are generated as one parameter.  If the
      option has an argument, the next parameter will be the argument.  If the option  takes  an  optional
      argument, but none was found, the next parameter will be generated but be empty in quoting mode, but
      no second parameter will be generated in unquoted (compatible) mode.  Note that many other getopt(1)
      implementations do not support optional arguments.
      If  several short options were specified after a single '-', each will be present in the output as a
      separate parameter.
      For a long option, '--' and the full option name are generated as one parameter.  This is  done  re‐
      gardless  whether the option was abbreviated or specified with a single '-' in the input.  Arguments
      are handled as with short options.
      Normally, no non-option parameters output is generated until all options and  their  arguments  have
      been  generated.   Then '--' is generated as a single parameter, and after it the non-option parame‐
      ters in the order they were found, each as a separate parameter.  Only if the first character of the
      short options string was a '-', non-option parameter output is generated at the place they are found
      in the input (this is not supported if the first format of the SYNOPSIS is used; in  that  case  all
      preceding occurrences of '-' and '+' are ignored).

QUOTING

      In compatible mode, whitespace or 'special' characters in arguments or non-option parameters are not
      handled correctly.  As the output is fed to the shell script, the script does not  know  how  it  is
      supposed to break the output into separate parameters.  To circumvent this problem, this implementa‐
      tion offers quoting.  The idea is that output is generated with quotes around each parameter.   When
      this  output is once again fed to the shell (usually by a shell eval command), it is split correctly
      into separate parameters.
      Quoting is not enabled if the environment variable GETOPT_COMPATIBLE is set, if the  first  form  of
      the SYNOPSIS is used, or if the option '-u' is found.
      Different shells use different quoting conventions.  You can use the '-s' option to select the shell
      you are using.  The following shells are currently supported: 'sh', 'bash', 'csh' and 'tcsh'.  Actu‐
      ally, only two 'flavors' are distinguished: sh-like quoting conventions and csh-like quoting conven‐
      tions.  Chances are that if you use another shell script language, one of these flavors can still be
      used.

SCANNING MODES

      The first character of the short options string may be a '-' or a '+' to indicate a special scanning
      mode.  If the first calling form in the SYNOPSIS is used they are ignored; the environment  variable
      POSIXLY_CORRECT is still examined, though.
      If  the first character is '+', or if the environment variable POSIXLY_CORRECT is set, parsing stops
      as soon as the first non-option parameter (i.e., a parameter that does not  start  with  a  '-')  is
      found  that  is  not an option argument.  The remaining parameters are all interpreted as non-option
      parameters.
      If the first character is a '-', non-option parameters are outputted at the  place  where  they  are
      found;  in  normal operation, they are all collected at the end of output after a '--' parameter has
      been generated.  Note that this '--' parameter is still generated, but it will always  be  the  last
      parameter in this mode.

COMPATIBILITY

      This version of getopt(1) is written to be as compatible as possible to other versions.  Usually you
      can just replace them with this version without any modifications, and with some advantages.
      If the first character of the first parameter of getopt is not a '-', getopt goes into compatibility
      mode.  It will interpret its first parameter as the string of short options, and all other arguments
      will be parsed.  It will still do parameter shuffling (i.e., all non-option parameters are output at
      the end), unless the environment variable POSIXLY_CORRECT is set.
      The environment variable GETOPT_COMPATIBLE forces getopt into compatibility mode.  Setting both this
      environment variable and POSIXLY_CORRECT offers 100% compatibility for 'difficult'  programs.   Usu‐
      ally, though, neither is needed.
      In compatibility mode, leading '-' and '+' characters in the short options string are ignored.

RETURN CODES

      getopt  returns error code 0 for successful parsing, 1 if getopt(3) returns errors, 2 if it does not
      understand its own parameters, 3 if an internal error occurs like out-of-memory,  and  4  if  it  is
      called with -T.

EXAMPLES

      Example  scripts  for  (ba)sh  and  (t)csh are provided with the getopt(1) distribution, and are in‐
      stalled in /usr/share/doc/util-linux/getopt/ directory.

ENVIRONMENT

      POSIXLY_CORRECT
             This environment variable is examined by the getopt(3) routines.  If it is set, parsing stops
             as  soon  as a parameter is found that is not an option or an option argument.  All remaining
             parameters are also interpreted as non-option parameters, regardless whether they start  with
             a '-'.
      GETOPT_COMPATIBLE
             Forces getopt to use the first calling format as specified in the SYNOPSIS.

BUGS

      getopt(3)  can  parse long options with optional arguments that are given an empty optional argument
      (but cannot do this for short options).  This getopt(1) treats optional arguments that are empty  as
      if they were not present.
      The  syntax  if you do not want any short option variables at all is not very intuitive (you have to
      set them explicitly to the empty string).

AUTHOR

      Frodo Looijaard ⟨frodo@frodo.looijaard.name⟩

SEE ALSO

      bash(1), tcsh(1), getopt(3)

AVAILABILITY

      The getopt command is part of the util-linux package and is  available  from  Linux  Kernel  Archive
      ⟨https://www.kernel.org/pub/linux/utils/util-linux/⟩.

External link