GETOPT(1) GETOPT(1)
名前
getopt - コマンドの引き数を解析する (拡張版)
書式
getopt optstring parameters
getopt [options] [--] optstring parameters
getopt [options] -o|--options optstring [options] [--] parameters
説明
getopt は、シェル手続きで簡単に解析できるように、コマンドラインのオプシ
ョンを分解 (解析) するために使われる。また、正しいオプションであるか を
調べるためにも使われる。これを行うために GNU getopt(3) ルーチンが使われ
る。
getopt を呼び出すときに使われたパラメータは、 2 つの部分に分けること が
で き る: getopt の解析動作を変更するオプション (「書式」セクションの
options と -o|--options optstring) と、解析されるパラメータ (「書式」セ
ク ションの parameters) である。第 2 部分は、最初のオプションではないパ
ラメータ (オプション引き数ではないもの) の後か、最初の ‘ -- ’ の後か ら
始 まる。第 1 部分に ‘-o’ または ‘--options’ オプションが見つからない場
合、第 2 部分の最初のパラメータは短い形式のオプション文字列として使われ
る。
環境変数 GETOPT_COMPATIBLE が設定された場合、または最初のパラメータがオ
プションでなかった場合 (‘-’ で始まっていない場合。これは 「書式」セクシ
ョ ンの最初の形式である)、 getopt は他のバージョンの getopt(1) と互換性
のある出力を生成する。この場合でも、パラメータの並べ替えを行い、オプ シ
ョン引き数を認識する (詳細は 「互換性」セクションを参照すること)。
伝統的な getopt(1) の実装では、引き数やオプションではないパラメータで空
白と他の (シェル特有の) 特殊文字を組み合わせて使うことができない。こ の
問 題を解決するため、この実装ではクォートした出力を生成する。この出力は
、シェル (通常は eval コマンドが使われる) によって再び解析されなけれ ば
な ら な い。これにはクォートすべき文字列を保護するという効果があるが、
getopt を他のバージョンとは互換性のない方法 (「書式」セクションの 2, 3
番目の形式) で呼び出さなければならない。拡張版の getopt(1) がインストー
ルされているかどうかを調べるには、特別なテストオプション (-T) を使う こ
とができる。
オプション
-a, --alternative
長いオプションを 1 個の ‘-’ で始めることができるようにする。
-h, --help
ちょっとした使用ガイドを表示し、正常終了する。それ以上は何も出力
されない。
-l, --longoptions longopts
認識させる長い (複数文字の) オプションを指定する。 2 つ以上の オ
プション名をコンマで区切って一度に指定できる。このオプションは 2
回以上指定することができる。 longopts は交換可能であ る 。 lon-
gopts で指定されている長いオプション名の後に、1 個のコロンを続け
ることができる。これは、そのオプションに引き数が必須であることを
示 す。また、長いオプション名の後に 2 個のコロンを続けることもで
きる。これは、そのオプションが引き数を取る場合もあることを示す。
-n, --name progname
エ ラーが報告された場合に getopt(3) ルーチンが使うプログラム名。
このオプションを指定しても、 getopt(1) のエラーは getopt から 発
生したものとして表示される点に注意すること。
-o, --options shortopts
認識させる短い (1 文字の) オプションを指定する。このオプションが
指定されていない場合、 getopt の 1 個の ‘-’ で始まっていない最初
のパラメータ (かつ、オプションの引き数でないもの) が短いオプショ
ンの文字列として使われる。 shortopts に指定されている短いオプ シ
ョン文字の後に、1 個のコロンを続けることができる。これは、そのオ
プションに引き数が必須であることを示す。また、長いオプション名の
後 に 2 個のコロンを続けることもできる。これは、そのオプションが
引き数を取る場合もあることを示す。オプションの解析法と出力の生成
法 を変更するために、 shortopts の最初の文字を ‘+’ または ‘-’ に
することができる (詳細は 「スキャンモード」セクションを参照す る
こと)。
-q, --quiet
getopt(3) によるエラー表示をさせない。
-Q, --quiet-output
通 常の出力を生成させない。 -q を指定しない限り、 getopt(3) によ
ってエラーが表示される。
-s, --shell shell
指定したシェルのクォート方法に設定する。 -s オプションが指定され
ていない場合、 BASH でのクォート方法が使われる。指定可能な引き数
は、現在のところ ‘sh’, ‘bash’, ‘csh’, ‘tcsh’ である。
-u, --unquoted
出力をクォートしない。空白と (シェル依存の) 特殊文字は 、( 他 の
getopt(1) の実装と同じように) このモードでは大混乱を引き起こす。
-T --test
getopt(1) が拡張バージョンか古いバージョンかをテストする。何も出
力しないが、エラーステータスを 4 に設定する。他の getopt(1) の実
装の場合や、このバージョンで環境変数 GETOPT_COMPATIBLE が設定 さ
れている場合、 ‘--’ を返し、エラーステータスを 0 に設定する。
-V, --version
バージョン情報を出力し、正常終了する。それ以上は何も出力されない
。
解析
このセクションでは getopt のパラメータの第 2 部分 (「書式」セクションの
parameters) のフォーマットについて説明している。次のセクション (出力)
では生成される出力について説明している。これらのパラメータは、一般に 、
シ ェル関数が呼ばれたときのパラメータである。シェル関数が呼ばれたときの
各パラメータが getopt のパラメータリストにある 1 つのパラメータに厳密に
対応している点に注意しなければならない (「例」セクションを参照すること)
。全ての解析が GNU getopt(3) ルーチンで行われる。
パラメータは左から右へ解析される。各パラメータは、短いオプション・長 い
オ プション・オプションへの引き数・オプションではないパラメータに分類さ
れる。
簡単な短いオプションは、 ‘-’ の後に短いオプション文字が続くものである。
オ プションが引き数を必須としている場合、引き数はオプション文字の直後に
書くことができる。 (コマンドライン上で空白で区切られた) 次のパラメー タ
と して書くこともできる。オプションが引き数を取ることも取らないこともで
きる場合、引き数が存在するならば、オプション文字の直後に書かなければ な
らない。
(最後のオプションを除く) 全てのオプションが必須の引き数もオプションとし
ての引き数も必要としない限り、 1 個の ‘-’ の後に複数の短いオプション を
指定することができる。
長 いオプションは、通常 ‘--’ の後に長いオプション名が続く。オプションが
引き数を必須としている場合、 ‘=’ で区切って長いオプション名の直後に書く
ことができる。また、(コマンドライン上で空白で区切って) 次の引き数として
書くこともできる。オプションが引き数を取ることも取らないこともできる 場
合、引き数が存在するならば、 ‘=’ で区切って長いオプション名の直後に書か
なければならない (‘=’ をオプションの後に書いたにも関らず、その後に何 も
指 定しなかった場合、引き数が存在しないものとして解釈される。これはちょ
っとしたバグである。 「バグ」セクションを参照すること)。長いオプショ ン
は 、省略形が曖昧でない (他のオプションと区別がつく) 限り、短く省略する
ことができる。
‘-’ で始まらず、かつ前のオプションが必須としている引き数でもないパラ メ
ー タは、オプションではないパラメータである。 ‘--’ パラメータの後にある
パラメータは、オプションではないパラメータとして解釈される。環 境 変 数
POSIXLY_CORRECT が設定されている場合、または短いオプション文字列が ‘+’
で始まっている場合、最初のオプションではないパラメータが見つかった時 点
で 、残りの全てのパラメータはオプションではないパラメータとして解釈され
る。
出力
出力は前のセクションで説明した各要素に対して生成される。出力は要素が 入
力 で指定された順番で生成される。ただし、オプションではないパラメータは
例外である。出力は 互換 (クォートされない) モードで生成することができる
。 また、引き数とオプションではないパラメータに含まれる空白と他の特殊文
字を保護するモードで出力することもできる (「クォート」セクションを参 照
すること)。出力がシェルスクリプトで処理される場合、その出力は別々の要素
から構成されているようにみえる。この要素は (大部分のシェ ル 言 語 で は
shift コマンドを使って) 1 つ 1 つ処理できる。この動作はクォートされない
モードでは不完全である。なぜなら、要素に空白や特殊文字があった場合、 要
素 が期待していない箇所で分割されてしまうからである。必須とされる引き数
が見つからない、またはオプションが認識されない、といった原因でパラメ ー
タ 解析に問題がある場合、標準エラーにエラーが表示される。このとき、不正
な要素に対しては何も出力されず、 0 でないエラーステータスが返される。
短いオプションに対して、出力として 1 個の ‘-’ とオプション文字が生成 さ
れ る。オプションが引き数を取る場合、次のパラメータが引き数になる。オプ
ションが引き数を取っても取らなくてもよい場合に、引き数が指定されてい な
い と、クォートモードでは次のパラメータが生成されるが空のパラメータにな
る。この場合、クォートしない (互換) モードでは 2 番目のパラメータは生成
されない。他の多くの getopt(1) の実装では、取っても取らなくてもよい引き
数はサポートされていない点に注意すること。
複数の短いオプションが 1 個の ‘-’ の後に指定されている場合、各オプシ ョ
ンは区切られたパラメータとして出力に表示される。
長いオプションに対して、 ‘--’ と完全なオプション名が 1 つのパラメータと
して生成される。「入力でオプションが略書きされている。または、オプシ ョ
ン が 1 個の ‘-’ を使って指定されている。」ということに関らず、この動作
をする。引き数は短いオプションとして扱われる。
通常、全てのオプションとその引き数が出力に生成されるまで、オプション で
は ないパラメータは出力されない。そして、1 個のパラメータとして ‘--’ が
生成される。その後にオプションではないパラメータは、見つかった順番で 別
々 のパラメータとして生成される。短いオプション文字列の最初の文字が ‘-’
である場合にのみ、オプションではないパラメータは入力で見つかった位置 で
出力される (この動作は 「書式」セクションの最初の形式が使われた場合には
サポートされない。この場合、 ‘-’ と ‘+’ が前に付く全てのパラメータが 無
視される)。
クォート
互 換モードでは、引き数やオプションではないパラメータにある空白や「特殊
」文字は正しく扱われない。この出力はシェルスクリプトに与えられるので 、
ス クリプトは、出力をどのようにして個々のパラメータに分割すべきなのかを
知らない。この問題を回避するため、この実装ではクォート機能を提供する 。
こ れは、各パラメータをクォートして出力を生成する、という手法を取る。こ
の出力がもう一度シェル (通常はシェルの eval コマンド) に与えられた場 合
、出力は個々のパラメータに正しく分割される。
環 境変数 GETOPT_COMPATIBLE が設定された場合・ 「書式」セクションの最初
の形式が使われた場合・ ‘-u’ オプションが指定された場合、クォートは行 わ
れない。
ク ォートの規則はシェルごとに異なる。使用しているシェルを選択するために
‘-s’ オプションを使うことができる。以下のシェルで正しく機能する: ‘sh’,
‘bash’, ‘csh’, ‘tcsh’. 実際には、2 つの「方式」に分類される: sh 式のク
ォート規則と csh 式のクォート規則である。他のシェルスクリプト言語を使っ
ている場合でも、これらの方式のどちらかが使える可能性がある。
スキャンモード
特 殊なスキャンモードであることを示すために、短いオプションの最初の文字
を ‘-’ または ‘+’ にすることができる。 「書式」セクションの最初の呼び出
し 形 式が使われた場合、これは無視される。しかし、環境変数 POSIXLY_COR-
RECT が指定されているかどうかは調べられる。
最初の文字が ‘+’ の場合、または環境変数 POSIXLY_CORRECT が設定されて い
る場合、オプションではない最初のパラメータ (つまり、 ‘-’ で始まっていな
いパラメータ) がオプション引き数でないと分かった時点で解析はストップ す
る 。それ以降の全てのパラメータは、オプションではないパラメータとして解
釈される。
最初の文字が ‘-’ の場合、オプションではない引き数は見つかった箇所で出力
さ れる。通常の操作では、 ‘--’ パラメータが生成された後で、最後にまとめ
て出力される。この場合でも ‘--’ パラメータは生成されるが、通常このモ ー
ドでは最後のパラメータになる点に注意すること。
互換性
このバージョンの getopt(1) は、出来るだけ他のバージョンと互換性があるよ
うに書かれた。通常は他のバージョンを修正することなく、このバージョン に
置き換えることができる。更に、いくつかの利点がある。
getopt の最初のパラメータの最初の文字が ‘-’ でない場合、getopt は互換モ
ードになる。最初のパラメータは短いオプションの文字列として解釈され、 他
の全ての引き数が解析される。この場合でも、環境変数 POSIXLY_CORRECT が設
定されていない限り、パラメータの並べ替えを行う (つまり、オプションで は
ない全てのパラメータが最後に出力される)。
環境変数 GETOPT_COMPATIBLE は getopt を強制的に互換モードにする。この環
境変数と POSIXLY_CORRECT の両方を設定すると、「難しい」プログラムのため
に 100% の互換性を提供する。しかし、通常はどちらも設定する必要がない。
互 換モードでは、短いオプション文字列の最初に付く ‘-’ と ‘+’ は無視され
る。
リターンコード
解析に成功した場合、 getopt はエラーコード 0 を返す。 getopt(3) がエ ラ
ー を返した場合は 1 を返す。パラメータが理解できなかった場合は 2 を返す
。メモリが足りない (out-of-memory) といった内部エラーの場合は 3 を返 す
。 -T オプションを付けて呼び出された場合は 4 を返す。
例
(ba)sh と (t)csh での使用例のスクリプトは、 getopt(1) ディストリビュー
ションで提供されている。これらはオプションとして /usr/local/lib/getopt
または /usr/lib/getopt にインストールされている。
環境変数
POSIXLY_CORRECT
こ の環境変数は getopt(3) ルーチンで調べられる。これが設定されて
いる場合、パラメータがオプションまたはオプション引き数でないと分
か っ た時点で解析は停止する。それ以降の全てのパラメータは、 ‘-’
で始まっているかどうかに関係なく、オプションではないパラメータと
して解釈される。
GETOPT_COMPATIBLE
getopt に対して強制的に 「書式」セクションの最初の呼び出し形式を
使わせる。
バグ
getopt(3) は、引き数を取っても取らなくてもよい長いオプションを解析で き
る (ただし、短いオプションの場合は解析できない)。この getopt(1) は、オ
プション引き数が指定されていない場合、それが存在しないものとして扱う。
短いオプション変数を全く使いたくない場合、書式は全く直感的でないもの に
なる (明示的に空の文字列に設定する必要がある)。
著者
Frodo Looijaard
GETOPT(1) GETOPT(1)
NAME
getopt - parse command options (enhanced)
SYNOPSIS
getopt optstring parameters
getopt [options] [--] optstring parameters
getopt [options] -o|--options optstring [options] [--] parameters
DESCRIPTION
getopt is used to break up (parse) options in command lines for easy
parsing by shell procedures, and to check for legal 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 will parse (options and
-o|--options 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 its first
parameter is not an option (does not start with a ‘-’, this is the
first format in the SYNOPSIS), getopt will generate output that is com-
patible with that of other versions of getopt(1). It will still do
parameter shuffling and recognize optional arguments (see section COM-
PATIBILITY for more information).
Traditional implementations of getopt(1) are unable to cope with
whitespace and other (shell-specific) special characters in arguments
and non-option parameters. To solve this problem, this implementation
can generate quoted output which must once again be interpreted by the
shell (usually by using the eval command). This has the effect of pre-
serving 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 option (-T) can be used.
OPTIONS
-a, --alternative
Allow long options to start with a single ‘-’.
-h, --help
Output a small usage guide and exit successfully. No other out-
put is generated.
-l, --longoptions longopts
The long (multi-character) options to be recognized. More than
one option name may be specified 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 argument.
-n, --name progname
The name that will be used by the getopt(3) routines when it
reports errors. Note that errors of getopt(1) are still reported
as coming from getopt.
-o, --options shortopts
The short (one-character) options to be recognized. If this
option is not found, the first 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 argu-
ment, and by two colons to indicate it has an optional argument.
The first character of shortopts may be ‘+’ or ‘-’ to influence
the way options are parsed and output is generated (see section
SCANNING MODES for details).
-q, --quiet
Disable error reporting by getopt(3).
-Q, --quiet-output
Do not generate normal output. Errors are still reported by
getopt(3), unless you also use -q.
-s, --shell shell
Set quoting conventions to those of shell. If no -s argument is
found, the BASH conventions are used. Valid arguments are cur-
rently ‘sh’ ‘bash’, ‘csh’, and ‘tcsh’.
-u, --unquoted
Do not quote the output. Note that whitespace and special
(shell-dependent) characters can cause havoc in this mode (like
they do with other getopt(1) implementations).
-T, --test
Test if your getopt(1) is this enhanced version or an old ver-
sion. This generates no output, 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.
-V, --version
Output version information and exit successfully. No other out-
put is generated.
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 parameter the shell function was called with corresponds to
exactly one parameter in the parameter list of getopt (see the EXAM-
PLES). All parsing is done by the GNU getopt(3) routines.
The parameters are parsed from left to right. Each parameter is classi-
fied 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 (ie. separated by whites-
pace on the command line). If the option has an optional argument, it
must be written 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 required argument, it may be written
directly after the long option name, separated by ‘=’, or as the next
argument (ie. 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 ambiguous.
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 compati-
ble (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 processed 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 characters.
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 regardless 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 parameters 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 implementation offers quoting. The idea is that output is gener-
ated with quotes around each parameter. When this output is once again
fed to the shell (usually by a shell eval command), it is split cor-
rectly 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’. Actually, only
two ‘flavors’ are distinguished: sh-like quoting conventions and
csh-like quoting conventions. 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 (ie. a parameter that does not start with a ‘-’) is found
that is not an option argument. The remaining parameters are all inter-
preted 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 col-
lected 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 param-
eter as the string of short options, and all other arguments will be
parsed. It will still do parameter shuffling (ie. all non-option param-
eters are outputted at the end), unless the environment variable
POSIXLY_CORRECT is set.
The environment variable GETOPT_COMPATIBLE forces getopt into compati-
bility mode. Setting both this environment variable and POSIXLY_CORRECT
offers 100% compatibility for ‘difficult’ programs. Usually, 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 optionally installed in /usr/share/getopt.
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 can not 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
Copyright(C) linux-cmd.com All Rights Reserved. Author Takayuki Yukawa