arguments_impl.hpp 7.1 KB

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