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的更多相关文章

  1. parse arguments in bash

    There are lots of ways to parse arguments in sh. Getopt is good. Here's a simple script that parses ...

  2. 函数参数选项的处理getopt getopt_long getopt_long_only

    转载:http://blog.chinaunix.net/uid-20321537-id-1966849.html   在头文件中int getopt(int argc,char *argv[], c ...

  3. C语言命令行解析函数:getopt/getopt_long

    命令行工具下的参数选项有两种,长选项和短选项.短选项以-开头,后面跟单个字母:长选项以--开头,后面可跟多个字母. 一. getopt() 1.功能:解析命令行短选项参数 2.函数原型: #inclu ...

  4. 命令行解析函数:getopt/getopt_long

    参考: http://blog.csdn.net/zhangyang0402/article/details/5671410 http://www.cnblogs.com/gnuhpc/archive ...

  5. getopt getopt_long

    getopt_long支持长选项的命令行解析,使用man getopt_long,得到其声明如下: #include <getopt.h> int getopt_long(int argc ...

  6. 命令行参数解析:getopt,getopt_long

    #include <unistd.h> int getopt(int argc, char * const argv[], const char *optstring); extern c ...

  7. getopt() getopt_long()函数手册[中文翻译]

    getopt()函数 getopt_long函数 函数原型(function prototype) #include <unistd.h> int getopt(int argc, cha ...

  8. apue编程之getopt ,getopt_long使用方法以及实例

    1.getopt 1.1 函数定义 int getopt(int argc, char * const argv[], const char *optstring);#include <unis ...

  9. 命令行解析函数:getopt_long、getopt

    一.前言 在学习一些项目代码时,尤其涉及到命令行传参的代码,经常遇到getopt相关的函数,对这一类函数可以说是既陌生又熟悉.陌生是因为不知道它是干啥的,熟悉呢,是因为经常遇到.于是乎在追踪了多天ip ...

随机推荐

  1. jQuery cbpContentSlider 滑动切换

    cbpContentSlider是一款选项卡插件,只要按照以下html结构就可以自动生成菜单切换内容特效. 在线实例 实例演示 使用方法 <div id="cbp-contentsli ...

  2. jQuery fullPage.js 全屏滚动

    fullPage 是一款不依赖任何 js 库的全屏滚动组件,支持垂直/水平滚动.CSS3 旋转/缩放动画,支持 IE5.5+,支持移动设备. 在线实例 垂直滚动 水平滚动 CSS3 动画1 CSS3 ...

  3. 如何用.NET生成二维码?

    二维码是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的,国外对二维码技术的研究始于20世纪80年代末,在二维码符号表示技术研究方面已研制出多种码制,常见的有P ...

  4. PhotoSwipe - 移动开发必备的 iOS 风格相册

    PhotoSwipe 是一个专门针对移动设备的图像画廊,它的灵感来自 iOS 的图片浏览器和谷歌移动端图像. PhotoSwipe 提供您的访客熟悉和直观的界面,使他们能够与您的移动网站上的图像进行交 ...

  5. Windows服务器如何选 搭建WAMP环境

    Windows Server 2003 Windows Server 2008 如何选择服务器系统版本.原文地址:http://www.xwamp.com/learn/1. 系统版本: Windows ...

  6. javascript 对象初探 (四)--- 内建对象之旅之Array

     我们不要去纠结神马是内建对象,神马是內建构造器.到后来你们便会发现其实她们都是对象. Array()是一个构建数组的內建构造器函数: var arr = new Array(); 与下面的是等效的: ...

  7. SharePoint文档库,如何在新窗口打开中的文件

    默认情况下,点击文档库中的文件是在当前浏览器中打开的(如果你设置的是在客户端软件打开,则不符合本文情况).那么如果让他在新窗口中打开呢? 这里需要借助jQuery,关于如何将jQuery集成到Shar ...

  8. android MediaPlayer的一些使用方法简历

    这里提供一些MediaPlayer的简单方法,方便以后熟练的使用它! 1)如何获得MediaPlayer实例: 可以使用直接new的方式: MediaPlayer mp = new MediaPlay ...

  9. 你真的了解UIViewController生命周期吗?

    一:首先了解一下生命周期图 二:UIViewController 生命周期介绍 1.通过alloc init 分配内存,初始化controller. 2.loadView loadView方法默认实现 ...

  10. IOS客户端Coding项目记录(六)

    1:获取某一行的坐标 UITableViewCell *cell = [_myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow: ...