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.

92 lines
2.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-2019 André Müller; foss@andremueller-online.de
  8. *
  9. *****************************************************************************/
  10. #include "testing.h"
  11. //-------------------------------------------------------------------
  12. struct active {
  13. active() = default;
  14. active(bool a_, int i_): a{a_}, i{i_} {}
  15. bool a = false;
  16. int i = 0;
  17. friend bool operator == (const active& x, const active& y) noexcept {
  18. return (x.a == y.a && x.i == y.i);
  19. }
  20. };
  21. //-------------------------------------------------------------------
  22. static void
  23. test(int lineNo,
  24. const std::initializer_list<const char*> args,
  25. const active& matches )
  26. {
  27. using namespace clipp;
  28. active m;
  29. auto cli = (
  30. option("-a").set(m.a),
  31. option("-ab", "-a-b", "-a-b=") & value("i", m.i)
  32. );
  33. run_wrapped_variants({ __FILE__, lineNo }, args, cli,
  34. [&]{ m = active{}; },
  35. [&]{ return m == matches; });
  36. }
  37. //-------------------------------------------------------------------
  38. int main()
  39. {
  40. using std::string;
  41. try {
  42. test(__LINE__, {""}, active{0,0});
  43. test(__LINE__, {"-a"}, active{1,0});
  44. test(__LINE__, {"-ab"}, active{0,0});
  45. test(__LINE__, {"-a-b"}, active{0,0});
  46. test(__LINE__, {"-a-b="}, active{0,0});
  47. test(__LINE__, {"-ab", "2"}, active{0,2});
  48. test(__LINE__, {"-a-b", "3"}, active{0,3});
  49. test(__LINE__, {"-a-b=", "4"}, active{0,4});
  50. test(__LINE__, {"-ab2" }, active{0,2});
  51. test(__LINE__, {"-a-b3" }, active{0,3});
  52. test(__LINE__, {"-a-b=4"}, active{0,4});
  53. test(__LINE__, {"-a", "-ab", "2"}, active{1,2});
  54. test(__LINE__, {"-a", "-a-b", "3"}, active{1,3});
  55. test(__LINE__, {"-a", "-a-b=", "4"}, active{1,4});
  56. test(__LINE__, {"-a", "-ab2" }, active{1,2});
  57. test(__LINE__, {"-a", "-a-b3" }, active{1,3});
  58. test(__LINE__, {"-a", "-a-b=4"}, active{1,4});
  59. test(__LINE__, {"-ab", "2", "-a"}, active{1,2});
  60. test(__LINE__, {"-a-b", "3", "-a"}, active{1,3});
  61. test(__LINE__, {"-a-b=", "4", "-a"}, active{1,4});
  62. test(__LINE__, {"-a", "-ab" }, active{1,0});
  63. test(__LINE__, {"-a", "-a-b" }, active{1,0});
  64. test(__LINE__, {"-a", "-a-b="}, active{1,0});
  65. test(__LINE__, {"-ab", "-a"}, active{1,0});
  66. test(__LINE__, {"-a-b", "-a"}, active{1,0});
  67. test(__LINE__, {"-a-b=", "-a"}, active{1,0});
  68. }
  69. catch(std::exception& e) {
  70. std::cerr << e.what() << std::endl;
  71. return 1;
  72. }
  73. }