首页 > 其他分享 >命令行参数解析getopt

命令行参数解析getopt

时间:2022-10-25 14:44:07浏览次数:78  
标签:opt const int argument char 命令行 getopt 解析 coption

 

 

命令行解析参数

 

GNU C提供的函数getopt、getopt_long、getopt_long_only函数来解析命令行参数

 

https://github.com/tylov/STC/blob/master/docs/coption_api.md  (最新版本已经删除)

 

https://github.com/tylov/STC/blob/e29110e296b7b4618778d4e57646fc5fe0103bd6/docs/coption_api.md

 

STC coption: Command line argument parsing

This describes the API of the coption_get() function for command line argument parsing.

See getopt_long for a similar posix function.

Types

typedef enum {
    coption_no_argument,
    coption_required_argument,
    coption_optional_argument
} coption_type;

typedef struct {
    const char *name;
    coption_type type;
    int val;
} coption_long;

typedef struct {
    int ind;            /* equivalent to posix optind */
    int opt;            /* equivalent to posix optopt */
    const char *optstr; /* points to the option string, if any */
    const char *arg;    /* equivalent to posix optarg */
    ...
} coption;

Methods

coption         coption_init(void);
int             coption_get(coption *opt, int argc, char *argv[],
                            const char *shortopts, const coption_long *longopts);

Example

#include <stdio.h>
#include <stc/coption.h>

int main(int argc, char *argv[]) {
    coption_long longopts[] = {
        {"foo", coption_no_argument,       'f'},
        {"bar", coption_required_argument, 'b'},
        {"opt", coption_optional_argument, 'o'},
        {0}
    };
    const char* shortopts = "xy:z::123";
    if (argc == 1) 
        printf("Usage: program -x -y ARG -z [ARG] -1 -2 -3 --foo --bar ARG --opt [ARG] [ARGUMENTS]\n", argv[0]);
    coption opt = coption_init();
    int c;
    while ((c = coption_get(&opt, argc, argv, shortopts, longopts)) != -1) {
        switch (c) {
            case '?': printf("error: unknown option: %s\n", opt.optstr); break;
            case ':': printf("error: missing argument for %s\n", opt.optstr); break;
            default:  printf("option: %c [%s]\n", opt.opt, opt.arg ? opt.arg : ""); break;
        }
    }
    printf("\nNon-option arguments:");
    for (int i = opt.ind; i < argc; ++i)
        printf(" %s", argv[i]);
    putchar('\n');
}
    return 0;
}

标签:opt,const,int,argument,char,命令行,getopt,解析,coption
From: https://www.cnblogs.com/sinferwu/p/16295411.html

相关文章

  • POJ 2588(解析几何+并查集)
    题目就是早从左到右的路注意输入的实数这题图画好就行,别像我一开始把图弄反就成从上开始找,若找到一个与下边界相邻的就无解,找到与左边相邻的记圆与左边界相交的下边的点(相当......
  • 我把一个json格式的数据读到dataframe里面了 怎么解析出自己需要的字段呢?
    大家好,我是皮皮。一、前言前几天在Python最强王者交流群【WYM】问了一个pandas处理的问题,提问截图如下:原始数据如下图所示:后来还提供了一个小文件。二、实现过程......
  • Python的bs4解析
    BeautifulSoup4使用requests库获取html页面并将其转换成字符串之后,需要进一步解析html页面格式,提取有用信息。BeautifulSoup4库,也被成为bs4库,用于解析和处理html和xml1......
  • 6.HashMap源码解析
    1.数据结构如上图所示,HashMap底层的数据结构主要是数组+链表+红黑树。其中当链表的长度大于等于8时,链表会转化成红黑树,当红黑树的大小小于等于6时,红黑树会转化成链表。......
  • 7.TreeMap源码解析
    1.数据结构TreeMap的底层数据结构是红黑树,和HashMap的红黑树结构一样。不同的是,TreeMap利用红黑树左节点小,右节点大的性质,根据key进行排序,使每个元素能够插入到红黑树的适......
  • 4.LinkedList源码解析
    1.数据结构LinkedList底层数据结构是一个双向链表,整体结构如上图所示,链表中的每个节点都可以向前或者向后追溯。源码privatestaticclassNode<E>{//节点值Eite......
  • 3.ArrayList源码解析
    1.数据结构ArrayList的数据结构是一个数组,如上图所示,图中展示的是一个长度为10的数组,从1开始计数,index表示数组的下标,从0开始计数,elementData表示数组元素。除此之外,源码中......
  • 1.String源码解析
    1.不变性这里说的不变性指的是类值一旦被初始化,就不能再被改变了,如果被修改,将会是新的类,如程序1-1所示。//程序1-1publicclassApp{publicstaticvoidmain(String[......
  • 2.Integer源码解析
    1.取值范围和基本数据类型源码publicfinalclassIntegerextendsNumberimplementsComparable<Integer>{//该值用于定义Integer取值的最小值@Nativepublicst......
  • timedatectl解析
    一,timedatectl输出解析root@sonic:/home/admin#timedatectl             Localtime:Mon2022-10-2421:01:56CST           Universalti......