C言語 - 標準ライブラリ - getopt.h

 クラウディア
1. getopt_long

1. getopt_long

 コマンドラインの引数リストから、オプション文字を取得します。  unistd.h で定義されている getopt の発展形と言えます。

#include <getopt.h>

extern char *optarg;
extern int optind;
extern int optopt;
extern int opterr;
extern int optreset;

int getopt_long(int argc, char * const *argv, const char *optstring,
						const struct option *longopts, int *longindex);

int getopt_long_only(int argc, char * const *argv, const char *optstring,
						const struct option *longopts, int *longindex);
 getopt は -(ハイフン1文字)形式のオプションしか許可していませんが、getopt_long は --(ハイフンを重ねる)形式を許可します。  getopt_long_only という関数は getopt_long に近いのですが、短いオプションと長いオプションの介し方が若干違うようです。