#include <unistd.h> int getopt(int argc, char * const argv[], const char *optstring); extern char *optarg; extern int optind, opterr, optopt;argc, argv: 跟 main() 一直
optstring: 接受的选项即选项类别(见下文)。
optarg: 如果选项带参数,则 optarg 指向参数字符串,如 -axxx, 如果 a 是可以接受参数的选项(见下文),则 optarg 指向字符串 "xxx".
optind: option index. The variable optind is the index of the next element to be processed in argv. The system initializes this value to 1. The caller can reset it to 1 to restart scanning of the same argv, or when scanning a new argument vector.
opterr: flag, 如果设置 opterr=0, 则不输出错误。
optopt: 遇到 optstring 中不存在的选项就保存在 optopt 中。
"1abc:d:2::"
1,a,b: 只有选项,无选项参数
c,d: 必须要选项参数(选项和选项参数之间可以有空格)
2: 选项参数可选(可选选项如果选项和选项参数之间有空格,则被视为无选项参数)
If an option was successfully found, then getopt() returns the option character.
If all command-line options have been parsed, then getopt() returns -1.
If getopt() encounters an option character that was not in optstring, then '?' is returned.
If getopt() encounters an option with a missing argument, then the return value depends on the first character in optstring:
if it is ':', then ':' is returned; otherwise '?' is returned.
#include <unistd.h> #include <iostream> using namespace std; #define OPTSTR "1abc:d:2::" int main(int argc, char *argv[]) { opterr = 0; int iRet = 0; int count = 0; cout << "optstring: " << OPTSTR << endl; cout << "BEGIN: optind=" << optind << endl; while ( (iRet = getopt(argc, argv, OPTSTR)) != -1 ) { switch (iRet) { case '1': cout << "-" << (char)iRet << ", value=" << (optarg ? optarg : "(no value)") << ", optind=" << optind << ", argv[optind-1]=" << argv[optind - 1] << endl; break; case 'a': cout << "-" << (char)iRet << ", value=" << (optarg ? optarg : "(no value)") << ", optind=" << optind << ", argv[optind-1]=" << argv[optind - 1] << endl; break; case 'b': cout << "-" << (char)iRet << ", value=" << (optarg ? optarg : "(no value)") << ", optind=" << optind << ", argv[optind-1]=" << argv[optind - 1] << endl; break; case 'c': cout << "-" << (char)iRet << ", value=" << (optarg ? optarg : "(no value)") << ", optind=" << optind << ", argv[optind-1]=" << argv[optind - 1] << endl; break; case 'd': cout << "-" << (char)iRet << ", value=" << (optarg ? optarg : "(no value)") << ", optind=" << optind << ", argv[optind-1]=" << argv[optind - 1] << endl; break; case '2': cout << "-" << (char)iRet << ", value=" << (optarg ? optarg : "(no value)") << ", optind=" << optind << ", argv[optind-1]=" << argv[optind - 1] << endl; break; default: // '?' cout << "error: iRet=" << (char)iRet << ", optind=" << optind << ", optopt=" << (char)optopt << endl; break; } ++count; } cout << "count: " << count << endl; return 0; } /* correct: ./test -1 -a -b -dvalue -cvalue -2value ./test -1 -dvalue -2value ./test -1 -dvalue -2 ./test -1 -a -b -d value -c value -2value wrong: ./test -1value ./test -x */
int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);
struct option { const char *name; int has_arg; int *flag; int val; };name: 长选项名称
has_arg: 选项类型,是否带参数
Symbolic constant |
Numeric value |
Meaning |
no_argument |
0 |
The option does not take an argument. |
required_argument |
1 |
The option requires an argument. |
optional_argument |
2 |
The option's argument is optional. |
flag, val: 如果 flag 设置为 NULL, 则返回值为 val. 如果 flag 为非 NULL, 则返回值为0, flag 所指向的变量的值被设为 val.
Typically, if flag is not NULL, then val is a true/false value, such as 1 or 0. On the other hand, if flag is NULL, then val is usually a character constant.
如果一个选项既有长格式又有短格式(如 --help 和 -h),将 val 的值设为短格式的值(如 'h')。
int do_all, do_help, do_verbose; /* flag variables */ char *myfile; struct option longopts[] = { { "all", no_argument, & do_all, 1 }, { "file", required_argument, NULL, 'f' }, { "help", no_argument, & do_help, 1 }, { "verbose", no_argument, & do_verbose, 1 }, { 0, 0, 0, 0 } }; ... while ((c = getopt_long(argc, argv, ":f:", longopts, NULL)) != -1) { switch (c) { case 'f': myfile = optarg; break; case 0: /* getopt_long() set a variable, just keep going */ break; ... Error handling code here } }
http://www.informit.com/articles/article.aspx?p=175771&seqNum=3