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函数的用法的更多相关文章

  1. C语言中getopt()和getopt_long()函数的用法

    一.参考文章 1.C语言中getopt()和getopt_long()函数的用法 2.linux 中解析命令行参数 (getopt_long用法) 二.调试经验

  2. getopt函数用法

    getopt被用来解析命令行选项参数. #include <unistd.h>      extern char *optarg;  //选项的参数指针      extern int o ...

  3. getopt()和getopt_long()用法

    参考链接: http://home.eeworld.com.cn/home.php?mod=space&do=blog&id=43897&uid=101752 http://b ...

  4. getopt函数的使用——分析命令行参数

    getopt(分析命令行参数) getopt(分析命令行参数) 短参数的定义 返回值 范例 getopt_long 相关函数表头文件#include<unistd.h> 函数声明int g ...

  5. Python中getopt()函数的使用

    在运行程序时,可能需要根据不同的条件,输入不同的命令行选项来实现不同的功能.目前有短选项和长选项两种格式.短选项格式为"-"加上单个字母选项:长选项为"--"加 ...

  6. 有关日期的函数操作用法总结,to_date(),trunc(),add_months();

    相关知识链接: Oracle trunc()函数的用法 oracle add_months函数 Oracle日期格式转换,tochar(),todate() №2:取得当前日期是一个星期中的第几天,注 ...

  7. Oracle to_date()函数的用法

    Oracle to_date()函数的用法 to_date()是Oracle数据库函数的代表函数之一,下文对Oracle to_date()函数的几种用法作了详细的介绍说明,供您参考学习. 在Orac ...

  8. js中bind、call、apply函数的用法

    最近一直在用 js 写游戏服务器,我也接触 js 时间不长,大学的时候用 js 做过一个 H3C 的 web的项目,然后在腾讯实习的时候用 js 写过一些奇怪的程序,自己也用 js 写过几个的网站.但 ...

  9. Oracle trunc()函数的用法

    Oracle trunc()函数的用法 /**************日期********************/1.select trunc(sysdate) from dual --2013-0 ...

随机推荐

  1. ajax邮箱、用户名唯一性验证

    <script type="text/javascript"> $(function () { $("#txtEmail").blur(functi ...

  2. AC日记——[SHOI2008]小约翰的游戏John bzoj 1022

    1022 思路: nim: 代码: #include <cstdio> #include <cstdlib> #include <iostream> #includ ...

  3. js 之面向对象

    对象 是什么? 对象就是一个整体,对外提供一些操作,比如:电视机(不用知道内部的构造,只知道怎么用) 面向对象 是什么? 使用对象时,只关注对象提供的功能,不关心内部细节,比如:操作电视机(只知道用遥 ...

  4. 前端读者 | 别人写的css,你敢用吗?

    本文来自@yeaseonzhang:链接:http://yeaseonzhang.github.io/2018/04/10/%E5%88%AB%E4%BA%BA%E5%86%99%E7%9A%84cs ...

  5. NYOJ 6.喷水装置(一)-贪心

    喷水装置(一) 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 现有一块草坪,长为20米,宽为2米,要在横中心线上放置半径为Ri的喷水装置,每个喷水装置的效果都会让以 ...

  6. (10)python 特殊方法

    一.构造方法 在类中定义构造函数 >>> class a: def __init__(self): self.age=42 >>> f=a() >>&g ...

  7. UVa1599 Ideal Path(双向bfs+字典序+非简单图的最短路+队列判重)

    题目大意: 对于一个n个房间m条路径的迷宫(Labyrinth)(2<=n<=100000, 1<=m<=200000),每条路径上都涂有颜色,颜色取值范围为1<=c&l ...

  8. uva10256

    uva10256 题意 平面上存在两种颜色的点,问是否存在一条直线,使得任取一个红点和一个蓝点都在直线的异侧,这条直线不经过任何点. 分析 对每种颜色的点求一发凸包,问题等价于判断两个多边形是否相交或 ...

  9. c++基础学习之string

    //学习使用string类 2013-10-18 lingc #include <iostream> #include <string>//include this head ...

  10. 概述struts,以及struts如何实现MVC架构的

    概述MVC体系结构? 答:MVC包括三类对象,model是应用对象,view是视图,controller是控制器,它定义用户界面对用户输入的响应方式. 在MVC体系中,模型通常被称为”业务逻辑”,是真 ...