下面资料来自百度百科:
getopt(分析命令行参数
相关函数
表头文件 #include<unistd.h>
定义函数 int getopt(int argc,char * const argv[ ],const char * optstring);
extern char *optarg;
extern int optind, opterr, optopt;
函数说明 getopt()用来分析命令行参数。参数argc和argv是由main()传递的参数个数和内容。参数 optstring为选项字符串, 告知 getopt()可以处理哪个选项以及哪个选项需要参数,如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数,全域变量optarg 即会指向此额外参数。如果在处理期间遇到了不符合optstring指定的其他选项getopt()将显示一个错误消息,并将全域变量optarg设为“?”字符,如果不希望getopt()打印出错信息,则只要将全域变量opterr设为0即可。
getopt() 所设置的全局变量包括:
optarg——指向当前选项参数(如果有)的指针。 optind——再次调用 getopt() 时的下一个 argv 指针的索引。 optopt——最后一个已知选项。
补充说明下optstring中的指定的内容的意义(例如getopt(argc, argv, "ab:c:de::");
1.单个字符,表示选项,(如上例中的abcde各为一个选项)
2.单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。(如上例中的b:c:)
3 单个字符后跟两个冒号,表示该选项后必须跟一个参数。参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。(如上例中的e::)
范例 #include<stdio.h>
#include<unistd.h>
int main(int argc,char **argv)
{
int ch;
opterr = 0;
while((ch = getopt(argc,argv,”a:bcde”))!= -1)
switch(ch)
{
case ‘a’: printf(“option a:’%s’\n”,optarg); break;
case ‘b’: printf(“option b :b\n”); break;
default: printf(“other option :%c\n”,ch);
}
printf(“optopt +%c\n”,optopt);
}
执行 $./getopt –b
option b:b
执行 $./getopt –c
other option:c
执行 $./getopt –a
other option :?
执行 $./getopt –a12345
option a:’12345’
 
 
 
 
在做escrow的时候遇到例子为:
int
main(int argc, char*argv[])
{
// Initialized the system running environment.
initEnvironment(); // Class initialized here
int op;
while ((op = getopt(argc, argv, "futred")) != -)
{
switch (op)
{
case 'f':
gp_dataProcess = new FullDataProcess();
generateFile(gp_dataProcess,"full");
break;
case 'u':
gp_dataProcess = new UrgentDataProcess();
generateFile(gp_dataProcess,"full");
break;
case 't':
gp_dataProcess = new ThinDataProcess();
generateFile(gp_dataProcess,"thin");
break;
case 'd':
gp_dataProcess = new DiffDataProcess();
generateFile(gp_dataProcess,"diff");
break;
//#ifdef WangRuiqing
//full类型的恢复
case 'r':
gp_dataProcess = new FullDataProcess();
recoverGenerateFile(gp_dataProcess,"full"); printf("test\n");
break;
//diff类型的恢复
case 'e':
// gp_dataProcess = ;
break;
//#endif
case '?':
default:
usage();
}
} // system resource clean up;
cleanUp(); return ;
}
 

转载:linux编程,命令行参数输入getopt的更多相关文章

  1. [转]Python 命令行参数和getopt模块详解

    FROM : http://www.tuicool.com/articles/jaqQvq 有时候我们需要写一些脚本处理一些任务,这时候往往需要提供一些命令行参数,根据不同参数进行不同的处理,在Pyt ...

  2. Python 命令行参数和getopt模块详解

    有时候我们需要写一些脚本处理一些任务,这时候往往需要提供一些命令行参数,根据不同参数进行不同的处理,在Python里,命令行的参数和C语言很类似(因为标准Python是用C语言实现的).在C语言里,m ...

  3. 命令行参数解析函数 getopt

    命令行参数解析函数 —— getopt() getopt()函数声明如下: #include <unistd.h> int getopt(int argc, char * const ar ...

  4. shell 命令行参数(getopt和getopts)

    getopt 命令 使用getopt命令,可以解析任何命令行选项和参数,但是用法比较复杂.getopt的命令用法如下: $ getopt --help 用法: getopt optstring par ...

  5. 命令行参数处理-getopt()和getopt_long()

    在实际编程当中,自己编写代码处理命令行参数是比较麻烦且易出错的.一般我们会直接使用getopt()和getopt_long()函数,下文将介绍具体的使用方法. getopt() getopt()用于处 ...

  6. Linux进程-命令行参数和环境列表

    命令行参数 在C中,main函数有很多的变种,比如 main(), int main(), int main(int argc, char *argv[]), int main(int argc, c ...

  7. 命令行参数解析函数getopt和getopt_long函数【转】

    原文地址:http://blog.csdn.net/cashey1991/article/details/7942809 getopt和getopt_long函数   平时在写程序时常常需要对命令行参 ...

  8. 【C】命令行参数解析——getopt、getopt_long及getopt_long_only

    前言 在linux下学习开源代码Webbench,遇到get_long等函数的用法,一时有点懵,故想深入了解这类命令行解析函数,并记此博文. 1.getopt getopt主要用来处理短命令行选项,例 ...

  9. 关于Arch Linux efibootmgr 命令行参数问题

    相关链接: https://wiki.archlinux.org/index.php/EFISTUB 今天安装Arch Linux 在 efibootmgr创建启动项时,总是提示 UUID=xxxx ...

随机推荐

  1. JDBC-防止SQL注入问题

      String sql = "select * from user where name = '" + name + "' and password = '" ...

  2. python入门:输出1-10以内除去7的所有数(自写)

    #!/usr/bin/env python # -*- coding:utf-8 -*- #输出1-10以内除去7的所有数(自写) """ 变量kaishi赋值等于1,w ...

  3. 【java】 java 内存解读

    具体请参考 vamei java 内存管理和垃圾回收 java的内存分为栈内存和堆内存两部分 栈内存 主要存储一些参数,局部变量和返回地址,参数和局部变量大部分是基本类型的变量,如果是引用类型,实际上 ...

  4. python爬虫基础17-抓包工具使用

    01 抓包工具原理 HTTP 由于HTTP请求是没有加密的,也没有做任何验证,所以抓包工具直接将请求转发即可. HTTPS 由于HTTPS请求,客户端会使用服务端的证书来加密数据,而且会验证服务端是否 ...

  5. python并发编程之进程1(守护进程,进程锁,进程队列)

    进程的其他方法 P = Process(target=f,) P.Pid 查看进程号  查看进程的名字p.name P.is_alive()  返回一个true或者False P.terminate( ...

  6. sqli-labs less1 &&less3&&less4学习心得

    0x01.less1 id=1/ id=1 and 1=1结果正常 id=1 and 1=2结果正常,不合理 id=1'提示:

  7. ACM-ICPC 2018 徐州赛区网络预赛 F. Features Track

    262144K   Morgana is learning computer vision, and he likes cats, too. One day he wants to find the ...

  8. Linux学习-systemctl 针对 timer 的配置文件

    如何使用 systemd 内建的 time 来处理各种任务呢? systemd.timer 的优势 在 archlinux 的官网 wiki 上面有提到,为啥要使用 systemd.timer 呢? ...

  9. HDU 2852 KiKi's K-Number 主席树

    题意: 要求维护一个数据结构,支持下面三种操作: \(0 \, e\):插入一个值为\(e\)的元素 \(1 \, e\):删除一个值为\(e\)的元素 \(2 \, a \, k\):查询比\(a\ ...

  10. MySQL基本命令和常用数据库对象

    MySQL基本命令: 连接远程主机的MySQL服务(为了保证安全性,执行下面命令时,可以省略-p后面的密码,执行命令后系统会提示输入密码) mysql -p 密码 -u 用户名 -h 主机地址 --d ...