C++ 命令行解析库 tclap 使用方法

本博文由 youngpan1101 出品,转载请注明出处。
文章链接: http://blog.csdn.net/youngpan1101/article/details/73742209
作者: 宋洋鹏(youngpan1101)
邮箱: yangpeng_song@163.com


简介

  • 通过使用 tclap 库,当我们运行 C++ 项目所生成的可执行文件时,可以很方便地进行命令行(运行指令)的解析。
  • tclap library webpage
  • tclap API

运行步骤

  • 下载 【Download

    下载 tclap-1.2.1.tar.gz , 然后解压,将 tclap-1.2.1/include/tclap 复制到工程目录下的 3rdParty 文件夹

  • 新建 CMake 工程

    • CMakeLists.txt

      cmake_minimum_required(VERSION 2.8)
      project( TclapDemo )
      
      include_directories( ${PROJECT_SOURCE_DIR}/3rdParty )
      
      add_executable( main main.cpp )
    • main.cpp

       #include <iostream>
       #include <sstream>
      
       #include "tclap/CmdLine.h"
      
      //ostringstream 对象用来进行格式化的输出,常用于将各种类型转换为 string 类型
      //ostringstream 只支持 << 操作符
      template<typename T> std::string toString(const T& t){
          std::ostringstream oss;  //创建一个格式化输出流
          oss<<t;                  //把值传递如流中
          return oss.str();   
      }
      
      //模板函数:将string类型变量转换为常用的数值类型(此方法具有普遍适用性)  
      template <class Type>  
      Type stringToNum(const std::string& str){  
          istringstream iss(str);  
          Type num;  
          iss >> num;  
          return num;      
      }  
      
      int main(int argc,char** argv){
          try {
              // Define the command line object.
              TCLAP::CmdLine cmd("Command description message", ' ', "0.1");
      
              TCLAP::ValueArg<std::string> nameArg("n", "name", "Name to print", false, "Tom", "std::string", cmd);  // Tom is the default value
      
              TCLAP::ValueArg<int> deviceIdArg("c", "cam", "Camera device id", false, 123, "integer", cmd); // 33 is the default value
      
              TCLAP::SwitchArg noShowOutputSw("", "no-show", "Don't show video", cmd, false);   // false is the default value
      
              TCLAP::ValueArg<double> deviceDataArg("d", "data", "data user defined", false, 2.3456, "double", cmd); // 33 is the default value
      
              TCLAP::ValueArg<std::string> initBbArg("b", "boundingbox", "Init Bounding Box", false, "-1,-1,-1,-1", "x,y,w,h", cmd);  // Tom is the default value
      
              // Parse the args.
              cmd.parse( argc, argv );
      
              std::string name = nameArg.getValue();
              std::cout << "My name is: " << name << std::endl;
      
              int camId = deviceIdArg.getValue();
              std::cout << "Camera ID is: " << camId << std::endl;
      
              bool bIsShowVideo = noShowOutputSw.getValue();
              std::cout << "The flag of showing video: " << bIsShowVideo << std::endl;
      
              double data = deviceDataArg.getValue();
              std::cout << "data is: " << data << std::endl;
      
              std::stringstream initBbSs(initBbArg.getValue());
              double initBb[4];
              for (int i = 0; i < 4; ++i)
              {
                std::string singleValueStr;
                getline(initBbSs, singleValueStr, ',');
                initBb[i] = stringToNum<double>(singleValueStr);
              }
              std::cout << "initBb : " << initBb[0] << " " << initBb[1] << " " << initBb[2] << " " << initBb[3] << std::endl;
      
          } catch (TCLAP::ArgException &e) {  // catch any exceptions
              std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl; 
          }
      
          return 1;
      }
      
  • 输出结果

    hwj@ubuntu:build$ sudo ./main
    My name is: Tom
    Camera ID is: 123
    The flag of showing video: 0
    data is: 2.3456
    initBb : -1 -1 -1 -1
    hwj@ubuntu:build$ sudo ./main -h
    
    USAGE: 
    
       ./main  [-b <x,y,w,h>] [-d <double>] [--no-show] [-c <integer>] [-n
               <std::string>] [--] [--version] [-h]
    
    
    Where: 
    
       -b <x,y,w,h>,  --boundingbox <x,y,w,h>
         Init Bounding Box
    
       -d <double>,  --data <double>
         data user defined
    
       --no-show
         Don't show video
    
       -c <integer>,  --cam <integer>
         Camera device id
    
       -n <std::string>,  --name <std::string>
         Name to print
    
       --,  --ignore_rest
         Ignores the rest of the labeled arguments following this flag.
    
       --version
         Displays version information and exits.
    
       -h,  --help
         Displays usage information and exits.
    
    
       Command description message
    hwj@ubuntu:build$ sudo ./main --version
    
    ./main  version: 0.1
    
    hwj@ubuntu:build$ sudo ./main -d 0.2457 --no-show -c 2 -n Mike -b 2,3,4,-12
    My name is: Mike
    Camera ID is: 2
    The flag of showing video: 1
    data is: 0.2457
    initBb : 2 3 4 -12
    
    hwj@ubuntu:build$ sudo ./main --data -0.2457 --no-show --cam 22 -n Mike
    My name is: Mike
    Camera ID is: 22
    The flag of showing video: 1
    data is: -0.2457
    initBb : -1 -1 -1 -1
    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值