下面资料来自百度百科:
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. java 的多态(2013-10-11-163 写的日志迁移

    java 的多态性:(所谓多态--就是指一个引用(类型)在不同情况下的多种状态)   1.方法的多态:    重载(overload)   重写(覆盖 override)   2.对象的多态性:(本人 ...

  2. 在ArchLinux、manjaro中安装MySql(mariaDB)

    安装MySql数据库.但是在MySql被Oracle收购之后,很多开源支持者就转而使用MariaDb了.不过MariaDb也和MySql兼容的,所以基本不用有什么担心.由于ArchLinux只带了Ma ...

  3. LeetCode(123) Best Time to Buy and Sell Stock III

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  4. PTA 银行排队问题之单队列多窗口加VIP服务 队列+模拟

    假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙.当有窗口空闲时,下一位顾客即去该窗口处理事务.当有多个窗口可选择时,假设顾客总是选择编号最小的窗口. 有些银行会给 ...

  5. Linux编程中链接库的使用

    链接库本质上是一段可执行的二进制代码,可以被操作系统载入内存执行.按加载的时机不同,链接库可以分为静态链接库和动态链接库. 静态链接库:编译过程中加载进可执行文件的库(静态库省去了运行时加载的消耗,但 ...

  6. Android后台的linux一直保持唤醒状态,不进入睡眠

    由于要做Android手机的电池续航测试,是不能插usb的,所以把case放到sh文件中,之后push到手机里,执行的. 但是出现个问题,假如case中有很长时间的sleep操作,关闭手机屏幕,这样l ...

  7. 关于.net 项目 nuget包还原项目失败的记录

    在.net项目中,一般通过vs打开项目,会自动进行nuget包还原,可能因为其他一些因素,包还原失败,同时在之前包依赖是可以正常使用,现在却提示包 AutoMapper 6.2.2 与 netcore ...

  8. luogu2564 [SCOI2009]生日礼物

    排序枚举左端点,则右端点必定不降 #include <algorithm> #include <iostream> #include <cstring> #incl ...

  9. x86 保护模式 十 分页管理机制

    x86   保护模式  十  分页管理机制 8.386开始支持分页管理机制 段机制实现虚拟地址到线性地址的转换,分页机制实现线性地址到物理地址的转换.如果不启用分页,那么线性就是物理地址 一  分页管 ...

  10. [linux小技巧]批量移动文件

    for i in {1..23};do mv test$i/ ../;done