arguments_impl.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #pragma once
  2. #include <utility>
  3. #include "arguments.h"
  4. #include "exception.h"
  5. #include "utilities.h"
  6. namespace program {
  7. template <typename Impl>
  8. template <typename T>
  9. Arguments<Impl>::Argument::operator T() const {
  10. if (!primed()) {
  11. return T();
  12. } else if (self->arguments.size() > index) {
  13. return convert<T>(self->arguments.at(index));
  14. }
  15. throw IllegalPositionError("No argument provided", index);
  16. }
  17. template <typename Impl> Arguments<Impl>::Argument::operator bool() const {
  18. return primed() && self->arguments.size() > index;
  19. }
  20. template <typename Impl>
  21. template <typename T>
  22. auto Arguments<Impl>::Argument::operator=(T && value) {
  23. is_optional = true;
  24. return WithDefault<Argument, T>{*this, std::forward<T>(value)};
  25. }
  26. template <typename Impl> bool Arguments<Impl>::Argument::primed() const {
  27. if (self->primed_) { return true; }
  28. if (is_optional) {
  29. self->optional_from = std::min(self->optional_from, index);
  30. } else if (self->optional_from < index) {
  31. throw ArgumentStructureError{"Required positional after optional", name};
  32. }
  33. if (!self->argument_names.emplace(index, name).second) {
  34. throw IllegalPositionError("Duplicate positional", index);
  35. }
  36. self->argument_descriptions.emplace(name, description);
  37. return false;
  38. }
  39. }
  40. namespace program {
  41. template <typename Impl>
  42. template <typename T>
  43. Arguments<Impl>::Option::operator T() const {
  44. return (*this) ? convert<T>(self->options.at(name)) : T();
  45. }
  46. template <typename Impl> Arguments<Impl>::Option::operator bool() const {
  47. return primed() && self->options.count(name);
  48. }
  49. template <typename Impl>
  50. template <typename T>
  51. auto Arguments<Impl>::Option::operator=(T && value) {
  52. return WithDefault<Option, T>{*this, std::forward<T>(value)};
  53. }
  54. template <typename Impl> bool Arguments<Impl>::Option::primed() const {
  55. if (self->primed_) { return true; }
  56. std::vector<std::string> aliases{"--" + name};
  57. if (abbrev) { aliases.emplace_back(std::string{'-', abbrev}); }
  58. for (auto & alias : aliases) {
  59. if (!self->option_name_mapping.emplace(alias, name).second) {
  60. throw ArgumentStructureError("Duplicate option string", alias);
  61. }
  62. }
  63. self->option_descriptions.emplace(join("/", aliases), description);
  64. return false;
  65. }
  66. }
  67. #define arg_equals(str) !strncmp(argv[i], str, sizeof(str))
  68. namespace program {
  69. template <typename Impl>
  70. Arguments<Impl>::Arguments(int argc, char const * const * const argv) {
  71. Impl generator;
  72. *this = static_cast<Arguments const &>(generator);
  73. if (argument_names.rbegin()->first != argument_names.size() - 1) {
  74. throw IllegalPositionError("Higher positional than number recorded",
  75. argument_names.rbegin()->first);
  76. }
  77. primed_ = true;
  78. program = argv[0];
  79. for (size_t i = 1; i < argc; ++i) {
  80. if (arg_equals("--help")) {
  81. usage();
  82. std::exit(0); // TODO: ???
  83. } else if (arg_equals("--")) {
  84. // TODO: Special arguments store for passthroughs
  85. arguments.insert(arguments.end(), &argv[i + 1], &argv[argc]);
  86. break;
  87. } else if (argv[i][0] == '-') {
  88. if (auto it = option_name_mapping.find(argv[i]);
  89. it != option_name_mapping.end()) {
  90. options[it->second].emplace_back(argv[i + 1]);
  91. } else {
  92. throw NotAnArgumentError(argv[i]);
  93. }
  94. // TODO: Arity
  95. ++i;
  96. } else {
  97. arguments.emplace_back(argv[i]);
  98. }
  99. }
  100. }
  101. template <typename Impl> void Arguments<Impl>::usage() const {
  102. std::cout << program << " [options...]";
  103. for (auto & [index, name] : argument_names) {
  104. std::cout << " " << (index == optional_from ? "[" : "") << name;
  105. }
  106. if (optional_from != std::numeric_limits<size_t>::max()) { std::cout << "]"; }
  107. std::cout << "\nArgument Arguments:\n";
  108. for (auto & [name, desc] : argument_descriptions) {
  109. std::cout << " " << name << ": " << desc << "\n";
  110. }
  111. std::cout << "Options:\n";
  112. for (auto & [opt, desc] : option_descriptions) {
  113. std::cout << " " << opt << ": " << desc << "\n";
  114. }
  115. }
  116. template <typename Impl>
  117. auto Arguments<Impl>::option(std::string const & name,
  118. std::string const & description) {
  119. return Option{this, name, 0, description};
  120. }
  121. template <typename Impl>
  122. auto Arguments<Impl>::option(std::string const & name, char abbrev,
  123. std::string const & description) {
  124. return Option{this, name, abbrev, description};
  125. }
  126. template <typename Impl>
  127. auto Arguments<Impl>::argument(size_t index, std::string const & name,
  128. std::string const & description) {
  129. return Argument{this, index, false, name, description};
  130. }
  131. }
  132. #undef arg_equals