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)

最新文章

  1. elasticsearch之python备份
  2. ZooKeeper与Curator注册和监控
  3. C#基础系列:开发自己的窗体设计器(PropertyGrid显示中文属性名)
  4. svn 设置
  5. iOS的常见文件及程序的启动原理
  6. 流程控制语句和增强for循环
  7. MongoDB入门分享-笔记整理精选
  8. Animation动画(一)
  9. input里面check 状态检测
  10. 自增运算a++和++b(1)
  11. 关于内层DIV设置margin-top不起作用的解决方案
  12. SQL in优化将In转化为联合查询
  13. Problem D: 来开个书店吧
  14. unity中调用其他脚本函数的方法(小白之路)
  15. angular路由详解一(基础知识)
  16. tensorflow调试tfdbg
  17. hibernate框架学习之二级缓存(测试用例)
  18. CSS外观属性
  19. nginx与apache的参考配置
  20. 什么是JIT,写的很好

热门文章

  1. Java2_JDK的安装和配置
  2. sql2008“备份集中的数据库备份与现有的xxx数据库不同”解决方法
  3. MediaWiki使用指南
  4. sharepoint列表如何进行随机取几条记录?
  5. android加固系列—5.加固前先学会破解,hook(钩子)jni层系统api
  6. 安卓开发_慕课网_Fragment实现Tab(App主界面)
  7. Android 4.4沉浸式状态栏的实现
  8. UIScrollView之轮转图片
  9. IT软件开发常用英语词汇
  10. 安装centos 7后恢复windows 2008R2 (Windows7)启动项