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. ArcGIS server开发之API for js 本地部署

    ArcGIS Server for javascript 本地部署 第一次使用arcgis server for js开发,在经验方面还有很多的不足,所以将自己在开发过程中遇到的问题写出来与大家共享. ...

  2. jPList – 实现灵活排序和分页功能的 jQuery 插件

    jPList 是一个灵活的 jQuery 插件,可以用于任何 HTML 结构的排序,分页和筛选.它支持的数据源包括:PHP + MySQL,ASP.NET + SQL Server,PHP + SQL ...

  3. [deviceone开发]-大家比较关注的应用内部升级

    一.简介 这个示例详细介绍了应用内升级的步骤,应用内升级是开发App必须的功能.推荐初学者学习. 二.效果图 三.相关下载 https://github.com/do-project/code4do/ ...

  4. SharePoint 2013 日历视图兼容性问题

    在IE11上访问SharePoint 2013 calendar视图,发现加入兼容性视图以后访问,正常,如下图: 不加入兼容性视图IE11访问,出现兼容性问题,如下图: 因为有些环境有问题,有些环境没 ...

  5. Git 撤销修改

    Git 撤销修改 增补提交 git commit –C HEAD –a --amend -C表示复用指定提交的提交留言,这个例子中是HEAD,实际上可以指定其他有效的提交名称. 如果参数是小写的-c, ...

  6. Android Animation学习(三) ApiDemos解析:XML动画文件的使用

    Android Animation学习(三) ApiDemos解析:XML动画文件的使用 可以用XML文件来定义Animation. 文件必须有一个唯一的根节点: <set>, <o ...

  7. Android 手机卫士--解析json与消息机制发送不同类型消息

    本文地址:http://www.cnblogs.com/wuyudong/p/5900800.html,转载请注明源地址. 1.解析json数据 解析json的代码很简单 JSONObject jso ...

  8. 【读书笔记】iOS网络-使用Bonjour实现自组织网络

    Bonjour就是这样一种技术:设备可以通过它轻松探测并连接到相同网络中的其他设备,整个过程只需要很少的用户参与或是根本就不需要用户参与.该框架提供了众多适合于移动的使用场景,如基于网络的游戏,设备间 ...

  9. 苹果企业账号打包发布APP流程详解

    原文链接:http://www.cnblogs.com/mddblog/p/4718228.html 一.通过企业账号申请证书 1 Certificate Signing Request (CSR)文 ...

  10. iOS开发之JSON格式数据的生成与解析

    本文将从四个方面对IOS开发中JSON格式数据的生成与解析进行讲解: 一.JSON是什么? 二.我们为什么要用JSON格式的数据? 三.如何生成JSON格式的数据? 四.如何解析JSON格式的数据? ...