/usr/bin/getopt: Difference between revisions

From Azupedia
Jump to navigation Jump to search
No edit summary
 
(14 intermediate revisions by the same user not shown)
Line 9: Line 9:


==DESCRIPTION==
==DESCRIPTION==
      getopt is  used  to break up (parse) options in command lines for easy parsing by shell procedures,
getoptは、コマンドラインのオプションを分割(パース)して、シェル手続きで簡単にパースできるようにしたり、有効なオプションかどうかをチェックしたりするのに使われる。 そのために'''[https://manpages.debian.org/bookworm/manpages-dev/getopt.3.en.html GNU getopt]'''(3)ルーチンを使用する。
      and to check for valid options. It uses the GNU getopt(3) routines to do this.


      The parameters getopt is called with can be divided into two parts: options  which  modify  the  way
getopt が呼び出されるパラメータは 2 つの部分に分けられる。 getopt がパースする方法を変更するオプション ('''SYNOPSIS''' 内のオプションと optstring) と、パースされるパラメータ ('''SYNOPSIS''' 内のパラメータ) である。 2番目の部分は、オプション引数ではない最初のパラメータ、 または '--' が最初に出現した後のパラメータから始まる。 最初の部分で'-o'または'--options'オプションが見つからない場合、 2番目の部分の最初のパラメーターが短いオプション文字列として使われる。
      getopt will do the parsing (the options and the optstring in the SYNOPSIS), and the parameters which
      are to be parsed (parameters in the SYNOPSIS).  The second part will start at the  first  non-option
      parameter  that  is  not  an  option argument, or after the first occurrence of '--'.  If no '-o' or
      '--options' option is found in the first part, the first parameter of the second part is used as the
      short options string.


      If  the  environment  variable  GETOPT_COMPATIBLE is set, or if the first parameter is not an option
環境変数 '''GETOPT_COMPATIBLE''' が設定されている場合、 または最初のパラメータがオプションでない場合 ('''SYNOPSIS''' の最初の書式である '-' で始まらない場合)getopt は他のバージョンの '''getopt'''(1) と互換性のある出力を生成する。 パラメータシャッフルを行い、オプション引数を認識する (詳細については '''COMPATIBILITY''' を参照のこと)
      (does not start with a '-', the first format in the SYNOPSIS), getopt will generate output  that  is
      compatible  with that of other versions of getopt(1).  It will still do parameter shuffling and rec‐
      ognize optional arguments (see section COMPATIBILITY for more information).


      Traditional implementations of getopt(1) are unable to cope with whitespace  and  other  (shell-spe‐
従来の '''getopt'''(1) の実装は、引数やオプション以外のパラメータに含まれる空白文字やその他の (シェル固有の) 特殊文字に対処できない。 この問題を解決するために、この実装では引用符で囲まれた出力を生成することができる。 これはこれらの文字を保持する効果があるが、他のバージョンと互換性のない方法('''SYNOPSIS'''の2番目か3番目の形式)でgetoptを呼び出さなければならない。 この拡張バージョンの getopt(1) がインストールされているかどうかを調べるには、特別なテストオプション (-T) を使用する。
      cific)  special  characters in arguments and non-option parameters.  To solve this problem, this im‐
      plementation can generate quoted output which must once again be interpreted by the  shell  (usually
      by  using  the eval command).  This has the effect of preserving those characters, but you must call
      getopt in a way that is no longer compatible with other versions (the second or third format in  the
      SYNOPSIS).  To determine whether this enhanced version of getopt(1) is installed, a special test op‐
      tion (-T) can be used.


== OPTIONS ==
== OPTIONS ==
      -a, --alternative
;-a, --alternative
              Allow long options to start with a single '-'.
:長いオプションは'-'で始まるようにする。


      -h, --help
;-h, --help
              Display help text and exit.  No other output is generated.
:ヘルプテキストを表示して終了する。 それ以外の出力は生成されない。


      -l, --longoptions longopts
;-l, --longoptions longopts
              The long (multi-character) options to be recognized.  More than one option name may be speci‐
:認識する長い(複数文字の)オプション。 カンマで区切ることで、一度に複数のオプション名を指定できる。 このオプションは複数回指定でき、longoptsは累積される。 longopts の各長いオプション名の後には、必須引数を持つことを示すコロン 1 つと、 オプション引数を持つことを示すコロン 2 つを続けることができる。
              fied  at once, by separating the names with commas.  This option may be given more than once,
              the longopts are cumulative.  Each long option name in longopts may be followed by one  colon
              to  indicate it has a required argument, and by two colons to indicate it has an optional ar‐
              gument.


      -n, --name progname
