You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.7 KiB

2 years ago
  1. /*****************************************************************************
  2. *
  3. * CLIPP - command line interfaces for modern C++
  4. *
  5. * released under MIT license
  6. *
  7. * (c) 2017-2018 André Müller; foss@andremueller-online.de
  8. *
  9. *****************************************************************************/
  10. #include "testing.h"
  11. //-------------------------------------------------------------------
  12. struct active {
  13. active() = default;
  14. explicit
  15. active(int i_, char a_, char b_, char c_) : i{i_}, a{a_}, b{b_}, c{c_} {}
  16. active(int i_, const std::string& s_) : i{i_}, s{s_} {}
  17. int i = 0;
  18. char a = ' ', b = ' ', c = ' ';
  19. std::string s = "";
  20. friend bool operator == (const active& x, const active& y) noexcept {
  21. return (x.i == y.i &&
  22. x.a == y.a && x.b == y.b && x.c == y.c &&
  23. x.s == y.s);
  24. }
  25. };
  26. //-------------------------------------------------------------------
  27. void test(int lineNo,
  28. const std::initializer_list<const char*> args,
  29. const active& matches)
  30. {
  31. using namespace clipp;
  32. auto one_char = [](const std::string& arg) {
  33. if(arg.empty() || !std::isalpha(arg[0])) return subrange{};
  34. return subrange{0,1};
  35. };
  36. active m;
  37. auto cli = group(
  38. option("-i").set(m.i,1) & value(one_char, "A", m.a),
  39. option("-j").set(m.i,2) & value(one_char, "A", m.a) & value(one_char, "B", m.b),
  40. option("-k").set(m.i,3) & value(one_char, "A", m.a) & value(one_char, "B", m.b) & value(one_char, "C", m.c)
  41. ,
  42. option("-m").set(m.i,4) & values(one_char, "char", [&](const std::string& a){m.s += a;})
  43. ,
  44. option("-o").set(m.i,9) & value("str", m.s)
  45. );
  46. run_wrapped_variants({ __FILE__, lineNo }, args, cli,
  47. [&]{ m = active{}; },
  48. [&]{ return m == matches; });
  49. }
  50. //-------------------------------------------------------------------
  51. int main()
  52. {
  53. try {
  54. test(__LINE__, {""}, active{});
  55. test(__LINE__, {"-i" }, active{1, ' ', ' ', ' '});
  56. test(__LINE__, {"-i", "x" }, active{1, 'x', ' ', ' '});
  57. test(__LINE__, {"-i", "x", "y" }, active{1, 'x', ' ', ' '});
  58. test(__LINE__, {"-i", "x", "y", "z"}, active{1, 'x', ' ', ' '});
  59. test(__LINE__, {"-j" }, active{2, ' ', ' ', ' '});
  60. test(__LINE__, {"-j", "x" }, active{2, 'x', ' ', ' '});
  61. test(__LINE__, {"-j", "x", "y" }, active{2, 'x', 'y', ' '});
  62. test(__LINE__, {"-j", "x", "y", "z"}, active{2, 'x', 'y', ' '});
  63. test(__LINE__, {"-k" }, active{3, ' ', ' ', ' '});
  64. test(__LINE__, {"-k", "x" }, active{3, 'x', ' ', ' '});
  65. test(__LINE__, {"-k", "x", "y" }, active{3, 'x', 'y', ' '});
  66. test(__LINE__, {"-k", "x", "y", "z"}, active{3, 'x', 'y', 'z'});
  67. test(__LINE__, {"-m" }, active{4, ""});
  68. test(__LINE__, {"-m", "x" }, active{4, "x"});
  69. test(__LINE__, {"-m", "x", "y" }, active{4, "xy"});
  70. test(__LINE__, {"-m", "x", "y", "z"}, active{4, "xyz"});
  71. test(__LINE__, {"-o" }, active{9, ""});
  72. test(__LINE__, {"-o", "x" }, active{9, "x"});
  73. test(__LINE__, {"-o", "x", "y" }, active{9, "x"});
  74. test(__LINE__, {"-o", "x", "y", "z"}, active{9, "x"});
  75. //joined sequence
  76. test(__LINE__, {"-i" }, active{1, ' ', ' ', ' '});
  77. test(__LINE__, {"-ix" }, active{1, 'x', ' ', ' '});
  78. test(__LINE__, {"-ixy" }, active{0, ' ', ' ', ' '});
  79. test(__LINE__, {"-ixyz"}, active{0, ' ', ' ', ' '});
  80. test(__LINE__, {"-j" }, active{2, ' ', ' ', ' '});
  81. test(__LINE__, {"-jx" }, active{2, 'x', ' ', ' '});
  82. test(__LINE__, {"-jxy" }, active{2, 'x', 'y', ' '});
  83. test(__LINE__, {"-jxyz"}, active{0, ' ', ' ', ' '});
  84. test(__LINE__, {"-k" }, active{3, ' ', ' ', ' '});
  85. test(__LINE__, {"-kx" }, active{3, 'x', ' ', ' '});
  86. test(__LINE__, {"-kxy" }, active{3, 'x', 'y', ' '});
  87. test(__LINE__, {"-kxyz" }, active{3, 'x', 'y', 'z'});
  88. test(__LINE__, {"-kxyza"}, active{0, ' ', ' ', ' '});
  89. test(__LINE__, {"-m" }, active{4, ""});
  90. test(__LINE__, {"-mx" }, active{4, "x"});
  91. test(__LINE__, {"-mxy" }, active{4, "xy"});
  92. test(__LINE__, {"-mxyz" }, active{4, "xyz"});
  93. test(__LINE__, {"-mxyza"}, active{4, "xyza"});
  94. test(__LINE__, {"-o" }, active{9, ""});
  95. test(__LINE__, {"-ox" }, active{9, "x"});
  96. test(__LINE__, {"-oxy" }, active{9, "xy"});
  97. test(__LINE__, {"-oxyz" }, active{9, "xyz"});
  98. test(__LINE__, {"-oxyza"}, active{9, "xyza"});
  99. }
  100. catch(std::exception& e) {
  101. std::cerr << e.what() << std::endl;
  102. return 1;
  103. }
  104. }