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.

95 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_, bool e_, bool f_):
  16. a{a_}, b{b_}, c{c_}, d{d_}, e{e_}, f{f_}
  17. {}
  18. bool a = false, b = false, c = false, d = false, e = false, f = false;
  19. friend bool operator == (const active& x, const active& y) noexcept {
  20. return (x.a == y.a && x.b == y.b && x.c == y.c && x.d == y.d &&
  21. x.e == y.e && x.f == y.f);
  22. }
  23. };
  24. //-------------------------------------------------------------------
  25. void test(int lineNo,
  26. const std::initializer_list<const char*> args,
  27. const active& matches)
  28. {
  29. using namespace clipp;
  30. active m;
  31. auto cli = (
  32. option("-a", "--aaa").set(m.a),
  33. option("-b", "--bee").set(m.b),
  34. option("-c", "--cee").set(m.c),
  35. option("-d", "--dee").set(m.d),
  36. option("-e", "--eee").set(m.e),
  37. option("-f", "--eff").set(m.f) );
  38. run_wrapped_variants({ __FILE__, lineNo }, args, cli,
  39. [&]{ m = active{}; },
  40. [&]{ return m == matches; });
  41. }
  42. //-------------------------------------------------------------------
  43. int main()
  44. {
  45. try {
  46. test(__LINE__, {""}, active{});
  47. //single option presence - primary flags
  48. test(__LINE__, {"-a"}, active{1,0,0,0,0,0});
  49. test(__LINE__, {"-b"}, active{0,1,0,0,0,0});
  50. test(__LINE__, {"-c"}, active{0,0,1,0,0,0});
  51. test(__LINE__, {"-d"}, active{0,0,0,1,0,0});
  52. test(__LINE__, {"-e"}, active{0,0,0,0,1,0});
  53. test(__LINE__, {"-f"}, active{0,0,0,0,0,1});
  54. //single option presence - secondary flags
  55. test(__LINE__, {"--aaa"}, active{1,0,0,0,0,0});
  56. test(__LINE__, {"--bee"}, active{0,1,0,0,0,0});
  57. test(__LINE__, {"--cee"}, active{0,0,1,0,0,0});
  58. test(__LINE__, {"--dee"}, active{0,0,0,1,0,0});
  59. test(__LINE__, {"--eee"}, active{0,0,0,0,1,0});
  60. test(__LINE__, {"--eff"}, active{0,0,0,0,0,1});
  61. //all (varying sequence)
  62. test(__LINE__, {"-a", "-b", "-c", "-d", "-e", "-f"}, active{1,1,1,1,1,1});
  63. test(__LINE__, {"-c", "-d", "-a", "-f", "-e", "-b"}, active{1,1,1,1,1,1});
  64. test(__LINE__, {"--aaa", "--bee", "--cee", "--dee", "--eee", "--eff"},
  65. active{1,1,1,1,1,1});
  66. test(__LINE__, {"--eee", "--bee", "--eff", "--dee", "--aaa", "--cee"},
  67. active{1,1,1,1,1,1});
  68. //several options
  69. test(__LINE__, {"-b", "--aaa", "-f", "--dee"}, active{1,1,0,1,0,1});
  70. test(__LINE__, {"-f", "-a", "-b", "--dee"}, active{1,1,0,1,0,1});
  71. //option repeat is ignored
  72. test(__LINE__, {"-b", "-a", "--bee", "--aaa"}, active{1,1,0,0,0,0});
  73. }
  74. catch(std::exception& e) {
  75. std::cerr << e.what() << std::endl;
  76. return 1;
  77. }
  78. }