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. 图标集锦:10套免费的社交媒体 & 社交网站图标

    社交网络是最近几年互联网领域最热门的关键词之一,如今社会网络化媒体也成为我们信息获取和传播的重要途径,很多网站都有把内容分享到社交媒体的功能. 社交媒体图标作为向用户传递信息的重要媒介,不管是在网页还 ...

  2. ae_将面积小于1500的Feature同附近Feature进行合并

    private void 合并1500图斑ToolStripMenuItem_Click(object sender, EventArgs e) { /* *将图层中面积小于1500的图斑与之相同BS ...

  3. html前端总结

    1.设置图片的最大宽高的css<style> .img-max { max-height:50px; width:expression(document.body.clientHeight ...

  4. AVAudioPlayer播放并实现了后台播放和远程控制

    // ViewController.h #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @class ...

  5. 操作系统开发系列—4.LDT

    一直以来,我们把所有的段描述符都放在GDT中,而不管它属于内核还是用户程序,为了有效地在任务之间实施隔离,处理器建议每个任务都应当具有自己的描述符表,称为局部描述符表LDT,并且把专属于自己的那些段放 ...

  6. Java 项目JDBC 链接数据库中会出现的错误

    1.出现的地方 package com.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql ...

  7. OC中的面向对象语法

    一. 面向对象和面向过程思想 OC是面向对象的,C是面向过程的.面向对象和面向过程只是解决问题的两种不同思想 1. 面向对象和面向过程的区别 1) 以用电脑听歌为例子 a) 面向过程 打开电脑 播放电 ...

  8. nodejs get/request

    灌水评论示例: var http = require('http'); var querystring = require('querystring'); var postData = queryst ...

  9. yii2增加验证码详细步骤

    作者:白狼 出处:http://www.manks.top/article/yii2_captcha本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留 ...

  10. JavaScript(二)——语法

    1.基本数据类型: 字符串.小数.整数.日期时间.布尔型等. 2.类型转换: 分为自动转换和强制转换,一般用强制转换. 其他类型转换为整数:parseint(): 其他类型转换为小数:parseflo ...