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.

81 lines
2.6 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 verbose = false;
  19. bool init = true;
  20. int bsize = 1024;
  21. enum class mode {help, build, query};
  22. bool add = false;
  23. enum class fmt {plain, json, xml};
  24. mode selected;
  25. std::vector<std::string> infiles;
  26. std::string outfile;
  27. std::string outfmt;
  28. auto cli = (
  29. command("help")
  30. | ( command("build").set(selected,mode::help),
  31. ( command("new").set(selected,mode::build).set(add,false)
  32. | command("add").set(selected,mode::build).set(add,true) ),
  33. values("file", infiles),
  34. option("-v", "--verbose").set(verbose) % "print detailed report",
  35. option("-b", "--buffer") & opt_value(
  36. "size="+std::to_string(bsize), bsize) % "sets buffer size in KiByte",
  37. ( option("--init").set(init)
  38. | option("--no-init").set(init,false) ) % "do or don't initialize"
  39. )
  40. | ( command("query").set(selected,mode::query),
  41. value("infile", infiles),
  42. required("-o", "--out") & value("outfile", outfile),
  43. option("-f", "--out-format") % "determine output format"
  44. & value("format",outfmt)
  45. )
  46. );
  47. if(parse(argc, argv, cli)) {
  48. switch(selected) {
  49. default:
  50. case mode::help:
  51. cout << make_man_page(cli, argv[0]) << '\n'; return 0;
  52. case mode::build:
  53. if(add)
  54. cout << "adding to database\n";
  55. else
  56. cout << "building new database\n";
  57. cout << "buffer size is " << bsize << '\n';
  58. if(init) cout << "showing detailed information\n";
  59. break;
  60. case mode::query:
  61. cout << "query database\n";
  62. cout << "output to " << outfile << " in format " << outfmt << '\n';
  63. break;
  64. }
  65. cout << "input files:";
  66. for(const auto& f : infiles) cout << ' ' << f;
  67. cout << "\n";
  68. if(verbose) cout << "showing detailed information\n";
  69. }
  70. else {
  71. cout << usage_lines(cli, argv[0]) << '\n';
  72. }
  73. }