arguments_impl.hpp 7.2 KB

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