Use getopt() & getopt_long() to Parse Arguments
Today I came across a function [getopt] by accident.
It is very useful to parse command-line arguments with this tool!
Here is:
#inlcude <unistd.h>
int getopt(int argc, char * const argv[], const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
#include <getopt.h>
int getopt_long(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
int getopt_long_only(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
Explain for getopt():
- Arguments argc and argv are the argument count and array as passed to the main() function.
- optstring is the options set. One semicolon following a character means that character need a argument, and two semicolon mean the argument is optional.
- optind is the index of the next element to be processed in argv. The system initializes this value to 1. You can reset it to 1 to restart scanning of the same argv, or when scanning a new argument vector.
- If there are no more option characters, getopt() returns -1 or, it will keep return the ASCII of the current character.
- optarg points to the argument string of one option if that option has argument following.
E.g.
#include <unistd.h>
#include <stdio.h>
#include <stdbool.h> // bool type
/* Macro Definition */
#define NO 0
#define YES 1
#define OK 0
#define ERROR -1
/* Structure for Global Arguments */
struct GlbArgs_t{
bool version;
bool help;
char *number;
char *type;
}GlbArgs;
/* Options allowed */
static const char *optString = "n:t::vh?";
/* Usage */
static const char *usage = "Usage: \n"
"-n\n"
"-tx (x=type-name) \n"
"-v\n"
"-h -?\n";
/* Initialize Global Argument structure */
void InitGlbArg()
{
GlbArgs.version = NO;
GlbArgs.help = NO;
GlbArgs.number = NULL;
GlbArgs.type = NULL;
}
int main(int argc, char **argv)
{
int opt = 0; // option
InitGlbArg();
while((opt = getopt(argc, argv, optString)) != ERROR){
switch(opt){
case 'n':
GlbArgs.number = optarg;
printf("Number: %s\n", GlbArgs.number);
break;
case 't':
GlbArgs.type = optarg;
printf("Type: %s\n", GlbArgs.type);
break;
case 'v':
GlbArgs.version = YES;
printf("Version: 1.0\n");
break;
case 'h':
case '?':
printf("%s", usage);
break;
default:
break;
}
}
return OK;
}
- Learn a lot from here
- For more details, refer to [man 3 getopt] (LInux)
Use getopt() & getopt_long() to Parse Arguments的更多相关文章
- parse arguments in bash
There are lots of ways to parse arguments in sh. Getopt is good. Here's a simple script that parses ...
- 函数参数选项的处理getopt getopt_long getopt_long_only
转载:http://blog.chinaunix.net/uid-20321537-id-1966849.html 在头文件中int getopt(int argc,char *argv[], c ...
- C语言命令行解析函数:getopt/getopt_long
命令行工具下的参数选项有两种,长选项和短选项.短选项以-开头,后面跟单个字母:长选项以--开头,后面可跟多个字母. 一. getopt() 1.功能:解析命令行短选项参数 2.函数原型: #inclu ...
- 命令行解析函数:getopt/getopt_long
参考: http://blog.csdn.net/zhangyang0402/article/details/5671410 http://www.cnblogs.com/gnuhpc/archive ...
- getopt getopt_long
getopt_long支持长选项的命令行解析,使用man getopt_long,得到其声明如下: #include <getopt.h> int getopt_long(int argc ...
- 命令行参数解析:getopt,getopt_long
#include <unistd.h> int getopt(int argc, char * const argv[], const char *optstring); extern c ...
- getopt() getopt_long()函数手册[中文翻译]
getopt()函数 getopt_long函数 函数原型(function prototype) #include <unistd.h> int getopt(int argc, cha ...
- apue编程之getopt ,getopt_long使用方法以及实例
1.getopt 1.1 函数定义 int getopt(int argc, char * const argv[], const char *optstring);#include <unis ...
- 命令行解析函数:getopt_long、getopt
一.前言 在学习一些项目代码时,尤其涉及到命令行传参的代码,经常遇到getopt相关的函数,对这一类函数可以说是既陌生又熟悉.陌生是因为不知道它是干啥的,熟悉呢,是因为经常遇到.于是乎在追踪了多天ip ...
随机推荐
- 钉钉客户端JS-API权限签名算法.NET版
前段时间写了一篇博文<钉钉如何进行PC端开发>,在里面并未解决本地生成签名的问题,需要到官网进行生成,由于钉钉门票等认证信息会超期,因此,必须能本地用代码自动更新相关参数信息,来换取签名. ...
- 【HTML5】浅析HTML5应用程序缓存(ApplicationCache)
一.为什么需要Web应用程序缓存 在移动互联网时代,设备终端位置不再固定,依赖无线信号,网络的可靠性变得降低,比如坐在火车上,过了一个隧道(15分钟),便无法访问网站,这对于web的伤害是很大的 ...
- JavaScript 事件绑定及深入
一.传统事件绑定的问题 解决覆盖问题,我们可以这样去解决:window.onload = function () { //第一个要执行的事件,会被覆盖 alert(1);};if (typeof wi ...
- #8.10.16总结# 属性选择符 伪对象选择符 CSS的常用样式
属性选择符 E[att] E[att="val"] E[att~="val"] E[att^="val"] E[att$="val ...
- IE中的条件注释(转载自网络)
IE条件注释是微软从IE5开始就提供的一种非标准逻辑语句,作用是可以灵活的为不同IE版本浏览器导入不同html元素,如:样式表,html标签等.很显然这种方法的最大好处就在于属于微软官方给出的兼容解决 ...
- 关于web软件信息安全问题防护资料的整理(三)
了解了web系统的安全威胁,那么我们应该怎样防范这些安全威胁呢? 1.时刻准备应战 Web应用系统所面临的威胁是非常严峻的.不管攻击的一方是采用单一形式的攻击,还是采用混合多种手段的混合攻击,作为防护 ...
- 系统补丁对sharepoint很重要
系统补丁对sharepoint很重要,会提高sharepoint运行效率,加载速度明显变快.
- 通过API找出Autodesk Vault中某个用户组可以访问的Vault
首先在Vault Explorer中可以这样查看和更改某个用户组有权访问的vault Tools –> Administration –> Global Settings –> Gr ...
- Troubleshooting FIM: (No Display Name) in FIM Portal
from: http://social.technet.microsoft.com/wiki/contents/articles/12682.troubleshooting-fim-no-displa ...
- 10个学习Android开发的网站推荐
1. Android Developers 作为一个Android开发者,官网的资料当然不可错过,从设计,培训,指南,文档,都不应该错过,在以后的学习过程中慢慢理解体会. 2. Android Gui ...