当前位置:   article > 正文

C++ 命令行解析库TCLAP

tclap

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

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

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

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

例子:代码如下

  1. #include <string>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <tclap/CmdLine.h>
  5. int main(int argc, char** argv)
  6. {
  7. // Wrap everything in a try block. Do this every time,
  8. // because exceptions will be thrown for problems.
  9.   try {  
  10. // Define the command line object, and insert a message
  11. // that describes the program. The "Command description message"
  12. // is printed last in the help text. The second argument is the
  13. // delimiter (usually space) and the last one is the version number.
  14. // The CmdLine object parses the argv array based on the Arg objects
  15. // that it contains.
  16.   TCLAP::CmdLine cmd("Command description message"' '"0.9");
  17. // Define a value argument and add it to the command line.
  18. // A value arg defines a flag and a type of value that it expects,
  19. // such as "-n Bishop".
  20.   // homer 是默认的name的数值。
  21. TCLAP::ValueArg<std::string> nameArg("n","name","Name to print",true,"homer","string");
  22. // Add the argument nameArg to the CmdLine object. The CmdLine object
  23. // uses this Arg to parse the command line.
  24. cmd.add( nameArg );
  25. // Define a switch and add it to the command line.
  26. // A switch arg is a boolean argument and only defines a flag that
  27. // indicates true or false. In this example the SwitchArg adds itself
  28. // to the CmdLine object as part of the constructor. This eliminates
  29. // the need to call the cmd.add() method. All args have support in
  30. // their constructors to add themselves directly to the CmdLine object.
  31. // It doesn't matter which idiom you choose, they accomplish the same thing.
  32. TCLAP::SwitchArg reverseSwitch("r","reverse","Print name backwards", cmd, false);
  33. // Parse the argv array.
  34. cmd.parse( argc, argv );
  35. // Get the value parsed by each arg.
  36. std::string name = nameArg.getValue();
  37. bool reverseName = reverseSwitch.getValue();
  38. // Do what you intend.
  39. if ( reverseName )
  40. {
  41. std::reverse(name.begin(),name.end());
  42. std::cout << "My name (spelled backwards) is: " << name << std::endl;
  43. }
  44. else
  45.     std::cout << "My name is: " << name << std::endl;
  46. } catch (TCLAP::ArgException &e) // catch any exceptions
  47. { std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl; }
  48. }

输出:

  1. % test1 -n mike // 指定了mike之后,name就被替换成mike了
  2. My name is: mike
  3. % test1 -n mike -r
  4. My name (spelled backwards) is: ekim
  5. % test1 -r -n mike
  6. My name (spelled backwards) is: ekim
  7. % test1 -r
  8. PARSE ERROR:
  9. One or more required arguments missing!
  10. Brief USAGE:
  11. test1 [-r] -n <string> [--] [-v] [-h]
  12. For complete USAGE and HELP type:
  13. test1 --help
  14. % test1 --help
  15. USAGE:
  16. test1 [-r] -n <string> [--] [-v] [-h]
  17. Where:
  18. -r, --reverse
  19. Print name backwards
  20. -n <string> --name <string>
  21. (required) (value required) Name to print
  22. --, --ignore_rest
  23. Ignores the rest of the labeled arguments following this flag.
  24. -v, --version
  25. Displays version information and exits.
  26. -h, --help
  27. Displays usage information and exits.
  28. 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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/976140
推荐阅读
相关标签
  

闽ICP备14008679号