arguments_impl.hpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #pragma once
  2. #include <iostream>
  3. #include <utility>
  4. #include "arguments.h"
  5. #include "exception.h"
  6. #include "utilities.h"
  7. #ifndef PROGRAM_ARGS_NOEXIT_ON_HELP
  8. #define PROGRAM_ARGS_EXIT(X) std::exit(X)
  9. #else
  10. #define PROGRAM_ARGS_EXIT(X) (void)X
  11. #endif
  12. namespace program {
  13. template <typename Impl>
  14. template <typename T>
  15. Arguments<Impl>::Action::operator T() const {
  16. static_assert(std::is_base_of_v<Arguments<T>, T>,
  17. "Action can only bind to Arguments");
  18. if (!primed()) {
  19. self->actions.emplace(name, [](int argc, char const * const *argv) {
  20. return [action = T(argc, argv)](Arguments<Impl> const &args){
  21. return typed_main(args, action);
  22. };
  23. });
  24. }
  25. return T();
  26. }
  27. template <typename Impl> Arguments<Impl>::Action::operator bool() const {
  28. return primed() && self->arguments.size() > 0;
  29. }
  30. template <typename Impl> bool Arguments<Impl>::Action::primed() const {
  31. if (self->primed_) { return true; }
  32. if (!self->argument_names.empty()) { throw ArgumentMixingError(); }
  33. self->action_descriptions.emplace(name, description);
  34. return false;
  35. }
  36. template <typename Impl>
  37. template <typename T>
  38. Arguments<Impl>::Argument::operator T() const {
  39. if (!primed()) {
  40. return T();
  41. } else if (self->arguments.size() > index) {
  42. return convert<T>(name, self->arguments.at(index));
  43. }
  44. throw IllegalPositionError("No argument provided", index);
  45. }
  46. template <typename Impl> Arguments<Impl>::Argument::operator bool() const {
  47. return primed() && self->arguments.size() > index;
  48. }
  49. template <typename Impl>
  50. template <typename T>
  51. auto Arguments<Impl>::Argument::operator=(T && value) {
  52. is_optional = true;
  53. if (description.size()) {
  54. using ::program::to_string;
  55. description += "\n Default Value: " + to_string(value);
  56. }
  57. return WithDefault<Argument, T>{*this, std::forward<T>(value)};
  58. }
  59. template <typename Impl> bool Arguments<Impl>::Argument::primed() const {
  60. if (self->primed_) { return true; }
  61. if (!self->actions.empty()) { throw ArgumentMixingError(); }
  62. if (is_optional) {
  63. self->optional_from = std::min(self->optional_from, index);
  64. } else if (self->optional_from < index) {
  65. throw ArgumentStructureError{"Required positional after optional", name};
  66. }
  67. if (!self->argument_names.emplace(index, name).second) {
  68. throw IllegalPositionError("Duplicate positional", index);
  69. }
  70. self->argument_descriptions.emplace(name, description);
  71. return false;
  72. }
  73. }
  74. namespace program {
  75. template <typename Impl>
  76. template <typename T>
  77. Arguments<Impl>::Flag::operator T() const {
  78. return primed(false) ? static_cast<T>(self->flags.at(name)) : T();
  79. }
  80. template <typename Impl> Arguments<Impl>::Flag::operator bool() const {
  81. return primed(true) ? static_cast<bool>(self->flags.at(name)) : default_value;
  82. }
  83. template <typename Impl> auto Arguments<Impl>::Flag::operator=(bool && value) {
  84. if (description.size()) {
  85. description += "\n Default Value: ";
  86. description += (value ? "true" : "false");
  87. }
  88. default_value = value;
  89. return *this;
  90. }
  91. template <typename Impl> bool Arguments<Impl>::Flag::primed(bool inv) const {
  92. if (self->primed_) { return self->flags.count(name); }
  93. self->add_options(name, abbrev, description, std::vector(inv, "no-" + name));
  94. self->flag_names.emplace(name);
  95. return false;
  96. }
  97. }
  98. namespace program {
  99. template <typename Impl>
  100. template <typename T>
  101. Arguments<Impl>::Option::operator T() const {
  102. return (*this) ? convert<T>(name, self->options.at(name)) : T();
  103. }
  104. template <typename Impl> Arguments<Impl>::Option::operator bool() const {
  105. return primed() && self->options.count(name);
  106. }
  107. template <typename Impl>
  108. template <typename T>
  109. auto Arguments<Impl>::Option::operator=(T && value) {
  110. if (description.size()) {
  111. using ::program::to_string;
  112. description += "\n Default Value: " + to_string(value);
  113. }
  114. return WithDefault<Option, T>{*this, std::forward<T>(value)};
  115. }
  116. template <typename Impl> bool Arguments<Impl>::Option::primed() const {
  117. if (self->primed_) { return true; }
  118. self->add_options(name, abbrev, description);
  119. return false;
  120. }
  121. }
  122. namespace program {
  123. template <typename Impl>
  124. Arguments<Impl>::Arguments(int argc, char const * const * const argv) {
  125. Impl generator;
  126. *this = static_cast<Arguments const &>(generator);
  127. if (argument_names.size() &&
  128. argument_names.rbegin()->first != argument_names.size() - 1) {
  129. throw IllegalPositionError("Higher positional than number recorded",
  130. argument_names.rbegin()->first);
  131. }
  132. primed_ = true;
  133. program = argv[0];
  134. for (size_t i = 1; i < argc; ++i) {
  135. std::string arg = argv[i];
  136. char const * const next = argv[i + 1];
  137. char abbrev = arg[1];
  138. if (arg == "--help") {
  139. usage();
  140. PROGRAM_ARGS_EXIT(0);
  141. } else if (arg == "--") {
  142. arguments.insert(arguments.end(), &next, &argv[argc]);
  143. break;
  144. } else if (auto hook = actions[arg]) {
  145. main_callback = hook(argc - i, argv + i);
  146. return;
  147. } else if (arg == "help" && next && actions[next]) {
  148. char const * const help[] = {next, "--help"};
  149. main_callback = actions[next](2, help);
  150. return;
  151. } else if (arg[0] != '-') {
  152. arguments.emplace_back(arg);
  153. } else if (is_flag(arg)) {
  154. if (arg.substr(0, 5) == "--no-") {
  155. flags[id(arg)] = 0;
  156. } else {
  157. ++flags[id(arg)];
  158. }
  159. } else if (is_option(arg)) {
  160. options[id(arg)].emplace_back(argv[++i]);
  161. } else if (is_flag(abbrev) && arg.find_last_not_of("0123456789") == 1) {
  162. flags[id(abbrev)] = std::stoi(arg.substr(2));
  163. } else if (is_flag(abbrev)) {
  164. for (auto c : arg.substr(1)) {
  165. if (!is_flag(c)) { throw NotAnArgumentError({'-', c}); }
  166. ++flags[id(c)];
  167. }
  168. } else if (is_option(abbrev)) {
  169. options[id(abbrev)].emplace_back(arg.substr(2));
  170. } else {
  171. throw NotAnArgumentError(arg);
  172. }
  173. }
  174. }
  175. template <typename Impl> void Arguments<Impl>::usage() const {
  176. std::cout << program << " [options...]";
  177. for (auto & [index, name] : argument_names) {
  178. std::cout << " " << (index == optional_from ? "[" : "") << name;
  179. }
  180. if (optional_from != no_optional_args) { std::cout << "]"; }
  181. std::cout << "\nArgument Arguments:\n";
  182. for (auto & [name, desc] : argument_descriptions) {
  183. std::cout << " " << name << ": " << desc << "\n";
  184. }
  185. std::cout << "Options:\n";
  186. for (auto & [opt, desc] : option_descriptions) {
  187. std::cout << " " << opt << ": " << desc << "\n";
  188. }
  189. }
  190. template <typename Impl>
  191. void Arguments<Impl>::add_options(std::string const & name, char abbrev,
  192. std::string const & description,
  193. std::vector<std::string> aliases) {
  194. for (auto & str : aliases) {
  195. str = "--" + str;
  196. }
  197. aliases.push_back("--" + name);
  198. if (abbrev) { aliases.push_back(std::string{'-', abbrev}); }
  199. for (auto & str : aliases) {
  200. if (!option_names.emplace(str, name).second) {
  201. throw ArgumentStructureError("Duplicate option string", str);
  202. }
  203. }
  204. option_descriptions.emplace(join(",", aliases), description);
  205. }
  206. template <typename Impl>
  207. auto Arguments<Impl>::id(std::string const & arg) const -> option_id {
  208. if (!is_option(arg)) { throw NotAnArgumentError(arg); }
  209. return option_names.at(arg);
  210. }
  211. template <typename Impl>
  212. bool Arguments<Impl>::is_option(std::string const & arg) const {
  213. return option_names.count(arg);
  214. }
  215. template <typename Impl>
  216. bool Arguments<Impl>::is_flag(std::string const & arg) const {
  217. return is_option(arg) && flag_names.count(id(arg));
  218. }
  219. template <typename Impl>
  220. auto Arguments<Impl>::action(std::string const & name,
  221. std::string const & description) {
  222. return Action{this, name, description};
  223. }
  224. template <typename Impl>
  225. auto Arguments<Impl>::argument(size_t index, std::string const & name,
  226. std::string const & description) {
  227. return Argument{this, index, false, name, description};
  228. }
  229. template <typename Impl>
  230. auto Arguments<Impl>::flag(std::string const & name,
  231. std::string const & description) {
  232. return Flag{this, name, 0, description, false};
  233. }
  234. template <typename Impl>
  235. auto Arguments<Impl>::flag(std::string const & name, char abbrev,
  236. std::string const & description) {
  237. return Flag{this, name, abbrev, description, false};
  238. }
  239. template <typename Impl>
  240. auto Arguments<Impl>::option(std::string const & name,
  241. std::string const & description) {
  242. return Option{this, name, 0, description};
  243. }
  244. template <typename Impl>
  245. auto Arguments<Impl>::option(std::string const & name, char abbrev,
  246. std::string const & description) {
  247. return Option{this, name, abbrev, description};
  248. }
  249. }