在Linux中,我們可以使用getopt、getopt_long、getopt_long_only來對這個問題進行處理。
#include <unistd.h>
int getopt(int argc, char * const argv[],
const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
#include <getopt.h>
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講起,getopt函數的前兩個參數,就是main函數的argc和argv,這兩者直接傳入即可,要考慮的就只剩下第三個參數。
optstring的格式舉例說明比較方便,例如:
char *optstring = "abcd:";
上面這個optstring在傳入之後,getopt函數將依次檢查命令行是否指定了 -a, -b, -c及 -d(這需要多次調用getopt函數,直到其返回-1),當檢查到上面某一個參數被指定時,函數會返回被指定的參數名稱(即該字母)
最後一個參數d後面帶有冒號,: 表示參數d是可以指定值的,如 -d 100 或 -d user。
optind表示的是下一個將被處理到的參數在argv中的下標值。
如果指定opterr = 0的話,在getopt、getopt_long、getopt_long_only遇到錯誤將不會輸出錯誤信息到標準輸出流。
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
int opt;
char *optstring = "a:b:c:d";
while ((opt = getopt(argc, argv, optstring)) != -1)
{
printf("opt = %c\n", opt);
printf("optarg = %s\n", optarg);
printf("optind = %d\n", optind);
printf("argv[optind - 1] = %s\n\n", argv[optind - 1]);
}
return 0;
}
編譯上述程序並運行,有如下結果:
cashey@ubuntu:~/Desktop/getopt$ ./test_getopt -a 100 -b 200 -c admin -d
opt = a
optarg = 100
optind = 3
argv[optind - 1] = 100
opt = b
optarg = 200
optind = 5
argv[optind - 1] = 200
opt = c
optarg = admin
optind = 7
argv[optind - 1] = admin
opt = d
optarg = (null)
optind = 8
argv[optind - 1] = -d
int getopt(int argc, char * const argv[],
const char *optstring);
int getopt_long(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
在這裏,longopts指向的是一個由option結構體組成的數組,那個數組的每個元素,指明瞭一個“長參數”(即形如--name的參數)名稱和性質:
struct option {
const char *name;
int has_arg;
int *flag;
int val;
};
name 是參數的名稱
has_arg 指明是否帶參數值,其數值可選:
no_argument (即 0) 表明這個長參數不帶參數(即不帶數值,如:--name)
required_argument (即 1) 表明這個長參數必須帶參數(即必須帶數值,如:--name Bob)
optional_argument(即2)表明這個長參數後面帶的參數是可選的,(即--name和--name Bob均可)
flag 當這個指針爲空的時候,函數直接將val的數值從getopt_long的返回值返回出去,當它非空時,val的值會被賦到flag指向的整型數中,而函數返回值爲0
val 用於指定函數找到該選項時的返回值,或者當flag非空時指定flag指向的數據的值。
另一個參數longindex,如果longindex非空,它指向的變量將記錄當前找到參數符合longopts裏的第幾個元素的描述,即是longopts的下標值。
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
int
main(int argc, char **argv)
{
int opt;
int digit_optind = 0;
int option_index = 0;
char *optstring = "a:b:c:d";
static struct option long_options[] = {
{"reqarg", required_argument, NULL, 'r'},
{"noarg", no_argument, NULL, 'n'},
{"optarg", optional_argument, NULL, 'o'},
{0, 0, 0, 0}
};
while ( (opt = getopt_long(argc, argv, optstring, long_options, &option_index)) != -1)
{
printf("opt = %c\n", opt);
printf("optarg = %s\n", optarg);
printf("optind = %d\n", optind);
printf("argv[optind - 1] = %s\n", argv[optind - 1]);
printf("option_index = %d\n", option_index);
}
return 0;
}
編譯運行以上程序並運行,可以得到以下結果:
cashey@ubuntu:~/Desktop/getopt$ ./test_getopt_long -a 100 --reqarg 100 --nonarg
opt = a
optarg = 100
optind = 3
argv[optind - 1] = 100
option_index = 0
opt = r
optarg = 100
optind = 5
argv[optind - 1] = 100
option_index = 0
./test_getopt_long: unrecognized option '--nonarg'
opt = ?
optarg = (null)
optind = 6
argv[optind - 1] = --nonarg
option_index = 0
當所給的參數存在問題時,opt(即函數返回值是'?'),如:
cashey@ubuntu:~/Desktop/getopt$ ./test_getopt_long -a
./test_getopt_long: option requires an argument -- 'a'
opt = ?
optarg = (null)
optind = 2
argv[optind - 1] = -a
option_index = 0
cashey@ubuntu:~/Desktop/getopt$ ./test_getopt_long --reqarg
./test_getopt_long: option '--reqarg' requires an argument
opt = ?
optarg = (null)
optind = 2
argv[optind - 1] = --reqarg
最後說說getopt_long_only函數,它與getopt_long函數使用相同的參數表,在功能上基本一致,只是getopt_long只將--name當作長參數,但getopt_long_only會將--name和-name兩種選項都當作長參數來匹配。在getopt_long在遇到-name時,會拆解成-n -a -m -e到optstring中進行匹配,而getopt_long_only只在-name不能在longopts中匹配時纔將其拆解成-n -a -m -e這樣的參數到optstring中進行匹配。