C++ 命令行解析库TCLAP

2023-02-28 17:00:37 浏览数 (1)

介绍一个命令行解析库:TCLAP,属于一个比较好用的命令行解析库,Envoy中也用到了这个库,整理出来,方便后续查看。

详细介绍参考:https://tclap.sourceforge.net/html/classTCLAP_1_1SwitchArg.html

使用方法分成几步: 先初始化一些默认的参数数值,parse传递过来的参数之后,会使用传递进来的参数代替掉这些默认值。

代码语言:javascript复制
// step 1: 实例化CmdLine
TCLAP::CmdLine cmd("Command description message", ' ', "0.9");
// step 2: 参数实现和添加
TCLAP::ValueArg<std::string> nameArg("n","name","Name to print",true,"homer","string");
cmd.add( nameArg );
// step 3: 解析参数
cmd.parse( argc, argv );
// step 4: 获取对应的命令行
std::string name = nameArg.getValue();
bool reverseName = reverseSwitch.getValue();

例子:代码如下

代码语言:javascript复制
#include <string>
#include <iostream>
#include <algorithm>
#include <tclap/CmdLine.h>

int main(int argc, char** argv)
{
  // Wrap everything in a try block.  Do this every time, 
  // because exceptions will be thrown for problems.
  try {  
  // Define the command line object, and insert a message
  // that describes the program. The "Command description message" 
  // is printed last in the help text. The second argument is the 
  // delimiter (usually space) and the last one is the version number. 
  // The CmdLine object parses the argv array based on the Arg objects
  // that it contains. 
  TCLAP::CmdLine cmd("Command description message", ' ', "0.9");
  // Define a value argument and add it to the command line.
  // A value arg defines a flag and a type of value that it expects,
  // such as "-n Bishop".
  // homer 是默认的name的数值。
  TCLAP::ValueArg<std::string> nameArg("n","name","Name to print",true,"homer","string");

  // Add the argument nameArg to the CmdLine object. The CmdLine object
  // uses this Arg to parse the command line.
  cmd.add( nameArg );

  // Define a switch and add it to the command line.
  // A switch arg is a boolean argument and only defines a flag that
  // indicates true or false.  In this example the SwitchArg adds itself
  // to the CmdLine object as part of the constructor.  This eliminates
  // the need to call the cmd.add() method.  All args have support in
  // their constructors to add themselves directly to the CmdLine object.
  // It doesn't matter which idiom you choose, they accomplish the same thing.
  TCLAP::SwitchArg reverseSwitch("r","reverse","Print name backwards", cmd, false);

  // Parse the argv array.
  cmd.parse( argc, argv );

  // Get the value parsed by each arg. 
  std::string name = nameArg.getValue();
  bool reverseName = reverseSwitch.getValue();

  // Do what you intend. 
  if ( reverseName )
  {
    std::reverse(name.begin(),name.end());
    std::cout << "My name (spelled backwards) is: " << name << std::endl;
  }
  else
    std::cout << "My name is: " << name << std::endl;
  } catch (TCLAP::ArgException &e)  // catch any exceptions
  { std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl; }
}

输出:

代码语言:javascript复制
% test1 -n mike // 指定了mike之后,name就被替换成mike了
My name is: mike

% test1 -n mike -r
My name (spelled backwards) is: ekim

% test1 -r -n mike
My name (spelled backwards) is: ekim

% test1 -r
PARSE ERROR:
             One or more required arguments missing!

Brief USAGE:
   test1  [-r] -n <string> [--] [-v] [-h]

For complete USAGE and HELP type:
   test1 --help


% test1 --help

USAGE:

   test1  [-r] -n <string> [--] [-v] [-h]


Where:

   -r,  --reverse
     Print name backwards

   -n <string>  --name <string>
     (required)  (value required)  Name to print

   --,  --ignore_rest
     Ignores the rest of the labeled arguments following this flag.

   -v,  --version
     Displays version information and exits.

   -h,  --help
     Displays usage information and exits.


   Command description message

参考下面三篇文章: https://tclap.sourceforge.net/html/classTCLAP_1_1SwitchArg.html https://dtai.cs.kuleuven.be/krr/files/gidl2docs/code/pcsolver/lib/tclap/docs/manual.html https://blog.csdn.net/youngpan1101/article/details/73742209

0 人点赞