arguments_impl.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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>::Flag::operator T() const {
  44. return primed(false) ? static_cast<T>(self->flags.at(name)) : T();
  45. }
  46. template <typename Impl> Arguments<Impl>::Flag::operator bool() const {
  47. return primed(true) ? static_cast<bool>(self->flags.at(name)) : default_value;
  48. }
  49. template <typename Impl> auto Arguments<Impl>::Flag::operator=(bool && value) {
  50. default_value = value;
  51. return *this;
  52. }
  53. template <typename Impl> bool Arguments<Impl>::Flag::primed(bool inv) const {
  54. if (self->primed_) { return self->flags.count(name); }
  55. self->add_options(name, abbrev, description, std::vector(inv, "no-" + name));
  56. self->flag_names.emplace(name);
  57. return false;
  58. }
  59. }
  60. namespace program {
  61. template <typename Impl>
  62. template <typename T>
  63. Arguments<Impl>::Option::operator T() const {
  64. return (*this) ? convert<T>(self->options.at(name)) : T();
  65. }
  66. template <typename Impl> Arguments<Impl>::Option::operator bool() const {
  67. return primed() && self->options.count(name);
  68. }
  69. template <typename Impl>
  70. template <typename T>
  71. auto Arguments<Impl>::Option::operator=(T && value) {
  72. return WithDefault<Option, T>{*this, std::forward<T>(value)};
  73. }
  74. template <typename Impl> bool Arguments<Impl>::Option::primed() const {
  75. if (self->primed_) { return true; }
  76. self->add_options(name, abbrev, description);
  77. return false;
  78. }
  79. }
  80. #define arg_equals(str) !strncmp(argv[i], str, sizeof(str))
  81. #define arg_starts_with(str) !strncmp(argv[i], str, strlen(str))
  82. namespace program {
  83. template <typename Impl>
  84. Arguments<Impl>::Arguments(int argc, char const * const * const argv) {
  85. Impl generator;
  86. *this = static_cast<Arguments const &>(generator);
  87. if (argument_names.rbegin()->first != argument_names.size() - 1) {
  88. throw IllegalPositionError("Higher positional than number recorded",
  89. argument_names.rbegin()->first);
  90. }
  91. primed_ = true;
  92. program = argv[0];
  93. for (size_t i = 1; i < argc; ++i) {
  94. if (arg_equals("--help")) {
  95. usage();
  96. std::exit(0); // TODO: ???
  97. } else if (arg_equals("--")) {
  98. // TODO: Special arguments store for passthroughs
  99. arguments.insert(arguments.end(), &argv[i + 1], &argv[argc]);
  100. break;
  101. } else if (argv[i][0] != '-') {
  102. arguments.emplace_back(argv[i]);
  103. } else if (!is_flag(argv[i])) {
  104. options[id(argv[i])].emplace_back(argv[i]);
  105. ++i;
  106. } else {
  107. // TODO: Flag handling for e.g. -v2 -vv
  108. if (arg_starts_with("--no-")) {
  109. flags[id(argv[i])] = 0;
  110. } else {
  111. ++flags[id(argv[i])];
  112. }
  113. }
  114. }
  115. }
  116. template <typename Impl> void Arguments<Impl>::usage() const {
  117. std::cout << program << " [options...]";
  118. for (auto & [index, name] : argument_names) {
  119. std::cout << " " << (index == optional_from ? "[" : "") << name;
  120. }
  121. if (optional_from != std::numeric_limits<size_t>::max()) { std::cout << "]"; }
  122. std::cout << "\nArgument Arguments:\n";
  123. for (auto & [name, desc] : argument_descriptions) {
  124. std::cout << " " << name << ": " << desc << "\n";
  125. }
  126. std::cout << "Options:\n";
  127. for (auto & [opt, desc] : option_descriptions) {
  128. std::cout << " " << opt << ": " << desc << "\n";
  129. }
  130. }
  131. template <typename Impl>
  132. void Arguments<Impl>::add_options(std::string const & name, char abbrev,
  133. std::string const & description,
  134. std::vector<std::string> aliases) {
  135. for (auto & str : aliases) {
  136. str = "--" + str;
  137. }
  138. aliases.push_back("--" + name);
  139. if (abbrev) { aliases.push_back(std::string{'-', abbrev}); }
  140. for (auto & str : aliases) {
  141. if (!option_names.emplace(str, name).second) {
  142. throw ArgumentStructureError("Duplicate option string", str);
  143. }
  144. }
  145. option_descriptions.emplace(join(",", aliases), description);
  146. }
  147. template <typename Impl>
  148. auto Arguments<Impl>::id(std::string const & arg) const -> option_id {
  149. if (!is_option(arg)) { throw NotAnArgumentError(arg); }
  150. return option_names.at(arg);
  151. }
  152. template <typename Impl>
  153. bool Arguments<Impl>::is_option(std::string const & arg) const {
  154. return option_names.count(arg);
  155. }
  156. template <typename Impl>
  157. bool Arguments<Impl>::is_flag(std::string const & arg) const {
  158. return is_option(arg) && flag_names.count(id(arg));
  159. }
  160. template <typename Impl>
  161. auto Arguments<Impl>::argument(size_t index, std::string const & name,
  162. std::string const & description) {
  163. return Argument{this, index, false, name, description};
  164. }
  165. template <typename Impl>
  166. auto Arguments<Impl>::flag(std::string const & name,
  167. std::string const & description) {
  168. return Flag{this, name, 0, description, false};
  169. }
  170. template <typename Impl>
  171. auto Arguments<Impl>::flag(std::string const & name, char abbrev,
  172. std::string const & description) {
  173. return Flag{this, name, abbrev, description, false};
  174. }
  175. template <typename Impl>
  176. auto Arguments<Impl>::option(std::string const & name,
  177. std::string const & description) {
  178. return Option{this, name, 0, description};
  179. }
  180. template <typename Impl>
  181. auto Arguments<Impl>::option(std::string const & name, char abbrev,
  182. std::string const & description) {
  183. return Option{this, name, abbrev, description};
  184. }
  185. }
  186. #undef arg_equals
  187. #undef arg_starts_with