Linux getopt()函数 getopt_long()函数---转
http://hi.baidu.com/scoundrelgg/item/d4083f8412eea05d26ebd97f
Linux getopt()函數 getopt_long()函數
get_opt()函數:
函數原型::
#include <unistd.h>
int getopt(int argc, char * const argv[], const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
用法見右邊欄
1.參數說明:
optstring:選項字母組成的字串。如果該字串里的任一字符后面有冒號,那么這個選項就要求有選項參數。
char *optarg:當前選項參數字串(如果有)。
int optind:argv的當前索引值。當getopt()在while循環中使用時,循環結束后,剩下的字串視為操作數,在argv[optind]至argv[argc-1]中可以找到。
int opterr:這個變量非零時,getopt()函數為“無效選項”和“缺少參數選項,并輸出其錯誤信息。
int optopt:當發現無效選項字符之時,getopt()函數或返回'?'字符,或返回':'字符,并且optopt包含了所發現的無效選項字符。
2.更改getopt()函數的出錯信息輸出與否:
a. 在調用getopt()之前,將opterr設置為0,這樣就可以在getopt()函數發現錯誤的時候強制它不輸出任何消息。
b. 在optstring參數的第一個字符前加冒號,那么getopt()函數就會保持沉默,并根據錯誤情況返回不同字符,如下:
“無效選項” —— getopt()返回'?',并且optopt包含了無效選項字符(這是正常的行為)。
“缺少選項參數” —— getopt()返回':',如果optstring的第一個字符不是冒號,那么getopt()返回'?',這會使得這種情況不能與無效選項的情況區分開。
?
/*get_opt function test
huasion 20090920
*/
#include <stdio.h>
#include <unistd.h>
?
int main( int argc, char ** argv)
{?
??
int i,oc;
int res = 0;
char *b_opt_arg;
char ec;
/*commen print argument*/
printf("******************************************************\n");?
printf("usage: get_opt \n ");
printf("argc=%d\n", argc);
for(i=0;i<argc;i++)
?? printf("argv[%d]:%s\n", i, argv[i]);
printf("******************************************************\n");
/*over*/
while((oc=getopt(argc,argv,":ab:cd:")) != -1){
?? switch(oc){
?? case 'a':
??? printf("case a optind = %d.\n",optind);
??? break;
?? case 'b':
??? printf("case b have a value,optarg = %s ,optind = %d.\n",optarg,optind);
??? break;
?? case 'c':
??? printf("case c optind = %d.\n",optind);
??? break;
?? case 'd':
??? printf("case d have a value optarg = %s ,optind = %d.\n",optarg,optind);
??? break;
?? case '?':
??? ec = (char)optopt;
???
??? printf("option \'%c\' invalid! optind = %d, .\n",ec,optarg,optind);
???
?? case ':':
??? printf("getopt() return value : , you need a option value \n");
???
?? default:
??? break;
?? }
}
printf("it is over, res = %d \n",res);
?
}
**********************************分割線**********************************
getopt_long()函數
函數原型:
int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);
前三個參數的含義和getopt()函數一致,其余意義如下:
longopts: 長選項表地址 ,定義為:
?? struct option{
??????????? const char *name;
??????????? int has_arg;
??????????? int *flag;
??????????? int val;
???? };
longindex: 如果沒有設置為NULL,那么它就指向一個變量,這個變量會被賦值為尋找到的長選項在longopts中的索引值,這可以用于錯誤診斷。
const char *name: 這是選項名,前面沒有短橫線。譬如"help"、"verbose"之類。
int has_arg:描述了選項是否有選項參數。如果有,是哪種類型的參數,此時,它的值一定是下表中的一個。符號常量 數值 含義?
no_argument 0 選項沒有參數?
required_argument 1 選項需要參數?
optional_argument 2 選項參數可選
int *flag
如 果這個指針為NULL,那么getopt_long()返回該結構val字段中的數值。如果該指針不為NULL,getopt_long()會使得它所指 向的變量中填入val字段中的數值,并且getopt_long()返回0。如果flag不是NULL,但未發現長選項,那么它所指向的變量的數值不變。
int val
這 個值是發現了長選項時的返回值,或者flag不是NULL時載入*flag中的值。典型情況下,若flag不是NULL,那么val是個真/假值,譬如1 或0;另一方面,如果flag是NULL,那么val通常是字符常量,若長選項與短選項一致,那么該字符常量應該與optstring中出現的這個選項的 參數相同。
每個長選項在長選項表中都有一個單獨條目,該條目里需要填入正確的數值。數組中最后的元素的值應該全是0。數組不需要排序,getopt_long()會進行線性搜索。但是,根據長名字來排序會使程序員讀起來更容易。
?
?
/*get_opt function test
huasion 20090920
*/
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
?
int main( int argc, char ** argv)
{?
??
int i,oc;
int res = 0;
char *b_opt_arg;
char ec;
int do_name, do_gf_name;
char *l_opt_arg;
struct option longopts[] = {
?? { "name", no_argument, &do_name, 1 },
?? { "gf_name", no_argument, &do_gf_name, 1 },
?? { "love", required_argument, NULL, 'l' },
?? { 0, 0, 0, 0},
};
/*commen print argument*/
printf("******************************************************\n");?
printf("usage: get_opt \n ");
printf("argc=%d\n", argc);
for(i=0;i<argc;i++)
?? printf("argv[%d]:%s\n", i, argv[i]);
printf("******************************************************\n");
/*over*/
while((oc = getopt_long(argc, argv, ":l:", longopts, NULL)) != -1){
switch (oc){
case 'l':
?? l_opt_arg = optarg;
?? printf("Our love is %s!\n", l_opt_arg);
break;
case 0:
?? printf("getopt_long()設置變量 : do_name = %d\n", do_name);
?? printf("getopt_long()設置變量 : do_gf_name = %d\n", do_gf_name);
break;
}
}
printf("it is over, res = %d \n",res);
?
}
?
?
#include <stdio.h>
#include <getopt.h>
int do_name, do_gf_name;
char *l_opt_arg;
struct option longopts[] = {
{ "name", no_argument, NULL, 'n' },
{ "gf_name", no_argument, NULL, 'g' },
{ "love", required_argument, NULL, 'l' },
{ 0, 0, 0, 0},
};
int main(int argc, char *argv[])
{
int c;
while((c = getopt_long(argc, argv, ":ngl:", longopts, NULL)) != -1){
//其中,選項以及其有無參數由":ngl:"決定 而不是在option longopts[] 表中的值決定
switch (c){
case 'n':
printf("My name is LYR.\n");
break;
case 'g':
printf("Her name is BX.\n");
break;
case 'l':
l_opt_arg = optarg;
printf("Our love is %s!\n", l_opt_arg);
break;
}
}
return 0;
}
轉載于:https://www.cnblogs.com/davidwang456/p/3544521.html
總結
以上是生活随笔為你收集整理的Linux getopt()函数 getopt_long()函数---转的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言中extern的用法--转
- 下一篇: 学习中接触的计算机概念