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.

58 lines
1.8 KiB

2 years ago
  1. /*****************************************************************************
  2. *
  3. * demo program - part of 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 <iostream>
  11. #include <string>
  12. #include <vector>
  13. #include <clipp.h>
  14. int main(int argc, char* argv[])
  15. {
  16. using namespace clipp;
  17. using std::cout;
  18. bool a = false, b = false;
  19. int i = 1, n = 0, m = 0;
  20. float x = 0.0f;
  21. auto cli = ( //INFORMAL description
  22. option("-a").set(a), //if(found("-a")) a = true;
  23. option("-b") >> b, //if(found("-b")) b = true;
  24. option("--toggle").call(flip(b)), //if(found("--toggle")) flip(b);
  25. //if(found("-z")) call_lambda("-z");
  26. option("-y").call([](const char* s) { cout << s << '\n'; }),
  27. //using 'operator()' instead of 'call'
  28. //if(found("bob")) call_lambda("bob");
  29. option("bob")([](std::string s) { cout << s; }),
  30. //for_each_occurence("-x arg", call_lambda(arg));
  31. option("-x") & values("X")([&](const char* s) { x = std::stof(s); }),
  32. //if(parsing_error_on("-z") call_lambda(get_errors())
  33. required("-z").if_missing([](){ cout << "-z is missing\n"; }),
  34. option("--all")
  35. >> []() { cout << "found --all\n"; }
  36. >> [](const char* s) { cout << "found flag " << s << '\n'; },
  37. value("n").set(n), //n = std::atoi(arg);
  38. option("-i") & value("#",i), //if(found("-i arg")) i = std::atoi(arg);
  39. option("-1").set(m,1), //if(found("-1")) m = 1;
  40. option("-2").set(m,2) //if(found("-2")) m = 2;
  41. );
  42. parse(argc, argv, cli);
  43. }