C++ 命令行参数解析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34719188/article/details/83788199

说明


主要参考以下博客:

博客一:getopt和getopt_long函数

博客二:C++中如何自定义命令行参数——完整实例演示

博客三:使用 Qt 解析命令行参数

博客四:linux c/c++中getopt的使用

短参数之 getopt()


头文件:#include <unistd.h>

函数原型:int getopt(int argc, char * const argv[], const char *optstring);

参数解释:
[param1] argc: main 函数的 argc
[param2] argv: main 函数的 argv
[param3] optstring: 格式控制符。"ab:c:d"代表 -b 和 -b 后面必须跟一个参数,而 -a 和 -d 不需要参数

相关变量:
extern char *optarg; 表示参数的具体内容
extern int optind; 表下一个将被处理到的参数在 argv 中的下标值
extern int opterr;
extern int optopt;

测试案例:

// Opt.cpp
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

// 短参数测试案例
void testGetOpt(int argc, char *argv[]) {
    int opt;  // getopt() 的返回值
    const char *optstring = "a:b:c:d"; // 设置短参数类型及是否需要参数

    while ((opt = getopt(argc, argv, optstring)) != -1) {
        printf("opt = %c\n", opt);  // 命令参数,亦即 -a -b -c -d
        printf("optarg = %s\n", optarg); // 参数内容
        printf("optind = %d\n", optind); // 下一个被处理的下标值
        printf("argv[optind - 1] = %s\n\n",  argv[optind - 1]); // 参数内容
    }
}

int main(int argc, char *argv[]) {
    testGetOpt(argc, argv);
    return 0;
}

正确的使用方法():

[User@ubuntu:~/Desktop]$ opt.exe -a a_para -b b_para -c c_para -d	# 参数全用
[User@ubuntu:~/Desktop]$ opt.exe -a a_para -b b_para -d				# 部分参数
[User@ubuntu:~/Desktop]$ opt.exe -b b_para -a a_para				# 可以倒序
[User@ubuntu:~/Desktop]$ opt.exe  									# 可以无参

错误的使用方法(×):

[User@ubuntu:~/Desktop]$ opt.exe -a  					# -a 后面没有跟参数
[User@ubuntu:~/Desktop]$ opt.exe -a a_para -d d_para  	# -d 后面跟了参数

长参数之 getopt_long()


头文件:#include <getopt.h>

函数原型:int getopt(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);

参数解释:
[param1] argc: main 函数的 argc
[param2] argv: main 函数的 argv
[param3] optstring: 格式控制符
[param4] longopts: 一个由option结构体组成的数组,数组的每个元素,指明了一个“长参数”(即形如–name的参数)名称和性质
[param5] longindex: 如果longindex非空,它指向的变量将记录当前找到参数符合longopts里的第几个元素的描述,即是longopts的下标值

相关变量:
同上

测试案例:

// Opt.cpp
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>

// 长参数测试案例
void testGetOptLong(int argc, char *argv[]) {
    int opt; // getopt_long() 的返回值
    int digit_optind = 0; // 设置短参数类型及是否需要参数

    // 如果option_index非空,它指向的变量将记录当前找到参数符合long_opts里的
    // 第几个元素的描述,即是long_opts的下标值
    int option_index = 0;
    // 设置短参数类型及是否需要参数
    const char *optstring = "ab:nr:";  

    // 设置长参数类型及其简写,比如 --reqarg <==>-r
    /*
    struct option {
             const char * name;  // 参数的名称
             int has_arg; // 是否带参数值,有三种:no_argument, required_argument,optional_argument
             int * flag; // 为空时,函数直接将 val 的数值从getopt_long的返回值返回出去,
                     // 当非空时,val的值会被赋到 flag 指向的整型数中,而函数返回值为0
             int val; // 用于指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值
        };
    其中:
        no_argument(即0),表明这个长参数不带参数(即不带数值,如:--name)
            required_argument(即1),表明这个长参数必须带参数(即必须带数值,如:--name Bob)
            optional_argument(即2),表明这个长参数后面带的参数是可选的,(即--name和--name Bob均可)
     */
    static struct option long_options[] = {
        {"reqarg", required_argument, NULL, 'r'},
        {"noarg",  no_argument,       NULL, 'n'},
        {"optarg", optional_argument, NULL, 'o'},
        {0, 0, 0, 0}  // 添加 {0, 0, 0, 0} 是为了防止输入空值
    };

    while ( (opt = getopt_long(argc,
                               argv,
                               optstring,
                               long_options,
                               &option_index)) != -1) {
        printf("opt = %c\n", opt); // 命令参数,亦即 -a -b -n -r
        printf("optarg = %s\n", optarg); // 参数内容
        printf("optind = %d\n", optind); // 下一个被处理的下标值
        printf("argv[optind - 1] = %s\n",  argv[optind - 1]); // 参数内容
        printf("option_index = %d\n", option_index);  // 当前打印参数的下标值
        printf("\n");
    }
}

int main(int argc, char *argv[]) {
    testGetOptLong(argc, argv);
    return 0;
}

正确的使用方法():

[User@ubuntu:~/Desktop]$ opt.exe -a -b b_para -n -r r_para					# 全部使用
[User@ubuntu:~/Desktop]$ opt.exe -a											# 部分参数
[User@ubuntu:~/Desktop]$ opt.exe -a -b b_para								# 部分参数
[User@ubuntu:~/Desktop]$ opt.exe -a -b b_para -n							# 部分参数
[User@ubuntu:~/Desktop]$ opt.exe -n -r r_para								# 部分参数
[User@ubuntu:~/Desktop]$ opt.exe  -a --noarg --reqarg required_para			# 长参数

错误的使用方法(×):

[User@ubuntu:~/Desktop]$ opt.exe -a -b -n -r required_para		# -b 没有跟参数

注意事项:一般地,不要将短参数的 optstring 和长参数的 struct option 的 val 设置为一样的字母,这样易于区分!!!

长参数之 getopt_long_only()


头文件:#include <unistd.h>

函数原型:int getopt(int argc, char * const argv[], const char *optstring, const struct option *long_opts, int *longindex);

与 getopt_long() 的区别:

  • 该函数与 getopt_long() 函数使用相同的参数表,在功能上基本一致
  • 只是 getopt_long() 只将 --name 当作长参数,但 getopt_long_only() 会将 --name 和 -name 两种选项都当作长参数来匹配
  • getopt_long() 在遇到 -name 时,会拆解成 -n -a -m -e 到 optstring 中进行匹配,而 getopt_long_only() 只在 -name 不能在longopts() 中匹配时才将其拆解成 -n -a -m -e 这样的参数到 optstring 中进行匹配

猜你喜欢

转载自blog.csdn.net/qq_34719188/article/details/83788199