命令解析常用的函数getopt、getopt_long、getopt_long_only能够解析长短命令和带参数的命令
定义
头文件
#include <unistd.h>
#include <getopt.h>
/*param
argc:main()函数传递过来的参数的个数
argv:main()函数传递过来的参数的字符串指针数组
optstring:选项字符串,告知 getopt()可以处理哪个选项以及哪个选项需要参数
return
如果选项成功找到,返回选项字母;如果所有命令行选项都解析完毕,返回 -1;如果遇到选项字符不在 optstring 中,返回字符 '?';如果遇到丢失参数,那么返回值依赖于 optstring 中第一个字符,如果第一个字符是 ':' 则返回':',否则返回'?'并提示出错误信息。
*/
int getopt(int argc, char * const argv[], const char *optstring);
//getopt是用来解析命令行选项参数的,但是只能解析短选项: -d 100,不能解析长选项:--prefix
optstring的格式意义
char*optstring = “x:b::d:j”;
单个字符j 表示选项j没有参数 格式:-j即可,不加参数
单字符加冒号x: 表示选项x有且必须加参数 格式:-x 100或-x100
单字符加2冒号b:: 表示选项b可以有,也可以无 仅有的格式:-b200
optarg —— 指向当前选项参数的指针。
optind —— argv未处理的下一个参数的索引,首次调用getopt,optind值为1
optopt —— getopt出现错误时,返回的错误选项字符
opterr —— 如果不希望getopt()打印出错信息,则只要将全域变量opterr设为0即可。
eg
#include <unistd.h>
#include <stdio.h>
#include <getopt.h>
int main(int argc, char *argv[])
{
char *string = "x:b::d:j";
int ret = 0;
if(argc <= 1)
printf("please input param\n");
while((ret = getopt(argc, argv, string)) != -1)
{
printf("ret = %c\t\t", ret);
printf("optarg = %s\t\t", optarg);
printf("optind = %d\t\t", optind);
printf("argv[optind] = %s\n", argv[optind]);
}
return 0;
}
测试:
ss@compile-etherLab:~/pass/c$ ./opt -x 200 -b100 -d 20 -j
ret = x optarg = 200 optind = 3 argv[optind] = -b100
ret = b optarg = 100 optind = 4 argv[optind] = -d
ret = d optarg = 20 optind = 6 argv[optind] = -j
ret = j optarg = (null) optind = 7 argv[optind] = (null)
getopt_long比起getopt除了能够解析长参数之外也能够解析短参数
#include <getopt.h>
struct option {
char *name; //选项参数的name
int has_arg; //选项是否带参 no_argument(or 0):选项不带参
required_argument:选项带一个参数
optional_argument:选项可带参数或者不带
int *flag; //决定getopt_long函数的返回值 NULL:返回val值
指向一个变量:返回0 指向的变量的值 = val
int val; //如果flag = NULL getopt_long返回val值
如果flag = &a getopt_long返回0 a = val
}
int getopt_long(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
optstring: NULL:长参数选项
longopts: option 数组
int getopt_long_only(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
eg
#include <unistd.h>
#include <stdio.h>
#include <getopt.h>
struct option demo_options[] = {
{
"mon", required_argument, NULL, 'p'},
{
"day", no_argument, NULL, 'n'},
{
"month", optional_argument, NULL, 'd'},
{
NULL, 0, NULL, 0},
};
int main(int argc, char *argv[])
{
char *string = ":x:b::d:j";
int ret = 0;
int index = 0;
if(argc <= 1)
printf("please input param\n");
while((ret = getopt_long(argc, argv, string, demo_options, &index)) != -1)
{
printf("ret = %c\t\t", ret);
printf("optarg = %s\t\t", optarg);
printf("optind = %d\t\t", optind);
printf("argv[optind] = %s\n", argv[optind]);
printf("optopt = %d\n", optopt);
}
return 0;
}
@compile-etherLab:~/pass/c$ ./getloog --mon 52 --day --month 45
ret = p optarg = 52 optind = 3 argv[optind] = --day
optopt = 0
ret = n optarg = (null) optind = 4 argv[optind] = --month
optopt = 0
ret = d optarg = (null) optind = 5 argv[optind] = 45
optopt = 0
用短参数也是可以的
@compile-etherLab:~/pass/c$ ./getloog -x 100 -b200 -d100 -j
ret = x optarg = 100 optind = 3 argv[optind] = -b200
optopt = 0
ret = b optarg = 200 optind = 4 argv[optind] = -d100
optopt = 0
ret = d optarg = 100 optind = 5 argv[optind] = -j
optopt = 0
ret = j optarg = (null) optind = 6 argv[optind] = (null)
optopt = 0
文章浏览阅读378次。他,传统企业的爆品王,造酒、制糖、产烟,种橙子,干什么都是最好的。他,影响企业家的企业家,他的故事和创业精神,深深影响了中国企业界包括柳传志、王石等一些大佬,以及无数要为明天而奋斗的年轻人。他,就是褚时健。褚时健,这个曾被报告文学形容为像太阳一样灿烂的男人,淡然外表下的内心,似乎没有一个人能触碰到。观其容,听其语,你也许读不出跌宕起伏的人生,看不到在老人温暖笑容中刻下的沧桑,但一定不会忽略那亲自铸_90岁了,褚时健罕见反思
文章浏览阅读114次。算法特训营本周内容:1. 录播视频:树状数组,二维树状数组。2. 直播刷题题目:POJ2352、POJ3067、POJ3321、POJ1195。友情提示:以下是直播刷题链接(收费),不需要看直播请忽略。【直播地址】https://www.epubit.com/courseDetails?id=PCCbf16b01a6788&recommenderCode=1540556欢迎大家一起刷题。...
文章浏览阅读3.6k次,点赞4次,收藏14次。地球微生物组的基因组集A genomic catalog of Earth’s microbiomesNature Biotechnology [IF:36.558]2020-11-09..._ani 新属
文章浏览阅读889次,点赞2次,收藏2次。cousera 上algorithm part I第三周课程讲述的是排序,包括插入排序、选择排序、希尔排序、归并排序和快速排序。其配套作业为Collinear Points,题目大意为给定若干点,求出其中的有四个及以上点共线的线段。要求提交三个文件,Point.java,BruteCollinearPoints.java,FastCollinearPoints.java。Point类给定的的..._algorithm i collinear points
文章浏览阅读7.7k次。如果a的name和页面中某个元素的id同名的话,在Safari、Chrome浏览器中会跳到id元素的位置,在IE中则会跳到a元素的位置可以使用jQuery的haschange事件来侦听浏览器点击后退时的hash变化的事件.$(window).bind('hashchange', function () { //});不过以上方案在IE浏览器只能支持到IE8_$(window).bind('hashchange',
文章浏览阅读6.8k次,点赞2次,收藏2次。含有斜杠的字符串 中的 斜杠 替换为 反斜杠... string a = "X:\Data Backup\UnityProjects\TestAssetBundle\Assets";//Application.dataPath a = a.Replace("\\", "/");...显示结果X:/Data Backup/UnityProjects/TestAssetBundle/Assets..._c# 替换斜杠
文章浏览阅读2.4w次,点赞15次,收藏91次。Python中如何使用matplotlib给柱状图添加数据标签(bar_label()) 本文主要记录如何用使用matplotlib给柱状图添加数据标签,是以matplotlib.pyplot.bar_label()为例。目录Python中如何使用matplotlib给柱状图添加数据标签(bar_label())0.更新matplotlib库1.导入库2.数据准备3.绘制柱状图4.绘图结果5.完整代码6.bar_label()相关参数的补充说明7.参考文献0.更新matplotlib库 _matplotlib柱状图添加标签
文章浏览阅读102次。*****************************************************************在这门课里你将学到Web Services(SOAP WebService和REST API)的手动测试及自动化测试,熟练使用Groovy脚本自动化测试WebService。这门课程设计的是从零基础入门开始学,然后以循序渐进的方式提升到高级水平,不需要在学习课程之前有任..._testng 可以提供soap
文章浏览阅读1.4k次。1. 创建命名空间,创建kubeless 控制管理容器>kubectl create ns kubeless#自行安装方便切换空间的kubens>kubens kubeless#根据官方提供的yaml ,创建Kubeless Controller Manager容器:>kubectl create -f https://github.com/kubeless/kubeless/releases/download/v1.0.7/kubeless-non-rbac-v1.0..._kubeless
文章浏览阅读154次。Eclipse 在Ubuntu 下总是感觉上面的工具栏感觉特别的大,控件之间的空隙非常的大,和在Windows 下的感觉非常的不一样(毕竟是刚刚从windows叛逃出来),其实也不光光是Eclipse 是这样,其他也软件也同样有这个问题。尝试过通过更换主题来解决这样的问题,老是看着一个主题,审美总是会疲劳的。在网上找来一圈,解决方案:修改或者新建(系统默认是没有的)/home/Your_usern..._eclipse toolbar颜色
文章浏览阅读1.1k次。测验2: Python基本图形绘制 (第2周)单项选择题1、哪个选项不能正确引用turtle库进而使用setup()函数?A、import turtle as tB、import setup from turtleC、from turtle import*D、import turtle正确答案 Bimport只有三种使用方法,以turtle库为例:import turtlefrom turtle ..._00390037003900301688536597255哪个选项不能正确引用turtle库进而使用setup()函数
文章浏览阅读343次。说实话,刚开始想简单了,只考虑了每个数的最后一位,但是没想到还能因式分解,每个数的因子里的2的个数和5的个数需要统计一下,因为2*5==0#include<stdio.h>#include<queue>#include<math.h>#include<map>#include<iostream>#include<string>#include<algorithm>#include<sstream>._乘积尾零思想