getopt函数的用法
Linux提供了一个解析命令行参数的函数。
#include <unistd.h>
int getopt(int argc, char * const argv[],
const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
使用这个函数,我们可以这样运行命令
./a.out -n -t 100
n后面不需要参数,t需要一个数值作为参数。
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#define ERR_EXIT(m) \
do { \
perror(m);\
exit(EXIT_FAILURE);\
}while(0) int main(int argc, char *argv[])
{
int opt;
while(1)
{
opt = getopt(argc, argv, "nt:");
if(opt == '?')
exit(EXIT_FAILURE);
if(opt == -1)
break; switch(opt)
{
case 'n':
printf("AAAAAAAA\n");
break;
case 't':
printf("BBBBBBBB\n");
int n = atoi(optarg);
printf("n = %d\n", n);
}
}
return 0;
}
当输入非法参数时,getopt返回’?’,当解析完毕时,返回-1.
如果需要参数,那么使用optarg获取,这是一个全局变量。
注意getopt的第三个参数”nt:”,说明可用的参数有n和t,t后面有一个冒号,说明t需要额外的参数。
运行结果如下:
5:30:22 wing@ubuntu msg ./getopt_test -n
AAAAAAAA
5:30:26 wing@ubuntu msg ./getopt_test -t
./getopt_test: option requires an argument -- 't'
5:30:31 wing@ubuntu msg ./getopt_test -t 100 1 ↵
BBBBBBBB
n = 100
getopt函数的用法的更多相关文章
- C语言中getopt()和getopt_long()函数的用法
一.参考文章 1.C语言中getopt()和getopt_long()函数的用法 2.linux 中解析命令行参数 (getopt_long用法) 二.调试经验
- getopt函数用法
getopt被用来解析命令行选项参数. #include <unistd.h> extern char *optarg; //选项的参数指针 extern int o ...
- getopt()和getopt_long()用法
参考链接: http://home.eeworld.com.cn/home.php?mod=space&do=blog&id=43897&uid=101752 http://b ...
- getopt函数的使用——分析命令行参数
getopt(分析命令行参数) getopt(分析命令行参数) 短参数的定义 返回值 范例 getopt_long 相关函数表头文件#include<unistd.h> 函数声明int g ...
- Python中getopt()函数的使用
在运行程序时,可能需要根据不同的条件,输入不同的命令行选项来实现不同的功能.目前有短选项和长选项两种格式.短选项格式为"-"加上单个字母选项:长选项为"--"加 ...
- 有关日期的函数操作用法总结,to_date(),trunc(),add_months();
相关知识链接: Oracle trunc()函数的用法 oracle add_months函数 Oracle日期格式转换,tochar(),todate() №2:取得当前日期是一个星期中的第几天,注 ...
- Oracle to_date()函数的用法
Oracle to_date()函数的用法 to_date()是Oracle数据库函数的代表函数之一,下文对Oracle to_date()函数的几种用法作了详细的介绍说明,供您参考学习. 在Orac ...
- js中bind、call、apply函数的用法
最近一直在用 js 写游戏服务器,我也接触 js 时间不长,大学的时候用 js 做过一个 H3C 的 web的项目,然后在腾讯实习的时候用 js 写过一些奇怪的程序,自己也用 js 写过几个的网站.但 ...
- Oracle trunc()函数的用法
Oracle trunc()函数的用法 /**************日期********************/1.select trunc(sysdate) from dual --2013-0 ...
随机推荐
- 使用System.getProperty("line.separator")时没有换行问题解决
项目中要实现替换模版txt文本里面的内容,然后生成新的文档,其中先把模版文本的内容通过创建的 BufferedReader bufReader 使用 readLine() 来一行一行读取,所以在完成替 ...
- poj 1654(利用叉积求面积)
Area Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17937 Accepted: 4957 Description ...
- RESTful 接口实现简明指南
REST 简介 REST 是一个术语的缩写,REpresentational State Transfer,中文直译「表征状态转移」,这是个很拗口的词.我的建议是先不要强行理解,直接看怎么做,等对实施 ...
- yii2.0在model里自定义数据表
无需多言,直接撸代码 class Zhuanjia extends \yii\db\ActiveRecord { public static function tableName() { return ...
- Nodejs微信开发
因为使用了Bot Framework开发了一个小功能,它目前支持了Skype\Teams\Slack等,但在国内来讲,微信还是一个比较流行的软件,所以需要接上微信 原来开发Bot的时候使用的是.Net ...
- uiautomator2 +Python进行Android原生应用UI测试
uiautomator2封装了google的uiautomator(只能用java),uiautomator2可以使用脚本语言python进行编写,更简单直观地修改.运行自动化测试代码: 不足为:仅支 ...
- Codeforces #480 Tutorial
Problem A,B,C: 简单的模拟,注意A中p mod q时对q=0特殊处理(注意范围) Problem D: Brief Intro: 给定长度为N的数组A,将A中所有连续子序列分成最少的组, ...
- 【树链剖分】【函数式权值分块】bzoj1146 [CTSC2008]网络管理Network
裸题,直接上.复杂度O(n*sqrt(n)*log(n)). //Num[i]表示树中的点i在函数式权值分块中对应的点 //Map[i]表示函数式权值分块中的点i在树中对应的点 #include< ...
- 【匈牙利算法模板】BZOJ1191-超级英雄
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...
- 前端基础-HTML简介及发展史
一 HTML简介 二 HTML发展史 一. HTML简介 用户使用浏览器打开网页看到结果的过程就是:浏览器将服务端的文本文件(即网页文件)内容下载到本地,然后打开显示的过程. 而文本文件的文档结构只有 ...