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.

70 lines
2.1 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 <clipp.h>
  13. int main(int argc, char* argv[])
  14. {
  15. using namespace clipp;
  16. using std::cout;
  17. std::string file;
  18. bool readonly = false;
  19. bool usebackup = false;
  20. bool useswap = false;
  21. enum class editor {vim, sublime3, atom, emacs};
  22. std::vector<editor> editors;
  23. auto add = [&](editor e){ return [&]{ editors.push_back(e); }; };
  24. auto cli = (
  25. value("file", file),
  26. joinable(
  27. option("-r").set(readonly) % "open read-only",
  28. option("-b").set(usebackup) % "use backup file",
  29. option("-s").set(useswap) % "use swap file"
  30. ),
  31. joinable(
  32. option(":vim") >> add(editor::vim),
  33. option(":st3") >> add(editor::sublime3),
  34. option(":atom") >> add(editor::atom),
  35. option(":emacs") >> add(editor::emacs)
  36. ) % "editor(s) to use; multiple possible"
  37. );
  38. if(parse(argc, argv, cli)) {
  39. cout << "open " << file
  40. << (readonly ? " read-only" : "")
  41. << (usebackup ? " using backup" : "")
  42. << (useswap ? " using swap" : "");
  43. if(!editors.empty()) {
  44. bool first = true;
  45. cout << "\nwith editor(s): ";
  46. for(auto e : editors) {
  47. if(first) first = false; else cout << ", ";
  48. switch(e) {
  49. case editor::vim: cout << "vim"; break;
  50. case editor::sublime3: cout << "Sublime Text 3"; break;
  51. case editor::atom: cout << "Atom"; break;
  52. case editor::emacs: cout << "GNU Emacs"; break;
  53. }
  54. }
  55. }
  56. cout << '\n';
  57. }
  58. else {
  59. cout << make_man_page(cli, argv[0]) << '\n';
  60. }
  61. }