;-n, --name progname
              The name that will be used by the getopt(3) routines when it reports errors.  Note  that  er‐
:'''[https://manpages.debian.org/bookworm/manpages-dev/getopt.3.en.html getopt]'''(3) ルーチンがエラーを報告するときに使われる名前。 '''getopt'''(1) のエラーは getopt から来たものとして報告されることに注意。
              rors of getopt(1) are still reported as coming from getopt.


      -o, --options shortopts
;-o, --options shortopts
              The  short  (one-character) options to be recognized.  If this option is not found, the first
:認識される短い(1文字の)オプション。 このオプションが見つからない場合、 getopt の最初のパラメータのうち、 '-' で始まらないもの(オプション引数でないもの)が、 短いオプション文字列として使われる。 shortopts の各ショートオプション文字の後には、必須引数であることを示すコロンを 1 つ、 オプション引数であることを示すコロンを 2 つ続けることができる。 shortoptsの最初の文字を'+'または'-'にすることで、オプションの解析方法と出力の生成方法に影響を与えることができる(詳細は[[#SCANNING MODES]]セクションを参照)。
              parameter of getopt that does not start with a '-' (and is not an option argument) is used as
              the  short  options  string.  Each short option character in shortopts may be followed by one
              colon to indicate it has a required argument, and by two colons to indicate  it  has  an  op‐
              tional argument.  The first character of shortopts may be '+' or '-' to influence the way op‐
              tions are parsed and output is generated (see section SCANNING MODES for details).


      -q, --quiet
;-q, --quiet
              Disable error reporting by getopt(3).
:'''[https://manpages.debian.org/bookworm/manpages-dev/getopt.3.en.html getopt]'''(3)によるエラー報告を無効化。


      -Q, --quiet-output
;-Q, --quiet-output
              Do not generate normal output. Errors are still reported by getopt(3), unless you  also  use
:通常の出力を生成しない。 -q も使用しない限り、エラーは ''[https://manpages.debian.org/bookworm/manpages-dev/getopt.3.en.html getopt]'''(3) によって報告される。
              -q.


      -s, --shell shell
;-s, --shell shell
              Set  quoting  conventions to those of shell.  If the -s option is not given, the BASH conven‐
:シェルの引用規約を設定する。 -sオプションが与えられない場合は、'''BASH'''の規約が使用される。 現在有効な引数は 'sh''bash''csh''tcsh' である。
              tions are used.  Valid arguments are currently 'sh' 'bash', 'csh', and 'tcsh'.


      -T, --test
;-T, --test
              Test if your getopt(1) is this enhanced version or an old version.  This generates no output,
:'''getopt'''(1) がこの拡張バージョンか古いバージョンかをテストする。 これは出力を生成せず、エラーステータスを 4 に設定する。 '''getopt'''(1) の他の実装、および環境変数 '''GETOPT_COMPATIBLE''' が設定されている場合のこのバージョンは、 '--' とエラーステータス 0 を返す。
              and  sets the error status to 4.  Other implementations of getopt(1), and this version if the
              environment variable GETOPT_COMPATIBLE is set, will return '--' and error status 0.


      -u, --unquoted
;-u, --unquoted
              Do not quote the output.  Note that whitespace and special (shell-dependent)  characters  can
:出力を引用符で囲まない。 このモードでは (他の '''getopt'''(1) の実装でそうであるように) 空白文字や特殊な (シェル依存の) 文字が大混乱を引き起こす可能性があることに注意。
              cause havoc in this mode (like they do with other getopt(1) implementations).


      -V, --version
;-V, --version
              Display version information and exit.  No other output is generated.
:バージョン情報を表示して終了する。 他の出力は生成されない。


== PARSING ==
== PARSING ==
      This  section specifies the format of the second part of the parameters of getopt (the parameters in
このセクションでは、getoptのパラメータの2番目の部分('''SYNOPSIS'''のパラメータ)の形式を指定する。 次のセクション([[#OUTPUT]])では、生成される出力について説明する。 これらのパラメータは通常、シェル関数が呼び出されたときのパラメータである。 シェル関数が呼び出された各パラメータが、getoptのパラメータリストにある1つのパラメータに正確に対応するように注意しなければならない([[#EXAMPLES]]を参照)。 すべての解析は GNU '''[https://manpages.debian.org/bookworm/manpages-dev/getopt.3.en.html getopt]'''(3) ルーチンによって行われる。
      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
1つの'-'の後に複数の短いオプションを指定することも可能で、 その場合、すべてのオプション(おそらく最後のオプションを除く)に 必須引数やオプション引数を指定することはできない。
      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
で始まらず、前のオプションの必須引数でもない各パラメータは、 非オプションパラメータである。  '--'パラメータ以降の各パラメータは、常に非オプションパラメータとして解釈される。 環境変数 '''POSIXLY_CORRECT''' が設定されている場合、 または短いオプション文字列が '+' で始まっている場合、 最初の非オプションパラメータが見つかった時点で、 残りのすべてのパラメータは非オプションパラメータとして解釈される。
      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 ==
      Output is generated for each element described in the previous section.  Output is done in the  same
出力は、前のセクションで説明した各要素に対して生成される。 出力は、非オプション・パラメーターを除き、入力で指定された要素と同じ順序で行われる。 出力は、互換(引用符なし)モードで行うこともできるし、引数や非オプション パラメータ内の空白文字やその他の特殊文字を保持するように行うこともできる(引用符を参照)。 出力がシェルスクリプトで処理されるとき、それは(ほとんどのシェル言語でshiftコマンドを使用して)1つずつ処理することができる個別の要素で構成されているように見える。 これはunquotedモードでは不完全である、 要素が空白や特殊文字を含むと、予期しない場所で分割されることがあるからだ。
      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
短いオプションの場合、1つの '-' とオプション文字が1つのパラメータとして生成される。 オプションに引数がある場合、次のパラメータが引数になる。 オプションが引数を取るが何も見つからなかった場合、次のパラメータが生成されるが、クォートモードでは空であり、クォートされていない (互換性のある) モードでは 2 番目のパラメータは生成されない。 他の多くの '''getopt'''(1) 実装はオプション引数をサポートしていないことに注意。
      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‐
長いオプションの場合、'--' と完全なオプション名が 1 つのパラメータとして生成される。 これは、オプションが省略されているか、入力で単一の '-' で指定されているかに関係なく行われる。 引数は短いオプションと同様に扱われる。
      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
通常、すべてのオプションとその引数が生成されるまで、 オプション以外のパラメータ出力は生成されない。 その後、'--'が1つのパラメータとして生成され、 その後に非オプション・パラメータが見つかった順に、 それぞれ別のパラメータとして生成される。 短いオプション文字列の最初の文字が '-' だった場合のみ、 非オプション・パラメータの出力は、入力で見つかった場所に生成される (これは、'''SYNOPSIS''' の最初の書式が使われている場合はサポートされない。 この場合、'-' '+' の前にあるものはすべて無視される)
      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==
==QUOTING==
      In compatible mode, whitespace or 'special' characters in arguments or non-option parameters are not
互換モードでは、引数やオプション以外のパラメータに含まれる空白文字や「特殊」文字は 正しく処理されない。 出力はシェルスクリプトに送られるため、スクリプトは出力をどのように個別の パラメータに分割すればよいのかわからない。 この問題を回避するために、この実装ではクォート処理を提供している。 これは、各パラメーターを引用符で囲んで出力を生成するというものである。  この出力が再び(通常はシェルの'''eval'''コマンドによって)シェルに供給されるとき、パラメータごとに正しく分割される。
      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
環境変数'''GETOPT_COMPATIBLE'''が設定されている場合、'''SYNOPSIS'''の最初の形式が使用されている場合、またはオプション'-u'が見つかった場合、クォートは有効にならない。
      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
シェルによって引用符の付け方が異なる。 '-s'オプションで使用するシェルを選択できる。 現在、以下のシェルがサポートされている: ''[[/usr/bin/sh|sh]]''、''[[/usr/bin/bash|bash]]''、''[[/usr/bin/csh|csh]]''、''[[/usr/bin/tcsh|tcsh]]''である。 shのようなクォート規則とcshのようなクォート規則である。 他のシェルスクリプト言語を使用している場合でも、これらのフレーバーのいずれかを使用できる可能性がある。
      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==
==SCANNING MODES==
      The first character of the short options string may be a '-' or a '+' to indicate a special scanning
短いオプション文字列の最初の文字は、特別なスキャンモードを示すために '-'または'+'にすることができる。 '''SYNOPSIS'''の最初の呼び出し形式が使われる場合、それらは無視される。
      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
最初の文字が'+'の場合、または環境変数'''POSIXLY_CORRECT'''が設定されている場合、 オプション引数でない最初のパラメータ(すなわち、'-'で始まらないパラメータ)が 見つかり次第、解析は停止する。 残りのパラメータは、すべて非オプション・パラメータとして解釈される。
      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==
==COMPATIBILITY==
      This version of getopt(1) is written to be as compatible as possible to other versions.  Usually you
このバージョンの '''getopt'''(1) は、他のバージョンと可能な限り互換性があるように書かれている。 通常、何も変更することなくこのバージョンと置き換えることができ、いくつかの利点もある。
      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
getopt の最初のパラメータの最初の文字が '-' でない場合、 getopt は互換モードに入る。 getoptは最初のパラメータを短いオプションの文字列として解釈し、他のすべての引数は解析される。 環境変数'''POSIXLY_CORRECT'''が設定されていない限り、パラメータのシャッフルは行われる(つまり、オプション以外のパラメータはすべて最後に出力される)。
      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
環境変数'''GETOPT_COMPATIBLE'''は、getoptを強制的に互換モードにする。 この環境変数と'''POSIXLY_CORRECT'''の両方を設定すると、「難しい」プログラムに対して100%の互換性を提供する。 しかし、通常はどちらも必要ない。
      environment variable and POSIXLY_CORRECT offers 100% compatibility for 'difficultprograms.  Usu‐
      ally, though, neither is needed.


      In compatibility mode, leading '-' and '+' characters in the short options string are ignored.
互換モードでは、短いオプション文字列の先頭の '-' '+' は無視される。


==RETURN CODES==
==RETURN CODES==
      getopt returns error code 0 for successful parsing, 1 if getopt(3) returns errors, 2 if it does not
getoptは、解析に成功した場合はエラーコード0を返し、'''[https://manpages.debian.org/bookworm/manpages-dev/getopt.3.en.html getopt]'''(3)がエラーを返した場合は1を返し、自身のパラメータを理解していない場合は2を返し、メモリ不足などの内部エラーが発生した場合は3を返し、-T付きで呼び出された場合は4を返す。
      understand its own parameters, 3 if an internal error occurs like out-of-memory,  and  4  if  it  is
      called with -T.


==EXAMPLES==
==EXAMPLES==
      Example  scripts  for  (ba)sh and  (t)csh are provided with the getopt(1) distribution, and are in‐
'''(ba)sh'''と'''(t)csh'''のスクリプト例は、'''getopt'''(1)ディストリビューションで提供されており、<u>/usr/share/doc/util-linux/getopt/</u>ディレクトリにインストールされている。
      stalled in /usr/share/doc/util-linux/getopt/ directory.


==ENVIRONMENT==
==ENVIRONMENT==
      POSIXLY_CORRECT
;POSIXLY_CORRECT
              This environment variable is examined by the getopt(3) routines.  If it is set, parsing stops
:この環境変数は '''[https://manpages.debian.org/bookworm/manpages-dev/getopt.3.en.html getopt]'''(3) ルーチンで調べられる。 この環境変数が設定されている場合、オプションでもオプション引数でもないパラメータが見つかると、すぐに解析が停止する。 残りのすべてのパラメータも、'-' で始まるかどうかに関係なく、非オプションパラメータとして解釈される。
              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
;GETOPT_COMPATIBLE
              Forces getopt to use the first calling format as specified in the SYNOPSIS.
:'''getopt'''は、'''SYNOPSIS'''で指定された最初の呼び出しフォーマットを強制的に使用する。


==BUGS==
==BUGS==
      getopt(3) can  parse long options with optional arguments that are given an empty optional argument
'''[https://manpages.debian.org/bookworm/manpages-dev/getopt.3.en.html getopt]'''(3) は、空のオプション引数を与えられたオプション引数を持つ長いオプションを解析することができる (しかし、短いオプションに対してはこれを行うことができない)。 この '''getopt'''(1) は、空のオプション引数は存在しないものとして扱う。
      (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==
==AUTHOR==
      Frodo Looijaard ⟨frodo@frodo.looijaard.name⟩
Frodo Looijaard ⟨frodo@frodo.looijaard.name⟩


==SEE ALSO==
==SEE ALSO==
      bash(1), tcsh(1), getopt(3)
'''[[/usr/bin/bash|bash]]''', '''[[/usr/bin/tcsh|tcsh]]''', '''[https://manpages.debian.org/bookworm/manpages-dev/getopt.3.en.html getopt]'''(3)


==AVAILABILITY==
==AVAILABILITY==
      The getopt command is part of the util-linux package and is  available  from  Linux  Kernel  Archive
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/⟩.
      ⟨https://www.kernel.org/pub/linux/utils/util-linux/⟩.


== External link ==
== External link ==

Latest revision as of 13:55, 3 July 2023

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

このセクションでは、getoptのパラメータの2番目の部分(SYNOPSISのパラメータ)の形式を指定する。 次のセクション(#OUTPUT)では、生成される出力について説明する。 これらのパラメータは通常、シェル関数が呼び出されたときのパラメータである。 シェル関数が呼び出された各パラメータが、getoptのパラメータリストにある1つのパラメータに正確に対応するように注意しなければならない(#EXAMPLESを参照)。 すべての解析は GNU getopt(3) ルーチンによって行われる。

パラメータは左から右に解析される。 各パラメータは、短いオプション、長いオプション、オプションの引数、オプションでないパラメータに分類される。

単純な短いオプションは、'-'の後に短いオプション文字が続くものである。 オプションに必須引数がある場合、その引数はオプション文字の直後に書くか、次のパラメータとして書く(つまり、コマンドライン上で空白で区切る)。 オプシ ョ ンにオプシ ョ ナル引数があ る と き は、 オプシ ョ ン文字の後に直接書 く 必要があ る。

1つの'-'の後に複数の短いオプションを指定することも可能で、 その場合、すべてのオプション(おそらく最後のオプションを除く)に 必須引数やオプション引数を指定することはできない。

長いオプションは通常、'--'で始まり、長いオプション名が続く。 オプションに必須引数がある場合、その引数は長いオプション名の後に '=' で区切って直接書くか、次の引数として書く(つまり、コマンドライン上で空白で区切る)。 オプションに省略可能な引数がある場合は、長いオプション名の直後に '=' で区切って書かなければならない('=' の後ろに何も書かないと、引数がないものとして解釈される。) 長いオプションは、省略形があいまいでない限り、省略することができる。

で始まらず、前のオプションの必須引数でもない各パラメータは、 非オプションパラメータである。 '--'パラメータ以降の各パラメータは、常に非オプションパラメータとして解釈される。 環境変数 POSIXLY_CORRECT が設定されている場合、 または短いオプション文字列が '+' で始まっている場合、 最初の非オプションパラメータが見つかった時点で、 残りのすべてのパラメータは非オプションパラメータとして解釈される。

OUTPUT

出力は、前のセクションで説明した各要素に対して生成される。 出力は、非オプション・パラメーターを除き、入力で指定された要素と同じ順序で行われる。 出力は、互換(引用符なし)モードで行うこともできるし、引数や非オプション パラメータ内の空白文字やその他の特殊文字を保持するように行うこともできる(引用符を参照)。 出力がシェルスクリプトで処理されるとき、それは(ほとんどのシェル言語でshiftコマンドを使用して)1つずつ処理することができる個別の要素で構成されているように見える。 これはunquotedモードでは不完全である、 要素が空白や特殊文字を含むと、予期しない場所で分割されることがあるからだ。

必須引数が見つからない、またはオプションが認識されないなど、パラメータの解析に問題がある場合、標準エラー出力にエラーが報告され、問題のある要素の出力は行われず、ゼロ以外のエラーステータスが返される。

短いオプションの場合、1つの '-' とオプション文字が1つのパラメータとして生成される。 オプションに引数がある場合、次のパラメータが引数になる。 オプションが引数を取るが何も見つからなかった場合、次のパラメータが生成されるが、クォートモードでは空であり、クォートされていない (互換性のある) モードでは 2 番目のパラメータは生成されない。 他の多くの getopt(1) 実装はオプション引数をサポートしていないことに注意。

複数の短いオプションが単一の '-' の後に指定された場合、 それぞれが独立したパラメータとして出力される。

長いオプションの場合、'--' と完全なオプション名が 1 つのパラメータとして生成される。 これは、オプションが省略されているか、入力で単一の '-' で指定されているかに関係なく行われる。 引数は短いオプションと同様に扱われる。

通常、すべてのオプションとその引数が生成されるまで、 オプション以外のパラメータ出力は生成されない。 その後、'--'が1つのパラメータとして生成され、 その後に非オプション・パラメータが見つかった順に、 それぞれ別のパラメータとして生成される。 短いオプション文字列の最初の文字が '-' だった場合のみ、 非オプション・パラメータの出力は、入力で見つかった場所に生成される (これは、SYNOPSIS の最初の書式が使われている場合はサポートされない。 この場合、'-' と '+' の前にあるものはすべて無視される)。

QUOTING

互換モードでは、引数やオプション以外のパラメータに含まれる空白文字や「特殊」文字は 正しく処理されない。 出力はシェルスクリプトに送られるため、スクリプトは出力をどのように個別の パラメータに分割すればよいのかわからない。 この問題を回避するために、この実装ではクォート処理を提供している。 これは、各パラメーターを引用符で囲んで出力を生成するというものである。 この出力が再び(通常はシェルのevalコマンドによって)シェルに供給されるとき、パラメータごとに正しく分割される。

環境変数GETOPT_COMPATIBLEが設定されている場合、SYNOPSISの最初の形式が使用されている場合、またはオプション'-u'が見つかった場合、クォートは有効にならない。

シェルによって引用符の付け方が異なる。 '-s'オプションで使用するシェルを選択できる。 現在、以下のシェルがサポートされている: shbashcshtcshである。 shのようなクォート規則とcshのようなクォート規則である。 他のシェルスクリプト言語を使用している場合でも、これらのフレーバーのいずれかを使用できる可能性がある。

SCANNING MODES

短いオプション文字列の最初の文字は、特別なスキャンモードを示すために '-'または'+'にすることができる。 SYNOPSISの最初の呼び出し形式が使われる場合、それらは無視される。

最初の文字が'+'の場合、または環境変数POSIXLY_CORRECTが設定されている場合、 オプション引数でない最初のパラメータ(すなわち、'-'で始まらないパラメータ)が 見つかり次第、解析は停止する。 残りのパラメータは、すべて非オプション・パラメータとして解釈される。

最初の文字が'-'の場合、非オプション・パラメータは見つかった場所に出力される。 通常の操作では、'--'パラメータが生成された後、それらはすべて出力の最後に集められる。 この'--'パラメーターも生成されるが、 このモードでは常に最後のパラメーターとなることに注意。

COMPATIBILITY

このバージョンの getopt(1) は、他のバージョンと可能な限り互換性があるように書かれている。 通常、何も変更することなくこのバージョンと置き換えることができ、いくつかの利点もある。

getopt の最初のパラメータの最初の文字が '-' でない場合、 getopt は互換モードに入る。 getoptは最初のパラメータを短いオプションの文字列として解釈し、他のすべての引数は解析される。 環境変数POSIXLY_CORRECTが設定されていない限り、パラメータのシャッフルは行われる(つまり、オプション以外のパラメータはすべて最後に出力される)。

環境変数GETOPT_COMPATIBLEは、getoptを強制的に互換モードにする。 この環境変数とPOSIXLY_CORRECTの両方を設定すると、「難しい」プログラムに対して100%の互換性を提供する。 しかし、通常はどちらも必要ない。

互換モードでは、短いオプション文字列の先頭の '-' と '+' は無視される。

RETURN CODES

getoptは、解析に成功した場合はエラーコード0を返し、getopt(3)がエラーを返した場合は1を返し、自身のパラメータを理解していない場合は2を返し、メモリ不足などの内部エラーが発生した場合は3を返し、-T付きで呼び出された場合は4を返す。

EXAMPLES

(ba)sh(t)cshのスクリプト例は、getopt(1)ディストリビューションで提供されており、/usr/share/doc/util-linux/getopt/ディレクトリにインストールされている。

ENVIRONMENT

POSIXLY_CORRECT
この環境変数は getopt(3) ルーチンで調べられる。 この環境変数が設定されている場合、オプションでもオプション引数でもないパラメータが見つかると、すぐに解析が停止する。 残りのすべてのパラメータも、'-' で始まるかどうかに関係なく、非オプションパラメータとして解釈される。
GETOPT_COMPATIBLE
getoptは、SYNOPSISで指定された最初の呼び出しフォーマットを強制的に使用する。

BUGS

getopt(3) は、空のオプション引数を与えられたオプション引数を持つ長いオプションを解析することができる (しかし、短いオプションに対してはこれを行うことができない)。 この getopt(1) は、空のオプション引数は存在しないものとして扱う。

短いオプション変数がまったく必要ない場合の構文は、あまり直感的ではない (明示的に空文字列に設定しなければならない)。

AUTHOR

Frodo Looijaard ⟨frodo@frodo.looijaard.name⟩

SEE ALSO

bash, tcsh, 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