🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
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.

168 lines
4.9 KiB

  1. /*
  2. Copyright (c) 2014 Jarryd Beck
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include <iostream>
  20. #include "cxxopts.hpp"
  21. void
  22. parse(int argc, char* argv[])
  23. {
  24. try
  25. {
  26. cxxopts::Options options(argv[0], " - example command line options");
  27. options
  28. .positional_help("[optional args]")
  29. .show_positional_help();
  30. bool apple = false;
  31. options
  32. .allow_unrecognised_options()
  33. .add_options()
  34. ("a,apple", "an apple", cxxopts::value<bool>(apple))
  35. ("b,bob", "Bob")
  36. ("char", "A character", cxxopts::value<char>())
  37. ("t,true", "True", cxxopts::value<bool>()->default_value("true"))
  38. ("f, file", "File", cxxopts::value<std::vector<std::string>>(), "FILE")
  39. ("i,input", "Input", cxxopts::value<std::string>())
  40. ("o,output", "Output file", cxxopts::value<std::string>()
  41. ->default_value("a.out")->implicit_value("b.def"), "BIN")
  42. ("x", "A short-only option", cxxopts::value<std::string>())
  43. ("positional",
  44. "Positional arguments: these are the arguments that are entered "
  45. "without an option", cxxopts::value<std::vector<std::string>>())
  46. ("long-description",
  47. "thisisareallylongwordthattakesupthewholelineandcannotbebrokenataspace")
  48. ("help", "Print help")
  49. ("int", "An integer", cxxopts::value<int>(), "N")
  50. ("float", "A floating point number", cxxopts::value<float>())
  51. ("vector", "A list of doubles", cxxopts::value<std::vector<double>>())
  52. ("option_that_is_too_long_for_the_help", "A very long option")
  53. #ifdef CXXOPTS_USE_UNICODE
  54. ("unicode", u8"A help option with non-ascii: à. Here the size of the"
  55. " string should be correct")
  56. #endif
  57. ;
  58. options.add_options("Group")
  59. ("c,compile", "compile")
  60. ("d,drop", "drop", cxxopts::value<std::vector<std::string>>());
  61. options.parse_positional({"input", "output", "positional"});
  62. auto result = options.parse(argc, argv);
  63. if (result.count("help"))
  64. {
  65. std::cout << options.help({"", "Group"}) << std::endl;
  66. exit(0);
  67. }
  68. if (apple)
  69. {
  70. std::cout << "Saw option ‘a’ " << result.count("a") << " times " <<
  71. std::endl;
  72. }
  73. if (result.count("b"))
  74. {
  75. std::cout << "Saw option ‘b’" << std::endl;
  76. }
  77. if (result.count("char"))
  78. {
  79. std::cout << "Saw a character ‘" << result["char"].as<char>() << "" << std::endl;
  80. }
  81. if (result.count("f"))
  82. {
  83. auto& ff = result["f"].as<std::vector<std::string>>();
  84. std::cout << "Files" << std::endl;
  85. for (const auto& f : ff)
  86. {
  87. std::cout << f << std::endl;
  88. }
  89. }
  90. if (result.count("input"))
  91. {
  92. std::cout << "Input = " << result["input"].as<std::string>()
  93. << std::endl;
  94. }
  95. if (result.count("output"))
  96. {
  97. std::cout << "Output = " << result["output"].as<std::string>()
  98. << std::endl;
  99. }
  100. if (result.count("positional"))
  101. {
  102. std::cout << "Positional = {";
  103. auto& v = result["positional"].as<std::vector<std::string>>();
  104. for (const auto& s : v) {
  105. std::cout << s << ", ";
  106. }
  107. std::cout << "}" << std::endl;
  108. }
  109. if (result.count("int"))
  110. {
  111. std::cout << "int = " << result["int"].as<int>() << std::endl;
  112. }
  113. if (result.count("float"))
  114. {
  115. std::cout << "float = " << result["float"].as<float>() << std::endl;
  116. }
  117. if (result.count("vector"))
  118. {
  119. std::cout << "vector = ";
  120. const auto values = result["vector"].as<std::vector<double>>();
  121. for (const auto& v : values) {
  122. std::cout << v << ", ";
  123. }
  124. std::cout << std::endl;
  125. }
  126. std::cout << "Arguments remain = " << argc << std::endl;
  127. auto arguments = result.arguments();
  128. std::cout << "Saw " << arguments.size() << " arguments" << std::endl;
  129. }
  130. catch (const cxxopts::OptionException& e)
  131. {
  132. std::cout << "error parsing options: " << e.what() << std::endl;
  133. exit(1);
  134. }
  135. }
  136. int main(int argc, char* argv[])
  137. {
  138. parse(argc, argv);
  139. return 0;
  140. }