arguments_impl.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. return (*this) ? convert<T>(self->arguments.at(index)) : T();
  11. }
  12. template <typename Impl> Arguments<Impl>::Argument::operator bool() const {
  13. bool const good = primed();
  14. if (good && self->arguments.size() <= index) {
  15. throw IllegalPositionError("No argument provided", index);
  16. }
  17. return good;
  18. }
  19. template <typename Impl>
  20. template <typename T>
  21. auto Arguments<Impl>::Argument::operator=(T && value) {
  22. is_optional = true;
  23. return WithDefault<Argument, T>{*this, std::forward<T>(value)};
  24. }
  25. template <typename Impl> bool Arguments<Impl>::Argument::primed() const {
  26. if (self->primed_) { return true; }
  27. if (is_optional) {
  28. self->optional_from = std::min(self->optional_from, index);
  29. } else if (self->optional_from < index) {
  30. throw ArgumentStructureError{"Required positional after optional", name};
  31. }
  32. if (!self->argument_names.emplace(index, name).second) {
  33. throw IllegalPositionError("Duplicate positional", index);
  34. }
  35. self->argument_descriptions.emplace(name, description);
  36. return false;
  37. }
  38. }
  39. namespace program {
  40. template <typename Impl>
  41. template <typename T>
  42. Arguments<Impl>::Option::operator T() const {
  43. return (*this) ? convert<T>(self->options.at(name)) : T();
  44. }
  45. template <typename Impl> Arguments<Impl>::Option::operator bool() const {
  46. return primed() && self->options.count(name);
  47. }
  48. template <typename Impl>
  49. template <typename T>
  50. auto Arguments<Impl>::Option::operator=(T && value) {
  51. return WithDefault<Option, T>{*this, std::forward<T>(value)};
  52. }
  53. template <typename Impl> bool Arguments<Impl>::Option::primed() const {
  54. if (self->primed_) { return true; }
  55. std::vector<std::string> aliases{"--" + name};
  56. if (abbrev) { aliases.emplace_back(std::string{'-', abbrev}); }
  57. for (auto & alias : aliases) {
  58. if (!self->option_name_mapping.emplace(alias, name).second) {
  59. throw ArgumentStructureError("Duplicate option string", alias);
  60. }
  61. }
  62. self->option_descriptions.emplace(join("/", aliases), description);
  63. return false;
  64. }
  65. }
  66. #define arg_equals(str) !strncmp(argv[i], str, sizeof(str))
  67. namespace program {
  68. template <typename Impl>
  69. Arguments<Impl>::Arguments(int argc, char const * const * const argv) {
  70. Impl generator;
  71. *this = static_cast<Arguments const &>(generator);
  72. if (argument_names.rbegin()->first != argument_names.size() - 1) {
  73. throw IllegalPositionError("Higher positional than number recorded",
  74. argument_names.rbegin()->first);
  75. }
  76. primed_ = true;
  77. program = argv[0];
  78. for (size_t i = 1; i < argc; ++i) {
  79. if (arg_equals("--help")) {
  80. usage();
  81. std::exit(0); // TODO: ???
  82. } else if (arg_equals("--")) {
  83. // TODO: Special arguments store for passthroughs
  84. arguments.insert(arguments.end(), &argv[i + 1], &argv[argc]);
  85. break;
  86. } else if (argv[i][0] == '-') {
  87. // TODO: Arity
  88. options[option_name_mapping.at(argv[i])].emplace_back(argv[i + 1]);
  89. ++i;
  90. } else {
  91. arguments.emplace_back(argv[i]);
  92. }
  93. }
  94. }
  95. template <typename Impl> void Arguments<Impl>::usage() const {
  96. std::cout << program << " [options...]";
  97. for (auto & [index, name] : argument_names) {
  98. std::cout << " " << (index == optional_from ? "[" : "") << name;
  99. }
  100. if (optional_from != std::numeric_limits<size_t>::max()) { std::cout << "]"; }
  101. std::cout << "\nArgument Arguments:\n";
  102. for (auto & [name, desc] : argument_descriptions) {
  103. std::cout << " " << name << ": " << desc << "\n";
  104. }
  105. std::cout << "Options:\n";
  106. for (auto & [opt, desc] : option_descriptions) {
  107. std::cout << " " << opt << ": " << desc << "\n";
  108. }
  109. }
  110. template <typename Impl>
  111. auto Arguments<Impl>::option(std::string const & name,
  112. std::string const & description) {
  113. return Option{this, name, 0, description};
  114. }
  115. template <typename Impl>
  116. auto Arguments<Impl>::option(std::string const & name, char abbrev,
  117. std::string const & description) {
  118. return Option{this, name, abbrev, description};
  119. }
  120. template <typename Impl>
  121. auto Arguments<Impl>::argument(size_t index, std::string const & name,
  122. std::string const & description) {
  123. return Argument{this, index, false, name, description};
  124. }
  125. }
  126. #undef arg_equals