getopt和getopt_long

getopt

#include <getopt.h>
int getopt(int argc, char * const argv[], const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;

如果选项成功找到,返回选项字符;如果所有命令行选项都解析完毕,返回 -1;如果遇到选项字符不在 optstring 中,getopt() 会输出一条错误信息,然后返回字符 ‘?’,并将 optopt 置为该选项字符;如果遇到丢失参数,返回 ‘?’ 或 ‘:’,当返回 ‘?’ 时且 opterr 非 0 会提示错误信息。

optarg —— 指向当前选项参数(如果有)的指针或NULL(无参数的情况)。
optind —— 再次调用 getopt() 时下一个 argv-element 的索引。
optopt —— 最后一个未知选项。
opterr ­—— 是否希望 getopt() 向 stderr 打印出错信息,0 为不打印,opterr 默认非0。

optstring:由三部分组成,第一部分是可选的字符’+‘或’-‘,第二部分是一个可选的字符’:',第三部分是具体的选项字符串。

举例说明第三部分 “ab:c::d:e”,abcde 分别是选项字符,选项字符其后跟 “:” 表示该选项有参数,选项跟参数之间可有空格,也可没有空格,选项字符后跟 “::” 表示参数可有可无,但如果有参数选项和参数之间不能有空格,选项字符后不跟 “:” 或 “::” 表示该选项没参数。"-ae -b100 -c -d 200"就是一个对应于上述 optstring 的命令选项,没参数的选项可以写在一起,比如 -ae。

getopt() 默认情况下会改变参数的顺序,从而使 nonoption argv-element(非选项及其参数) 移至所有选项之后。
如果第一部分是字符 ‘+’,或者设置了环境变量POSIXLY_CORRECT,getopt() 不会改变参数顺序,第一个 nonoption argv-element 出现时立即停止选项处理,例如 “+ab:c::d:e”,“-ae 100 -c -d 200” 中 100 及其以后的参数都不再作为选项处理。
如果第一部分是 ‘-’,getopt() 不会改变参数顺序,每个 nonoption argv-element 作为选项 1 的参数,也就是 getopt() 会返回1,这里的 1 是整数 1 不是字符 1。

如果第二部分的 ‘:’ 存在,当丢失选项参数时 getopt() 不再返回 ‘?’ 而是返回 ‘:’,且不会打印出错信息。

The special argument “–” forces an end of option-scanning regardless of the scanning mode.

#include <stdio.h>
#include <getopt.h>
int main(int argc, char *argv[])
{
	int opt;
	const char *optstring = "ab:c::d:e";
	while (((opt = getopt(argc, argv, optstring))) != -1)
	{
		printf("args: ");
		for(int i=0;i<argc;i++) printf("%s ", argv[i]);
		printf("\n");
		if(opt == 1) printf("opt = (%d)\t", opt);
		else printf("opt = %c \t", opt);
		printf("optarg = %s\t", optarg);
		printf("optind = %d\t", optind);
		printf("argv[optind] = %s\t", argv[optind]);
		printf("optopt = %c(%d)\n\n", optopt, optopt);
	}
	printf("args: ");
	for(int i=0;i<argc;i++) printf("%s ", argv[i]);
	printf("\n");
	printf("after processing optind = %d, argv[optind] = %s\n", optind, argv[optind]);
}

./a.out file -ae -b100 -c -z -d 200
args: t file -ae -b100 -c -z -d 200 
opt = a         optarg = (null) optind = 2      argv[optind] = -ae      optopt = (0)

args: t file -ae -b100 -c -z -d 200 
opt = e         optarg = (null) optind = 3      argv[optind] = -b100    optopt = (0)

args: t -ae file -b100 -c -z -d 200 
opt = b         optarg = 100    optind = 4      argv[optind] = -c       optopt = (0)

args: t -ae -b100 file -c -z -d 200 
opt = c         optarg = (null) optind = 5      argv[optind] = -z       optopt = (0)

t: invalid option -- 'z'
args: t -ae -b100 -c file -z -d 200 
opt = ?         optarg = (null) optind = 6      argv[optind] = -d       optopt = z(122)

args: t -ae -b100 -c -z file -d 200 
opt = d         optarg = 200    optind = 8      argv[optind] = (null)   optopt = z(122)

args: t -ae -b100 -c -z -d 200 file 
after processing optind = 7, argv[optind] = file

getopt_long

#include <getopt.h>
struct option {
    const char *name;
    int         has_arg;
    int        *flag;
    int         val;
};
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);

The getopt_long() function works like getopt() except that it also accepts long options, started with two dashes. A long option may take a parameter, of the form --arg=param or --arg param.

struct option 的成员意义如下:
name:长选项的名字。
has_arg:no_argument (or 0) 选项没有参数;required_argument (or 1) 选项有一个参数; optional_argument (or 2) 选项可有可无参数。
flag:标识结果如何返回。选项如果被找到,如果 flag 是 NULL,getopt_long() 将会返回 val,否则 getopt_long() 返回 0,而且 *flag 被设置为 val;选项如果未被找到,getopt_long() 返回 ‘?’,*flag(如果 flag 不为 NULL ) 保持不变。
val:标识返回值。

longopts 的最后一个元素必须 be filled with zeros。
如果 longindex 不是 NULL,*longindex 将被设置为长选项在 longopts 中的索引。

getopt_long_only() is like getopt_long(), but ‘-’ as well as “–” can indicate a long option. If an option that starts with ‘-’ (not “–”) doesn’t match a long option, but does match a short option, it is parsed as a short option instead.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值