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 ...
随机推荐
- Strip JS – 低侵入,响应式的 Lightbox 效果
Strip 是一个灯箱效果插件,显示的时候只会覆盖部分的页面,这使得侵扰程度较低,并留出了空间与页面上的大屏幕,同时给予小型移动设备上的经典灯箱体验.Strp JS 基于 jQuery 库实现,支持 ...
- Web 前沿——HTML5 Form Data 对象的使用
XMLHttpRequest Level 2 添加了一个新的接口——FormData.利用 FormData 对象,我们可以通过 JavaScript 用一些键值对来模拟一系列表单控件,我们还可以使用 ...
- 计算DEM上的Profile图
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys, gdal, os from gdalconst import GA_ReadOnly ...
- Shapely中的几何图形操作
Geometric Objects object.area Returns the area (float) of the object. object.bounds Returns a (minx, ...
- 2013 Visual Studio Magazine读者选择奖界面框架类获奖情况
2013 Visual Studio Magazine读者选择奖已经正式揭晓了!据了解,截至今年此奖项已经评选了21次,非常值得.NET开发人员信赖和参考.此次评选共有400多个产品角逐28个分类的奖 ...
- 腾讯bugly团队提供的android国内镜像
腾讯bugly团队提供的国内镜像 如果使用Android SDK Manager下载比较慢或者打不开,可以使用国内镜像 使用说明 http://android-mirror.bugly.qq.co ...
- 让结构体类型frame的某个属性可以直接修改
本篇是是本人在博客园写的第一篇博客,前几天因为种种原因最终决定离开混了几年的csdn.希望在博客园有个新的开始 Foundation框架里面的frame是大家最熟悉不过的一个属性了,但是修改起来比较麻 ...
- Android studio 如何查看模拟器里面的文件
1.查看SD卡里面的内容 2.看数据库
- 如何去定义一个jquery插件
扩展jquery的时候.最核心的方法是以下两种: $.extend(object) 可以理解为jquery添加一个静态方法 $.fn.extend(object) 可以理解为jquery实例添加一个方 ...
- [转]android笔记--Intent和IntentFilter详解
Intent用于启动Activity, Service, 以及BroadcastReceiver三种组件, 同时还是组件之间通信的重要媒介. 使用Intent启动组件的优势1, Intent为组件的启 ...