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.

157 lines
7.5 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(bool a_, bool b_, bool c_, bool d_, bool e_, bool f_,
  16. std::initializer_list<int> i_) :
  17. a{a_}, b{b_}, c{c_}, d{d_}, e{e_}, f{f_}, i{i_}
  18. {}
  19. bool a = false, b = false, c = false, d = false, e = false, f = false;
  20. std::vector<int> i;
  21. friend bool operator == (const active& x, const active& y) noexcept {
  22. return (x.a == y.a && x.b == y.b && x.c == y.c &&
  23. x.d == y.d && x.e == y.e && x.f == y.f &&
  24. std::equal(begin(x.i), end(x.i), begin(y.i)));
  25. }
  26. };
  27. struct repeats {
  28. repeats() = default;
  29. explicit
  30. repeats(int a_, int b_, int c_, int d_, int e_, int f_, int i_) :
  31. a{a_}, b{b_}, c{c_}, d{d_}, e{e_}, f{f_}, i{i_}
  32. {}
  33. int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, i = 0;
  34. friend bool operator == (const repeats& x, const repeats& y) noexcept {
  35. return (x.a == y.a && x.b == y.b && x.c == y.c &&
  36. x.d == y.d && x.e == y.e && x.f == y.f && x.i == y.i);
  37. }
  38. };
  39. //-------------------------------------------------------------------
  40. void test(int lineNo,
  41. const std::initializer_list<const char*> args,
  42. const active& matches,
  43. const repeats& repeated)
  44. {
  45. using namespace clipp;
  46. active m;
  47. repeats r;
  48. auto cli = group(
  49. //repeatable option
  50. option("a").repeatable(true).set(m.a).if_repeated(increment(r.a))
  51. ,
  52. //repeatable option + exactly one optional value
  53. option("b").repeatable(true).set(m.b).if_repeated(increment(r.b)) &
  54. opt_value("B", m.i).if_repeated(increment(r.i))
  55. ,
  56. //option + values...
  57. option("c").set(m.c).if_repeated(increment(r.c)) &
  58. values("C", m.i).if_repeated(increment(r.i))
  59. ,
  60. //repeatable group of (option + value)
  61. repeatable(
  62. option("d").set(m.d).if_repeated(increment(r.d)) &
  63. value("D", m.i).if_repeated(increment(r.i))
  64. )
  65. ,
  66. //non-repeatable option
  67. option("e").set(m.e).if_repeated(increment(r.e))
  68. ,
  69. //repeatable group of (option + values...)
  70. //Note that we have to make sure, that args {"f", "3", "f", "4"}
  71. //won't be interpreted as "f" followed by 3 values.
  72. //This is achieved with the prefix_not predicate which excludes "f"
  73. //itself from beeing matched as a value.
  74. repeatable(
  75. option("f").set(m.f).if_repeated(increment(r.f)) &
  76. values(match::prefix_not("f"), "F", m.i).if_repeated(increment(r.i))
  77. )
  78. );
  79. run_wrapped_variants({ __FILE__, lineNo }, args, cli,
  80. [&]{ m = active{}; r = repeats{}; },
  81. [&]{ return m == matches && r == repeated; });
  82. }
  83. //-------------------------------------------------------------------
  84. int main()
  85. {
  86. try {
  87. test(__LINE__, {""}, active{}, repeats{});
  88. // a b c d e f i a b c d e f i
  89. test(__LINE__, {"a"}, active{1,0,0,0,0,0,{}}, repeats{0,0,0,0,0,0,0});
  90. test(__LINE__, {"a", "a"}, active{1,0,0,0,0,0,{}}, repeats{1,0,0,0,0,0,0});
  91. test(__LINE__, {"a", "a", "a"}, active{1,0,0,0,0,0,{}}, repeats{2,0,0,0,0,0,0});
  92. test(__LINE__, {"a", "a", "a", "a"}, active{1,0,0,0,0,0,{}}, repeats{3,0,0,0,0,0,0});
  93. test(__LINE__, {"a", "a", "a", "a", "a" }, active{1,0,0,0,0,0,{}}, repeats{4,0,0,0,0,0,0});
  94. test(__LINE__, {"a", "a", "e", "a", "a", "a"}, active{1,0,0,0,1,0,{}}, repeats{4,0,0,0,0,0,0});
  95. test(__LINE__, {"b"}, active{0,1,0,0,0,0,{}}, repeats{0,0,0,0,0,0,0});
  96. test(__LINE__, {"b", "b", "2"}, active{0,1,0,0,0,0,{2}}, repeats{0,1,0,0,0,0,0});
  97. test(__LINE__, {"b", "2", "b"}, active{0,1,0,0,0,0,{2}}, repeats{0,1,0,0,0,0,0});
  98. test(__LINE__, {"b", "a", "b"}, active{1,1,0,0,0,0,{}}, repeats{0,1,0,0,0,0,0});
  99. test(__LINE__, {"b", "b", "2", "3"}, active{0,1,0,0,0,0,{2}}, repeats{0,1,0,0,0,0,1});
  100. test(__LINE__, {"b", "2", "b", "b", "3"}, active{0,1,0,0,0,0,{2}}, repeats{0,2,0,0,0,0,1});
  101. test(__LINE__, {"c"}, active{0,0,1,0,0,0,{}}, repeats{0,0,0,0,0,0,0});
  102. test(__LINE__, {"c", "c"}, active{0,0,1,0,0,0,{}}, repeats{0,0,1,0,0,0,0});
  103. test(__LINE__, {"c", "2"}, active{0,0,1,0,0,0,{2}}, repeats{0,0,0,0,0,0,0});
  104. test(__LINE__, {"c", "2", "3"}, active{0,0,1,0,0,0,{2,3}}, repeats{0,0,0,0,0,0,1});
  105. test(__LINE__, {"c", "c", "2", "3"}, active{0,0,1,0,0,0,{2,3}}, repeats{0,0,1,0,0,0,1});
  106. test(__LINE__, {"c", "2", "3", "c"}, active{0,0,1,0,0,0,{2,3}}, repeats{0,0,1,0,0,0,1});
  107. test(__LINE__, {"d"}, active{0,0,0,1,0,0,{}}, repeats{0,0,0,0,0,0,0});
  108. test(__LINE__, {"d", "d"}, active{0,0,0,1,0,0,{}}, repeats{0,0,0,1,0,0,0});
  109. test(__LINE__, {"d", "2"}, active{0,0,0,1,0,0,{2}}, repeats{0,0,0,0,0,0,0});
  110. test(__LINE__, {"d", "2", "d", "3"}, active{0,0,0,1,0,0,{2,3}}, repeats{0,0,0,1,0,0,1});
  111. test(__LINE__, {"d", "2", "3"}, active{0,0,0,1,0,0,{2}}, repeats{0,0,0,0,0,0,0});
  112. test(__LINE__, {"e"}, active{0,0,0,0,1,0,{}}, repeats{0,0,0,0,0,0,0});
  113. test(__LINE__, {"e", "a"}, active{1,0,0,0,1,0,{}}, repeats{0,0,0,0,0,0,0});
  114. test(__LINE__, {"e", "e"}, active{0,0,0,0,1,0,{}}, repeats{0,0,0,0,1,0,0});
  115. test(__LINE__, {"e", "e", "e"}, active{0,0,0,0,1,0,{}}, repeats{0,0,0,0,2,0,0});
  116. test(__LINE__, {"e", "a", "e", "e"}, active{1,0,0,0,1,0,{}}, repeats{0,0,0,0,2,0,0});
  117. test(__LINE__, {"e", "b", "e", "a", "e" }, active{1,1,0,0,1,0,{}}, repeats{0,0,0,0,2,0,0});
  118. test(__LINE__, {"e", "a", "a", "e", "e", "a"}, active{1,0,0,0,1,0,{}}, repeats{2,0,0,0,2,0,0});
  119. test(__LINE__, {"f"}, active{0,0,0,0,0,1,{}}, repeats{0,0,0,0,0,0,0});
  120. test(__LINE__, {"f", "2"}, active{0,0,0,0,0,1,{2}}, repeats{0,0,0,0,0,0,0});
  121. test(__LINE__, {"f", "2", "3"}, active{0,0,0,0,0,1,{2,3}}, repeats{0,0,0,0,0,0,1});
  122. test(__LINE__, {"f", "2", "f", "3"}, active{0,0,0,0,0,1,{2,3}}, repeats{0,0,0,0,0,1,1});
  123. test(__LINE__, {"f", "2", "3", "4"}, active{0,0,0,0,0,1,{2,3,4}}, repeats{0,0,0,0,0,0,2});
  124. test(__LINE__, {"f", "2", "f", "3", "4"}, active{0,0,0,0,0,1,{2,3,4}}, repeats{0,0,0,0,0,1,2});
  125. test(__LINE__, {"f", "2", "3", "f", "4"}, active{0,0,0,0,0,1,{2,3,4}}, repeats{0,0,0,0,0,1,2});
  126. test(__LINE__, {"f", "2", "f", "3", "f", "4"}, active{0,0,0,0,0,1,{2,3,4}}, repeats{0,0,0,0,0,2,2});
  127. test(__LINE__, {"f", "f"}, active{0,0,0,0,0,1,{}}, repeats{0,0,0,0,0,1,0});
  128. test(__LINE__, {"f", "2", "3", "f"}, active{0,0,0,0,0,1,{2,3}}, repeats{0,0,0,0,0,1,1});
  129. }
  130. catch(std::exception& e) {
  131. std::cerr << e.what() << std::endl;
  132. return 1;
  133. }
  134. }