zoukankan      html  css  js  c++  java
  • getopt,getoptlong学习

         getopt和getoptlong被用来解析命令行参数。
     
    一、getopt
    #include <unistd.h>
    extern char *optarg;  
    extern int optind,   
    extern int opterr,  
    extern int optopt;  
    int getopt(int argc, char * const argv[], const char *optstring);

         定义了四个全局变量:optarg是选项的参数指针,optind记录目前查找的位置,当opterr = 0时,getopt不向stderr输出错误信息。当命令选项字符不包括在optstring中或者缺少必要的参数时,该选项存储在optopt中,getopt返回 '?' 

         getopt调用一次,返回一次选项,当检查不到参数时,返回-1,同时,optind储存第一个不包含选项的命令行参数。
         optstring参数可以有以下元素。
         1.单个字符:表示选项
         2.单个字符后接一个冒号:表示该选项必须跟一个参数,参数紧跟在选项后,或以一个空格隔开,将参数的指针赋给optarg。
         3.单个字符后跟两个冒号:表示该选项必须跟一个参数,且不能用空格隔开。
    例子:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
     
    int main(int argc, char **argv)
    {
        int opt = 0;
     
        while ((opt = getopt(argc, argv, "if:?lr::")) != -1)
        {
            switch(opt)
            {
                case 'i':
                case 'l':
                    printf("option: %c
    ", opt);
                    break;
                case 'f':
                    printf("filename: %s
    ", optarg);
                    break;
                case 'r':
                    printf("arg:%s
    ", optarg);
                    break;
                case ':':
                    printf("option needs a value
    ");
                    break;
                case '?':
                    printf("unknow option: %c
    ", optopt);
                    break;
            }
        }
     
     
        for (; optind < argc; optind++)
        {
            printf("argument: %s
    ", argv[optind]);
     
        ar
     
        return 0;
    }
         二、getoptlong
         getoptlong就是添加了对长选项支持的getopt,getoptlong比getopt多两个参数,struct option *longopts和int *longindex,后者用到的少,一般置为NULL。直接来看struct option 结构
    struct option
    {
         char *name;
         int has_arg;
         int *flag;
         int val;
    };
         name表示的是长参数名,has_arg有三个值,当has_arg = no_argument(即0)时,表示该参数后面不跟参数值,当has_arg = required_argument(即1)时,表示参数后面一定跟参数值,当has_arg = optional_argument(即2)时,表示该参数后面可跟参数值,也可不跟。flag   当这个指针为空的时候,函数直接将val的数值从getopt_long的返回值返回出去,当它非空时,val的值会被赋到flag指向的整型数中,而函数返回值为0 。 val    用于指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值。
    定义option試例:
    struct option longopts[] =
    {
        {"initialize", 0, NULL, 'i'},
        {"filename", 1, NULL, 'f'},
        {"list", 0, NULL, 'l'},
        {"restart", 0, NULL, 'r'}
    };
    例子:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
     
    #define _GNU_SOURCE
    #include <getopt.h>
     
    int main(int argc, char **argv)
    {
        int opt;
     
        struct option longopts[] =
        {
            {"initialize", 0, NULL, 'i'},
            {"filename", 1, NULL, 'f'},
            {"list", 0, NULL, 'l'},
            {"restart", 0, NULL, 'r'}
        };
     
        while ((opt = getopt_long(argc, argv, ":if:lr", longopts, NULL)) != -1)
        {
            switch(opt)
            {
                case 'i':
                case 'l':
                case 'r':
                    printf("option: %c
    ", opt);
                    break;
                case 'f':
                    printf("filename: %s
    ", optarg);
                    break;
                case ':':
                    printf("option needs a value
    ");
                    break;
                case '?':
                    printf("unknow option: %c
    ", optopt);
                    break;
            }
        }
     
        for (; optind < argc; optind++)
        {
            printf("argument: %s
    ", argv[optind]);
     
        }
     
        return 0;
    }
  • 相关阅读:
    01-Django 简介
    函数及函数的嵌套等
    循环及循环嵌套
    运算符
    if, elif, else及if嵌套
    变量及变量计算和引用
    Python的3种执行方式
    Jmeter设置默认中文启动
    Vysor
    python基础学习(二)
  • 原文地址:https://www.cnblogs.com/shenlinken/p/5791428.html
Copyright © 2011-2022 走看看