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 ...
随机推荐
- 一款经典的jQuery kxbdMarquee 无缝滚动插件
<marquee> 曾是 IE 下独有的一个走马灯效果的标签,其他浏览器并不兼容,于是出现了使用 JavaScript 来模拟该效果的插件. 版本: jQuery v1.3.2+ 在线实例 ...
- Hadoop出现 native snappy library not available: SnappyCompressor has not been loaded的解决办法
我目前测试环境是Hadoop 2.7.1, 搭建群集之后经常出现错误“native snappy library not available: SnappyCompressor has not bee ...
- setTimeout和setInterval
setTimeout(methodName, interval); //间隔时间单位为毫秒,表示interval毫秒后执行方法methodName setInterval(methodName, in ...
- markdown 使用
一:markdown编辑器下载 小书匠 http://soft.xiaoshujiang.com/ 在线编辑工具,所写即所见 作业部落 https://www.zybuluo.com/mdeditor ...
- html 常用的标签
1.html 的基本格式 <html> <head> <meta charset="UTF-8"> <title>HTML5的标题& ...
- android studio 使用SVN 锁定文件,防止别人修改(基于Android studio 1.4 )
首先假设开发 A , 和 开发 B , 在使用 SVN 进行项目管理.那么A如何才能 某个锁定文件,防止B修改. 1.第一步,给这个文件加锁 完成这一步,则这个文件就别锁定了. 2.第二步,假如 ...
- 2016最新CocoaPods安装和错误解决 + 自己的经验
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px Times; color: #333333; background-color: #fffff ...
- (20160602)开源第三方学习之SDWebImage
这个类库提供一个UIImageView类别以支持加载来自网络的远程图片.具有缓存管理.异步下载.同一个URL下载次数控制和优化等特征. 地址:https://github.com/rs/SDWebIm ...
- iOS 开发之路(使用WKWebView加载Html5) 四
基于Swift 3 . Xcode 8 . iOS 10 下的WKWebView的使用. 首先是WKWebView的基本用法: var wk:WKWebView! var progBar:UIProg ...
- iOS中的小知识点
1.tableView隐藏滚动条 self.tableView.showsVerticalScrollIndicator = NO; 2.关于属性 使用assign: 对基础数据类型 (NSI ...