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.

150 lines
5.6 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_, int i_ = 0) :
  16. a{a_}, b{b_}, c{c_}, d{d_}, e{e_}, f{f_}, i{i_}
  17. {}
  18. bool a = false, b = false, c = false, d = false, e = false, f = false;
  19. int i = 0;
  20. friend bool operator == (const active& x, const active& y) noexcept {
  21. return (x.a == y.a && x.b == y.b && x.c == y.c && x.d == y.d &&
  22. x.e == y.e && x.f == y.f && x.i == y.i);
  23. }
  24. };
  25. //-------------------------------------------------------------------
  26. void test(int lineNo,
  27. const std::initializer_list<const char*> args,
  28. const active& matches)
  29. {
  30. using namespace clipp;
  31. active m;
  32. auto cli = (
  33. ( command("a"),
  34. option("-x").set(m.a)
  35. ).doc("group (~a,-x)")
  36. |
  37. ( command("b"),
  38. option("-x").set(m.b),
  39. ( command("u").set(m.i,1) |
  40. command("v").set(m.i,2) |
  41. command("w").set(m.i,3)
  42. ).doc("group (u|v|w)"),
  43. option("-e").set(m.e)
  44. ).doc("group (~b,-x,(~u|~v|~w),-e)")
  45. |
  46. ( command("c"),
  47. option("-x").set(m.c),
  48. command("d").set(m.d),
  49. required("-i").set(m.e) & value("i", m.i),
  50. option("-f").set(m.f)
  51. ).doc("group (~c,-x,d,(~-i,~i),-f)")
  52. );
  53. run_wrapped_variants({ __FILE__, lineNo }, args, cli,
  54. [&]{ m = active{}; },
  55. [&]{ return m == matches; });
  56. }
  57. //-------------------------------------------------------------------
  58. int main()
  59. {
  60. try {
  61. test(__LINE__, {""}, active{});
  62. test(__LINE__, {"a"}, active{});
  63. test(__LINE__, {"b"}, active{});
  64. test(__LINE__, {"c"}, active{});
  65. test(__LINE__, {"-x"}, active{});
  66. test(__LINE__, {"a", "-x"}, active{1,0,0,0,0,0, 0});
  67. test(__LINE__, {"b", "-x"}, active{0,1,0,0,0,0, 0});
  68. test(__LINE__, {"c", "-x"}, active{0,0,1,0,0,0, 0});
  69. test(__LINE__, {"b", "u"}, active{0,0,0,0,0,0, 1});
  70. test(__LINE__, {"b", "v"}, active{0,0,0,0,0,0, 2});
  71. test(__LINE__, {"b", "w"}, active{0,0,0,0,0,0, 3});
  72. test(__LINE__, {"b", "-x", "u"}, active{0,1,0,0,0,0, 1});
  73. test(__LINE__, {"b", "-x", "v"}, active{0,1,0,0,0,0, 2});
  74. test(__LINE__, {"b", "-x", "w"}, active{0,1,0,0,0,0, 3});
  75. test(__LINE__, {"b", "u", "-x"}, active{0,0,0,0,0,0, 1});
  76. test(__LINE__, {"b", "v", "-x"}, active{0,0,0,0,0,0, 2});
  77. test(__LINE__, {"b", "w", "-x"}, active{0,0,0,0,0,0, 3});
  78. test(__LINE__, {"b", "-x", "u", "-e"}, active{0,1,0,0,1,0, 1});
  79. test(__LINE__, {"b", "-x", "v", "-e"}, active{0,1,0,0,1,0, 2});
  80. test(__LINE__, {"b", "-x", "w", "-e"}, active{0,1,0,0,1,0, 3});
  81. test(__LINE__, {"b", "u", "-x", "-e"}, active{0,0,0,0,1,0, 1});
  82. test(__LINE__, {"b", "v", "-x", "-e"}, active{0,0,0,0,1,0, 2});
  83. test(__LINE__, {"b", "w", "-x", "-e"}, active{0,0,0,0,1,0, 3});
  84. test(__LINE__, {"b", "-x", "u", "-e"}, active{0,1,0,0,1,0, 1});
  85. test(__LINE__, {"b", "-x", "v", "-e"}, active{0,1,0,0,1,0, 2});
  86. test(__LINE__, {"b", "-x", "w", "-e"}, active{0,1,0,0,1,0, 3});
  87. test(__LINE__, {"b", "-x", "-e", "u"}, active{0,1,0,0,0,0, 1});
  88. test(__LINE__, {"b", "-x", "-e", "v"}, active{0,1,0,0,0,0, 2});
  89. test(__LINE__, {"b", "-x", "-e", "w"}, active{0,1,0,0,0,0, 3});
  90. test(__LINE__, {"c", "-x", "-i"}, active{0,0,1,0,0,0, 0});
  91. test(__LINE__, {"c", "-x", "-f"}, active{0,0,1,0,0,0, 0});
  92. test(__LINE__, {"c", "-x", "123"}, active{0,0,1,0,0,0, 0});
  93. test(__LINE__, {"c", "-x", "-i", "123"}, active{0,0,1,0,0,0, 0});
  94. test(__LINE__, {"c", "-x", "-i", "-f"}, active{0,0,1,0,0,0, 0});
  95. test(__LINE__, {"c", "-x", "123", "-i"}, active{0,0,1,0,0,0, 0});
  96. test(__LINE__, {"c", "-x", "123", "-f"}, active{0,0,1,0,0,0, 0});
  97. test(__LINE__, {"c", "-x", "-i", "123", "-f"}, active{0,0,1,0,0,0, 0});
  98. test(__LINE__, {"c", "-x", "d"}, active{0,0,1,1,0,0, 0});
  99. test(__LINE__, {"c", "-x", "d", "-i"}, active{0,0,1,1,1,0, 0});
  100. test(__LINE__, {"c", "-x", "d", "-f"}, active{0,0,1,1,0,1, 0});
  101. test(__LINE__, {"c", "-x", "d", "123"}, active{0,0,1,1,0,0, 0});
  102. test(__LINE__, {"c", "-x", "d", "-i", "123"}, active{0,0,1,1,1,0, 123});
  103. test(__LINE__, {"c", "-x", "d", "-i", "-f"}, active{0,0,1,1,1,1, 0});
  104. test(__LINE__, {"c", "-x", "d", "123", "-i"}, active{0,0,1,1,1,0, 0});
  105. test(__LINE__, {"c", "-x", "d", "123", "-f"}, active{0,0,1,1,0,1, 0});
  106. test(__LINE__, {"c", "-x", "d", "-i", "123", "-f"}, active{0,0,1,1,1,1, 123});
  107. test(__LINE__, {"c", "-x", "d", "-f", "-i"}, active{0,0,1,1,1,1, 0});
  108. test(__LINE__, {"c", "-x", "d", "-f", "-i", "123"}, active{0,0,1,1,1,1, 123});
  109. test(__LINE__, {"c", "d", "-f", "-i", "123"}, active{0,0,0,1,1,1, 123});
  110. test(__LINE__, {"c", "d", "-i", "123", "-f"}, active{0,0,0,1,1,1, 123});
  111. test(__LINE__, {"c", "d", "-x", "-i", "123", "-f"}, active{0,0,0,1,1,1, 123});
  112. test(__LINE__, {"c", "d", "-f", "-i", "123", "-x"}, active{0,0,0,1,1,1, 123});
  113. }
  114. catch(std::exception& e) {
  115. std::cerr << e.what() << std::endl;
  116. return 1;
  117. }
  118. }