首页 技术 正文
技术 2022年11月14日
0 收藏 583 点赞 2,133 浏览 2152 个字

     getopt和getoptlong被用来解析命令行参数。 一、getopt

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

定义了四个全局变量:optarg是选项的参数指针,optind记录目前查找的位置,当opterr = 0时,getopt不向stderr输出错误信息。当命令选项字符不包括在optstring中或者缺少必要的参数时,该选项存储在optopt中,getopt返回 ‘?’

     getopt调用一次,返回一次选项,当检查不到参数时,返回-1,同时,optind储存第一个不包含选项的命令行参数。     optstring参数可以有以下元素。     1.单个字符:表示选项     2.单个字符后接一个冒号:表示该选项必须跟一个参数,参数紧跟在选项后,或以一个空格隔开,将参数的指针赋给optarg。     3.单个字符后跟两个冒号:表示该选项必须跟一个参数,且不能用空格隔开。例子:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>int main(int argc, char **argv)
{
int opt = ; while ((opt = getopt(argc, argv, "if:?lr::")) != -)
{
switch(opt)
{
case 'i':
case 'l':
printf("option: %c\n", opt);
break;
case 'f':
printf("filename: %s\n", optarg);
break;
case 'r':
printf("arg:%s\n", optarg);
break;
case ':':
printf("option needs a value\n");
break;
case '?':
printf("unknow option: %c\n", optopt);
break;
}
} for (; optind < argc; optind++)
{
printf("argument: %s\n", argv[optind]); ar return ;
}

     二、getoptlong     getoptlong就是添加了对长选项支持的getopt,getoptlong比getopt多两个参数,struct option *longopts和int *longindex,后者用到的少,一般置为NULL。直接来看struct option 结构

struct option
{
char *name;
int has_arg;
int *flag;
int val;
};

     name表示的是长参数名,has_arg有三个值,当has_arg = no_argument(即0)时,表示该参数后面不跟参数值,当has_arg = required_argument(即1)时,表示参数后面一定跟参数值,当has_arg = optional_argument(即2)时,表示该参数后面可跟参数值,也可不跟。flag   当这个指针为空的时候,函数直接将val的数值从getopt_long的返回值返回出去,当它非空时,val的值会被赋到flag指向的整型数中,而函数返回值为0 。 val    用于指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值。定义option試例:

struct option longopts[] =
{
{"initialize", , NULL, 'i'},
{"filename", , NULL, 'f'},
{"list", , NULL, 'l'},
{"restart", , NULL, 'r'}
};

例子:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>#define _GNU_SOURCE
#include <getopt.h>int main(int argc, char **argv)
{
int opt; struct option longopts[] =
{
{"initialize", , NULL, 'i'},
{"filename", , NULL, 'f'},
{"list", , NULL, 'l'},
{"restart", , NULL, 'r'}
}; while ((opt = getopt_long(argc, argv, ":if:lr", longopts, NULL)) != -)
{
switch(opt)
{
case 'i':
case 'l':
case 'r':
printf("option: %c\n", opt);
break;
case 'f':
printf("filename: %s\n", optarg);
break;
case ':':
printf("option needs a value\n");
break;
case '?':
printf("unknow option: %c\n", optopt);
break;
}
} for (; optind < argc; optind++)
{
printf("argument: %s\n", argv[optind]); } return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918