arguments_impl.hpp 8.6 KB

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