tclap库的使用

这是一个C++ 命令行解析库,通过使用 tclap 库,可以自定义命令行参数,最终运行程序时解析出来。

下载地址:https://tclap.sourceforge.net/

文档地址:https://tclap.sourceforge.net/html/classTCLAP_1_1ValueArg.html#abcfd865f772d3fd26cdbe4d8112b3722

安装

解压后把include目录复制到 usr/local/tclp 目录下即可使用,因为只有头文件

1 tar -xzvf tclap-1.2.5.tar.gz
2 cd tclap-1.2.5
3 cd include
4 sudo cp -r tclap/ /usr/local/include/

 

使用方法:

 1 #include <iostream>
 2 #include <sstream>
 3 #include <tclap/CmdLine.h>
 4 using namespace std;
 5 using namespace TCLAP;
 6 
 7 template <class Type>  
 8 Type stringToNum(const std::string& str){  
 9     istringstream iss(str);  
10     Type num;  
11     iss >> num;  
12     return num;      
13 } 
14 
15 int main(int argc, char* argv[])
16 {
17     /*
18         第二个参数表示命令行参数之间用的分隔符,默认是空格
19         第三个参数设置版本,使用 --version 可显示结果
20         第四个参数表示是否创建Help信息,默认为true,使用-h或者--help即可展示
21     */
22     CmdLine cmd("this is description message", ' ', "0.1", true); //创建命令行解释器
23     /*    
24         创建命令行参数
25         第一个参数是短标识,即[-n 参数],第二个参数则是长标识,即[--name 参数],第三个参数是该命令行参数的说明,
26         第四个参数表示该命令行参数是否必须设置,第五个参数表示该命令行参数的默认值,第六个参数说明该命令行参数的
27         数据类型,只是在help信息中做展示使用
28     */
29     ValueArg<std::string> nameArg("n", "name", "Name to print", false, "Tom", "std::string" );  // Tom is the default value
30 
31     ValueArg<int> deviceIdArg("c", "cam", "Camera device id", false, 123, "integer" ); // 33 is the default value
32 
33     ValueArg<double> deviceDataArg("d", "data", "data user defined", false, 2.3456, "double" ); // 33 is the default value
34 
35     ValueArg<std::string> initBbArg("b", "boundingbox", "Init Bounding Box", false, "-1,-1,-1,-1", "x,y,w,h"); 
36     cmd.add(nameArg); //向命令行解释器添加命令行参数
37     cmd.add(deviceIdArg);
38     cmd.add(deviceDataArg);
39     cmd.add(initBbArg);
40     cmd.parse(argc, argv);//解析命令行参数
41     std::string name = nameArg.getValue(); //获得该命令行参数的值
42     std::cout << "My name is: " << name << std::endl;
43 
44     int camId = deviceIdArg.getValue();
45     std::cout << "Camera ID is: " << camId << std::endl;
46 
47     double data = deviceDataArg.getValue();
48     std::cout << "data is: " << data << std::endl;
49 
50     std::stringstream initBbSs(initBbArg.getValue());
51     double initBb[4];
52     for (int i = 0; i < 4; ++i)
53     {
54         std::string singleValueStr;
55         getline(initBbSs, singleValueStr, ',');
56         initBb[i] = stringToNum<double>(singleValueStr);
57     }
58     std::cout << "initBb : " << initBb[0] << " " << initBb[1] << " " << initBb[2] << " " << initBb[3] << std::endl;
59  
60     return 0;
61 }

 

结果:

 1 $ ./test  //未设置命令行参数,按默认值输出
 2 My name is: Tom
 3 Camera ID is: 123
 4 data is: 2.3456
 5 initBb : -1 -1 -1 -1
 6 
 7 $ ./test -n zzqi -c 99 -d 3.5 -b 2,4,6,1  //设置了命令行参数,都会按照设置的值进行输出
 8 My name is: zzqi
 9 Camera ID is: 99
10 data is: 3.5
11 initBb : 2 4 6 1
12 
13 $ ./test --version
14 ./test  version: 0.1
15     
16     
17 $ ./test -h
18 USAGE: 
19 
20    ./test  [-b <x,y,w,h>] [-d <double>] [-c <integer>] [-n <std::string>]
21            [--] [--version] [-h]
22 
23 
24 Where: 
25 
26    -b <x,y,w,h>,  --boundingbox <x,y,w,h>
27      Init Bounding Box
28 
29    -d <double>,  --data <double>
30      data user defined
31 
32    -c <integer>,  --cam <integer>
33      Camera device id
34 
35    -n <std::string>,  --name <std::string>
36      Name to print
37 
38    --,  --ignore_rest
39      Ignores the rest of the labeled arguments following this flag.
40 
41    --version
42      Displays version information and exits.
43 
44    -h,  --help
45      Displays usage information and exits.
46 
47 
48    this is description message

 

tclap库的使用

 

 

 

原文链接: https://www.cnblogs.com/zq8421/p/17020128.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    tclap库的使用

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/309259

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月16日 上午10:58
下一篇 2023年2月16日 上午10:58

相关推荐