|
|
@@ -14,25 +14,21 @@ Arguments<Impl>::Option::operator T() const {
|
|
|
return (*this) ? convert<T>(self->options.at(name)) : T();
|
|
|
}
|
|
|
|
|
|
-template <typename Impl>
|
|
|
-Arguments<Impl>::Option::operator bool() const {
|
|
|
+template <typename Impl> Arguments<Impl>::Option::operator bool() const {
|
|
|
return primed() && self->options.count(name);
|
|
|
}
|
|
|
|
|
|
template <typename Impl>
|
|
|
template <typename T>
|
|
|
-auto Arguments<Impl>::Option::operator=(T &&value) {
|
|
|
+auto Arguments<Impl>::Option::operator=(T && value) {
|
|
|
return WithDefault<Option, T>{*this, std::forward<T>(value)};
|
|
|
}
|
|
|
|
|
|
-template <typename Impl>
|
|
|
-bool Arguments<Impl>::Option::primed() const {
|
|
|
+template <typename Impl> bool Arguments<Impl>::Option::primed() const {
|
|
|
if (self->primed_) { return true; }
|
|
|
std::vector<std::string> aliases{"--" + name};
|
|
|
- if (abbrev) {
|
|
|
- aliases.emplace_back(std::string{'-', abbrev});
|
|
|
- }
|
|
|
- for (auto &alias : aliases) {
|
|
|
+ if (abbrev) { aliases.emplace_back(std::string{'-', abbrev}); }
|
|
|
+ for (auto & alias : aliases) {
|
|
|
if (!self->option_name_mapping.emplace(alias, name).second) {
|
|
|
throw ArgumentStructureError("Duplicate option string", alias);
|
|
|
}
|
|
|
@@ -51,8 +47,7 @@ Arguments<Impl>::Positional::operator T() const {
|
|
|
return (*this) ? convert<T>(self->arguments.at(index)) : T();
|
|
|
}
|
|
|
|
|
|
-template <typename Impl>
|
|
|
-Arguments<Impl>::Positional::operator bool() const {
|
|
|
+template <typename Impl> Arguments<Impl>::Positional::operator bool() const {
|
|
|
bool const good = primed();
|
|
|
if (good && self->arguments.size() <= index) {
|
|
|
throw IllegalPositionError("No argument provided", index);
|
|
|
@@ -62,13 +57,12 @@ Arguments<Impl>::Positional::operator bool() const {
|
|
|
|
|
|
template <typename Impl>
|
|
|
template <typename T>
|
|
|
-auto Arguments<Impl>::Positional::operator=(T &&value) {
|
|
|
+auto Arguments<Impl>::Positional::operator=(T && value) {
|
|
|
is_optional = true;
|
|
|
return WithDefault<Positional, T>{*this, std::forward<T>(value)};
|
|
|
}
|
|
|
|
|
|
-template <typename Impl>
|
|
|
-bool Arguments<Impl>::Positional::primed() const {
|
|
|
+template <typename Impl> bool Arguments<Impl>::Positional::primed() const {
|
|
|
if (self->primed_) { return true; }
|
|
|
if (is_optional) {
|
|
|
self->optional_from = std::min(self->optional_from, index);
|
|
|
@@ -90,7 +84,7 @@ namespace program {
|
|
|
template <typename Impl>
|
|
|
Arguments<Impl>::Arguments(int argc, char const * const * const argv) {
|
|
|
Impl generator;
|
|
|
- *this = static_cast<Arguments const&>(generator);
|
|
|
+ *this = static_cast<Arguments const &>(generator);
|
|
|
if (argument_names.rbegin()->first != argument_names.size() - 1) {
|
|
|
throw IllegalPositionError("Higher positional than number recorded",
|
|
|
argument_names.rbegin()->first);
|
|
|
@@ -104,11 +98,11 @@ Arguments<Impl>::Arguments(int argc, char const * const * const argv) {
|
|
|
std::exit(0); // TODO: ???
|
|
|
} else if (arg_equals("--")) {
|
|
|
// TODO: Special arguments store for passthroughs
|
|
|
- arguments.insert(arguments.end(), &argv[i+1], &argv[argc]);
|
|
|
+ arguments.insert(arguments.end(), &argv[i + 1], &argv[argc]);
|
|
|
break;
|
|
|
} else if (argv[i][0] == '-') {
|
|
|
// TODO: Arity
|
|
|
- options[option_name_mapping.at(argv[i])].emplace_back(argv[i+1]);
|
|
|
+ options[option_name_mapping.at(argv[i])].emplace_back(argv[i + 1]);
|
|
|
++i;
|
|
|
} else {
|
|
|
arguments.emplace_back(argv[i]);
|
|
|
@@ -116,40 +110,37 @@ Arguments<Impl>::Arguments(int argc, char const * const * const argv) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-template <typename Impl>
|
|
|
-void Arguments<Impl>::usage() const {
|
|
|
+template <typename Impl> void Arguments<Impl>::usage() const {
|
|
|
std::cout << program << " [options...]";
|
|
|
- for (auto &[index, name] : argument_names) {
|
|
|
- std::cout << " " << (index == optional_from ? "[" : "") << name;
|
|
|
- }
|
|
|
- if (optional_from != std::numeric_limits<size_t>::max()) {
|
|
|
- std::cout << "]";
|
|
|
+ for (auto & [index, name] : argument_names) {
|
|
|
+ std::cout << " " << (index == optional_from ? "[" : "") << name;
|
|
|
}
|
|
|
+ if (optional_from != std::numeric_limits<size_t>::max()) { std::cout << "]"; }
|
|
|
std::cout << "\nPositional Arguments:\n";
|
|
|
- for (auto &[name, desc] : positional_descriptions) {
|
|
|
+ for (auto & [name, desc] : positional_descriptions) {
|
|
|
std::cout << " " << name << ": " << desc << "\n";
|
|
|
}
|
|
|
std::cout << "Options:\n";
|
|
|
- for (auto &[opt, desc] : option_descriptions) {
|
|
|
+ for (auto & [opt, desc] : option_descriptions) {
|
|
|
std::cout << " " << opt << ": " << desc << "\n";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
template <typename Impl>
|
|
|
-auto Arguments<Impl>::option(std::string const &name,
|
|
|
- std::string const &description) {
|
|
|
+auto Arguments<Impl>::option(std::string const & name,
|
|
|
+ std::string const & description) {
|
|
|
return Option{this, name, 0, description};
|
|
|
}
|
|
|
|
|
|
template <typename Impl>
|
|
|
-auto Arguments<Impl>::option(std::string const &name, char abbrev,
|
|
|
- std::string const &description) {
|
|
|
+auto Arguments<Impl>::option(std::string const & name, char abbrev,
|
|
|
+ std::string const & description) {
|
|
|
return Option{this, name, abbrev, description};
|
|
|
}
|
|
|
|
|
|
template <typename Impl>
|
|
|
-auto Arguments<Impl>::argument(size_t index, std::string const &name,
|
|
|
- std::string const &description) {
|
|
|
+auto Arguments<Impl>::argument(size_t index, std::string const & name,
|
|
|
+ std::string const & description) {
|
|
|
return Positional{this, index, false, name, description};
|
|
|
}
|
|
|
|