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.

213 lines
9.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(bool a_,
  16. bool ie_, bool je_, bool ke_, bool ve_,
  17. int i_, int j_, int k_, std::initializer_list<int> v_)
  18. :
  19. a{a_},
  20. ie{ie_}, je{je_}, ke{ke_}, ve{ve_},
  21. i{i_}, j{j_}, k{k_}, v{v_}
  22. {}
  23. //option presence
  24. bool a = false;
  25. //requirement error
  26. bool ie = false, je = false, ke = false, ve = false;
  27. //values
  28. int i = 0, j = 0, k = 0;
  29. std::vector<int> v;
  30. friend bool operator == (const active& x, const active& y) noexcept {
  31. return (x.a == y.a && x.ie == y.ie && x.je == y.je &&
  32. x.ke == y.ke && x.ve == y.ve && x.i == y.i &&
  33. x.j == y.j && x.k == y.k &&
  34. std::equal(begin(x.v), end(x.v), begin(y.v)));
  35. }
  36. template<class OStream>
  37. friend OStream& operator << (OStream& os, const active& x) {
  38. return os << x.a <<' '<< x.ie <<' '<< x.je <<' '<< x.ke <<' '
  39. << x.ve <<' '<< x.i <<' '<< x.j <<' '<< x.k;
  40. }
  41. };
  42. //-------------------------------------------------------------------
  43. void test(int lineNo,
  44. const std::initializer_list<const char*> args,
  45. const active& matches)
  46. {
  47. using namespace clipp;
  48. active m;
  49. auto cli = group(
  50. option("-a").set(m.a) & opt_value("i", m.i).if_missing(set(m.ie))
  51. ,
  52. option("-b").set(m.a) & opt_value("i", m.i).if_missing(set(m.ie))
  53. & opt_value("j", m.j).if_missing(set(m.je))
  54. ,
  55. option("-c").set(m.a) & opt_value("i", m.i).if_missing(set(m.ie))
  56. & opt_value("j", m.j).if_missing(set(m.je))
  57. & opt_value("k", m.k).if_missing(set(m.ke))
  58. ,
  59. option("-d").set(m.a) & opt_values("v", m.v).if_missing(set(m.ve))
  60. ,
  61. option("-e").set(m.a) & opt_value("i", m.i).if_missing(set(m.ie))
  62. & opt_values("v", m.v).if_missing(set(m.ve))
  63. ,
  64. option("-f").set(m.a) & opt_value("i", m.i).if_missing(set(m.ie))
  65. & opt_value("j", m.j).if_missing(set(m.je))
  66. & opt_values("v", m.v)
  67. ,
  68. option("-k").set(m.a) & value("i", m.i).if_missing(set(m.ie))
  69. ,
  70. option("-l").set(m.a) & value("i", m.i).if_missing(set(m.ie))
  71. & opt_value("j", m.j).if_missing(set(m.je))
  72. ,
  73. option("-m").set(m.a) & value("i", m.i).if_missing(set(m.ie))
  74. & opt_value("j", m.j).if_missing(set(m.je))
  75. & opt_value("k", m.k)
  76. ,
  77. option("-n").set(m.a) & values("v", m.v).if_missing(set(m.ve))
  78. ,
  79. option("-o").set(m.a) & value("i", m.i).if_missing(set(m.ie))
  80. & opt_values("v", m.v).if_missing(set(m.ve))
  81. ,
  82. option("-p").set(m.a) & value("i", m.i).if_missing(set(m.ie))
  83. & value("j", m.j).if_missing(set(m.je))
  84. & opt_values("v", m.v).if_missing(set(m.ve))
  85. ,
  86. option("-q").set(m.a) & value("i", m.i).if_missing(set(m.ie))
  87. & value("j", m.j).if_missing(set(m.je))
  88. & values("v", m.v).if_missing(set(m.ve))
  89. ,
  90. option("-r").set(m.a) & value("i", m.i).if_missing(set(m.ie))
  91. & value("j", m.j).if_missing(set(m.je))
  92. ,
  93. option("-s").set(m.a) & value("i", m.i).if_missing(set(m.ie))
  94. & value("j", m.j).if_missing(set(m.je))
  95. & opt_value("k", m.k).if_missing(set(m.ke))
  96. ,
  97. option("-t").set(m.a) & value("i", m.i).if_missing(set(m.ie))
  98. & value("j", m.j).if_missing(set(m.je))
  99. & value("k", m.k).if_missing(set(m.ke))
  100. );
  101. run_wrapped_variants({ __FILE__, lineNo }, args, cli,
  102. [&]{ m = active{}; },
  103. [&]{ return m == matches; });
  104. }
  105. //-------------------------------------------------------------------
  106. int main()
  107. {
  108. try {
  109. test(__LINE__, {""}, active{});
  110. test(__LINE__, {"-a"}, active{1, 0,0,0,0, 0,0,0,{}});
  111. test(__LINE__, {"-a", "2"}, active{1, 0,0,0,0, 2,0,0,{}});
  112. test(__LINE__, {"-b"}, active{1, 0,0,0,0, 0,0,0,{}});
  113. test(__LINE__, {"-b", "2"}, active{1, 0,0,0,0, 2,0,0,{}});
  114. test(__LINE__, {"-b", "2", "3"}, active{1, 0,0,0,0, 2,3,0,{}});
  115. test(__LINE__, {"-c"}, active{1, 0,0,0,0, 0,0,0,{}});
  116. test(__LINE__, {"-c", "2"}, active{1, 0,0,0,0, 2,0,0,{}});
  117. test(__LINE__, {"-c", "2", "3" }, active{1, 0,0,0,0, 2,3,0,{}});
  118. test(__LINE__, {"-c", "2", "3", "4"}, active{1, 0,0,0,0, 2,3,4,{}});
  119. test(__LINE__, {"-d"}, active{1, 0,0,0,0, 0,0,0,{}});
  120. test(__LINE__, {"-d", "2"}, active{1, 0,0,0,0, 0,0,0,{2}});
  121. test(__LINE__, {"-d", "2", "3"}, active{1, 0,0,0,0, 0,0,0,{2,3}});
  122. test(__LINE__, {"-d", "2", "3", "4"}, active{1, 0,0,0,0, 0,0,0,{2,3,4}});
  123. test(__LINE__, {"-d", "2", "3", "4", "5"}, active{1, 0,0,0,0, 0,0,0,{2,3,4, 5}});
  124. test(__LINE__, {"-c", "2", "-d", "3"}, active{1, 0,0,0,0, 2,0,0,{3}});
  125. test(__LINE__, {"-d", "3", "-c", "2"}, active{1, 0,0,0,0, 2,0,0,{3}});
  126. test(__LINE__, {"-e"}, active{1, 0,0,0,0, 0,0,0,{}});
  127. test(__LINE__, {"-e", "2"}, active{1, 0,0,0,0, 2,0,0,{}});
  128. test(__LINE__, {"-e", "2", "3"}, active{1, 0,0,0,0, 2,0,0,{3}});
  129. test(__LINE__, {"-e", "2", "3", "4"}, active{1, 0,0,0,0, 2,0,0,{3,4}});
  130. test(__LINE__, {"-e", "2", "3", "4", "5"}, active{1, 0,0,0,0, 2,0,0,{3,4, 5}});
  131. test(__LINE__, {"-f"}, active{1, 0,0,0,0, 0,0,0,{}});
  132. test(__LINE__, {"-f", "2"}, active{1, 0,0,0,0, 2,0,0,{}});
  133. test(__LINE__, {"-f", "2", "3"}, active{1, 0,0,0,0, 2,3,0,{}});
  134. test(__LINE__, {"-f", "2", "3", "4"}, active{1, 0,0,0,0, 2,3,0,{4}});
  135. test(__LINE__, {"-f", "2", "3", "4", "5"}, active{1, 0,0,0,0, 2,3,0,{4, 5}});
  136. test(__LINE__, {"-k"}, active{1, 1,0,0,0, 0,0,0,{}});
  137. test(__LINE__, {"-k", "2"}, active{1, 0,0,0,0, 2,0,0,{}});
  138. test(__LINE__, {"-l"}, active{1, 1,0,0,0, 0,0,0,{}});
  139. test(__LINE__, {"-l", "2"}, active{1, 0,0,0,0, 2,0,0,{}});
  140. test(__LINE__, {"-l", "2", "3"}, active{1, 0,0,0,0, 2,3,0,{}});
  141. test(__LINE__, {"-m"}, active{1, 1,0,0,0, 0,0,0,{}});
  142. test(__LINE__, {"-m", "2"}, active{1, 0,0,0,0, 2,0,0,{}});
  143. test(__LINE__, {"-m", "2", "3" }, active{1, 0,0,0,0, 2,3,0,{}});
  144. test(__LINE__, {"-m", "2", "3", "4"}, active{1, 0,0,0,0, 2,3,4,{}});
  145. test(__LINE__, {"-n"}, active{1, 0,0,0,1, 0,0,0,{}});
  146. test(__LINE__, {"-n", "2"}, active{1, 0,0,0,0, 0,0,0,{2}});
  147. test(__LINE__, {"-n", "2", "3"}, active{1, 0,0,0,0, 0,0,0,{2,3}});
  148. test(__LINE__, {"-n", "2", "3", "4"}, active{1, 0,0,0,0, 0,0,0,{2,3,4}});
  149. test(__LINE__, {"-n", "2", "3", "4", "5"}, active{1, 0,0,0,0, 0,0,0,{2,3,4, 5}});
  150. test(__LINE__, {"-o"}, active{1, 1,0,0,0, 0,0,0,{}});
  151. test(__LINE__, {"-o", "2"}, active{1, 0,0,0,0, 2,0,0,{}});
  152. test(__LINE__, {"-o", "2", "3"}, active{1, 0,0,0,0, 2,0,0,{3}});
  153. test(__LINE__, {"-o", "2", "3", "4"}, active{1, 0,0,0,0, 2,0,0,{3,4}});
  154. test(__LINE__, {"-o", "2", "3", "4", "5"}, active{1, 0,0,0,0, 2,0,0,{3,4, 5}});
  155. test(__LINE__, {"-p"}, active{1, 1,1,0,0, 0,0,0,{}});
  156. test(__LINE__, {"-p", "2"}, active{1, 0,1,0,0, 2,0,0,{}});
  157. test(__LINE__, {"-p", "2", "3"}, active{1, 0,0,0,0, 2,3,0,{}});
  158. test(__LINE__, {"-p", "2", "3", "4"}, active{1, 0,0,0,0, 2,3,0,{4}});
  159. test(__LINE__, {"-p", "2", "3", "4", "5"}, active{1, 0,0,0,0, 2,3,0,{4, 5}});
  160. test(__LINE__, {"-q"}, active{1, 1,1,0,1, 0,0,0,{}});
  161. test(__LINE__, {"-q", "2"}, active{1, 0,1,0,1, 2,0,0,{}});
  162. test(__LINE__, {"-q", "2", "3"}, active{1, 0,0,0,1, 2,3,0,{}});
  163. test(__LINE__, {"-q", "2", "3", "4"}, active{1, 0,0,0,0, 2,3,0,{4}});
  164. test(__LINE__, {"-q", "2", "3", "4", "5"}, active{1, 0,0,0,0, 2,3,0,{4, 5}});
  165. test(__LINE__, {"-r"}, active{1, 1,1,0,0, 0,0,0,{}});
  166. test(__LINE__, {"-r", "2"}, active{1, 0,1,0,0, 2,0,0,{}});
  167. test(__LINE__, {"-r", "2", "3"}, active{1, 0,0,0,0, 2,3,0,{}});
  168. test(__LINE__, {"-s"}, active{1, 1,1,0,0, 0,0,0,{}});
  169. test(__LINE__, {"-s", "2"}, active{1, 0,1,0,0, 2,0,0,{}});
  170. test(__LINE__, {"-s", "2", "3" }, active{1, 0,0,0,0, 2,3,0,{}});
  171. test(__LINE__, {"-s", "2", "3", "4"}, active{1, 0,0,0,0, 2,3,4,{}});
  172. test(__LINE__, {"-t"}, active{1, 1,1,1,0, 0,0,0,{}});
  173. test(__LINE__, {"-t", "2"}, active{1, 0,1,1,0, 2,0,0,{}});
  174. test(__LINE__, {"-t", "2", "3" }, active{1, 0,0,1,0, 2,3,0,{}});
  175. test(__LINE__, {"-t", "2", "3", "4"}, active{1, 0,0,0,0, 2,3,4,{}});
  176. }
  177. catch(std::exception& e) {
  178. std::cerr << e.what() << std::endl;
  179. return 1;
  180. }
  181. }