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.

111 lines
3.1 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_):
  16. a{a_}, b{b_}, c{c_}, d{d_}
  17. {}
  18. bool a = false, b = false, c = false, d = false;
  19. bool conflict = false;
  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 &&
  22. x.d == y.d && x.conflict == y.conflict);
  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. {
  32. active m;
  33. auto cli = option("-a").set(m.a) |
  34. option("-b").set(m.b) |
  35. option("-c").set(m.c) |
  36. option("-d").set(m.d);
  37. auto res = parse(args, cli);
  38. m.conflict = res.any_conflict();
  39. if(!(m == matches)) {
  40. throw std::runtime_error{"failed " + std::string( __FILE__ ) +
  41. " #1 in line " + std::to_string(lineNo)};
  42. }
  43. }
  44. {
  45. active m;
  46. auto cli = (
  47. option("?????"),
  48. (
  49. option("-a").set(m.a) |
  50. option("-b").set(m.b) |
  51. option("-c").set(m.c) |
  52. option("-d").set(m.d)
  53. )
  54. );
  55. auto res = parse(args, cli);
  56. m.conflict = res.any_conflict();
  57. if(!(m == matches)) {
  58. throw std::runtime_error{"failed " + std::string( __FILE__ ) +
  59. " #2 in line " + std::to_string(lineNo)};
  60. }
  61. }
  62. }
  63. //-------------------------------------------------------------------
  64. int main()
  65. {
  66. try {
  67. test(__LINE__, {""}, active{});
  68. test(__LINE__, {"-a"}, active{1,0,0,0});
  69. test(__LINE__, {"-b"}, active{0,1,0,0});
  70. test(__LINE__, {"-c"}, active{0,0,1,0});
  71. test(__LINE__, {"-d"}, active{0,0,0,1});
  72. {
  73. active e; e.conflict = true;
  74. test(__LINE__, {"-a","-b"}, e);
  75. test(__LINE__, {"-b","-a"}, e);
  76. test(__LINE__, {"-c","-b"}, e);
  77. test(__LINE__, {"-d","-c"}, e);
  78. test(__LINE__, {"-a","-d"}, e);
  79. test(__LINE__, {"-a","-b","-c"}, e);
  80. test(__LINE__, {"-c","-b","-a"}, e);
  81. test(__LINE__, {"-d","-a","-c"}, e);
  82. test(__LINE__, {"-a","-b","-c", "-d"}, e);
  83. test(__LINE__, {"-d","-c","-b", "-a"}, e);
  84. test(__LINE__, {"-c","-b","-a", "-d"}, e);
  85. test(__LINE__, {"-d","-a","-c", "-b"}, e);
  86. }
  87. }
  88. catch(std::exception& e) {
  89. std::cerr << e.what() << std::endl;
  90. return 1;
  91. }
  92. }