linux使用getopt解析参数

      getopt是linux下解析命令行参数的api。以linux内核代码的一个例子来说明:

 

static void cmdline(int argc, char *argv[])
{
    int opt;
    progname = basename(argv[0]);

    while ((opt = getopt(argc, argv, "+lci:m:")) != -1) {
        switch (opt) {
        case 'l':
            if (mode)
                print_wrong_arg_exit();
            mode = list;
            break;
        case 'i':
            /* only allow -i with -m or no option */
            if (mode && mode != show)
                print_wrong_arg_exit();
            interval = atoi(optarg);
            break;
        case 'm':
            if (mode)
                print_wrong_arg_exit();
            mode = show;
            show_monitors_param = optarg;
            break;
        case 'c':
            wake_cpus = 1;
            break;
        default:
            print_wrong_arg_exit();
        }
    }
    if (!mode)
        mode = show_all;
}

    

    getopt仅支持"-v"类型的带参以及不带参数的命令行参数解析,包含"--v"一对短横线的复杂命令行参数请使用其他api。

    函数原型

       int getopt(int argc, char * const argv[],
                  const char *optstring);
    argc和argv都懂得,optstring告诉getopt需要哪些命令以及哪些命令需要参数,以"+lci:m:"为例,带"+"的参数不多见忽略掉,'l'和'c'是不带参数的命令选项,'i:'和'm:'是带参数的命令选项,两者通过':'进行区分。getopt获取命令以后参数保存在全局变量optarg中。

    包括optarg在内,getopt使用三个全局变量来保存状态(该函数是线程非安全的,多线程使用需要自己加保护)。

    optarg,保存命令的参数,如果有的话。

    optind,下一次getopt时指向的argv指针的索引 。

    optopt,最后一个已知选项。

    

    optind用来获取除了命令行命令和参数以外的其他内容,比如命令行的最后添加一个默认参数配置文件。

    上面的例子是一个直观的解析用法,再来一个推荐的解析方式。
    while ((opt = getopt(argc, argv, "+lci:m:")) != -1) {
        switch (opt) {

        case 'A':
            /* enables "shutdown" command */
            settings.shutdown_command = true;
            break;

        case 'a':
            /* access for unix domain socket, as octal mask (like chmod)*/
            settings.access= strtol(optarg,NULL,8);
            break;
        default:

             break;

}

 settings是一个入参结构体,对应入参的命令和参数

struct settings {
    size_t maxbytes;
    int maxconns;
    int port;
    int udpport;
    char *inter;
    int verbose;
    rel_time_t oldest_live; /* ignore existing items older than this */
    uint64_t oldest_cas; /* ignore existing items with CAS values lower than this */
    int evict_to_free;
    char *socketpath;   /* path to unix socket if using local socket */
    int access;  /* access mask (a la chmod) for unix domain socket */
    double factor;          /* chunk size growth factor */
    int chunk_size;
    int num_threads;        /* number of worker (without dispatcher) libevent threads to run */
    int num_threads_per_udp; /* number of worker threads serving each udp socket */
    char prefix_delimiter;  /* character that marks a key prefix (for stats) */
    int detail_enabled;     /* nonzero if we're collecting detailed stats */
    int reqs_per_event;     /* Maximum number of io to process on each
                               io-event. */
    bool use_cas;
    enum protocol binding_protocol;
    int backlog;
    int item_size_max;        /* Maximum item size, and upper end for slabs */
    bool sasl;              /* SASL on/off */
    bool maxconns_fast;     /* Whether or not to early close connections */
    bool lru_crawler;        /* Whether or not to enable the autocrawler thread */
    bool lru_maintainer_thread; /* LRU maintainer background thread */
    bool slab_reassign;     /* Whether or not slab reassignment is allowed */
    int slab_automove;     /* Whether or not to automatically move slabs */
    int hashpower_init;     /* Starting hash power level */
    bool shutdown_command; /* allow shutdown command */
    int tail_repair_time;   /* LRU tail refcount leak repair time */
    bool flush_enabled;     /* flush_all enabled */
    char *hash_algorithm;     /* Hash algorithm in use */
    int lru_crawler_sleep;  /* Microsecond sleep between items */
    uint32_t lru_crawler_tocrawl; /* Number of items to crawl per run */
    int hot_lru_pct; /* percentage of slab space for HOT_LRU */
    int warm_lru_pct; /* percentage of slab space for WARM_LRU */
    int crawls_persleep; /* Number of LRU crawls to run before sleeping */
    bool expirezero_does_not_evict; /* exptime == 0 goes into NOEXP_LRU */
};
    settings封装了可能有的各个命令及参数。把settings定义为全局变量可在整个程序访问。

原文地址:https://www.cnblogs.com/learn-my-life/p/4627385.html