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.

51 lines
1.5 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. std::vector<std::string> wrong;
  19. auto istarget = match::prefix_not("-");
  20. auto cli = (
  21. value("file")
  22. .if_missing([]{ cout << "You need to provide a source filename!\n"; } )
  23. .if_repeated([](int idx){ cout << "Only one source file allowed! (index " << idx << ")\n"; } )
  24. ,
  25. required("-t") & values(istarget, "target")
  26. .if_missing([]{ cout << "You need to provide at least one target filename!\n"; } )
  27. .if_blocked([]{ cout << "Target names must not be given before the file command and the source file name!\n"; })
  28. ,
  29. option("--http") | option("--ftp")
  30. .if_conflicted([]{ cout << "You can only use one protocol at a time!\n"; } )
  31. ,
  32. any_other(wrong)
  33. );
  34. if(parse(argc, argv, cli) && wrong.empty()) {
  35. cout << "OK\n";
  36. }
  37. else {
  38. for(const auto& arg : wrong) {
  39. cout << "'" << arg << "' is not a valid command line argument\n";
  40. }
  41. cout << "Usage:\n" << usage_lines(cli,argv[0]) << '\n';
  42. }
  43. }