| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- #pragma once
- #include "options.hpp"
- #include <utility>
- namespace program {
- template <typename Impl>
- template <typename T>
- Arguments<Impl>::Option::operator T() const {
- return (*this) ? self->options.at(name) : T();
- }
- 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) {
- return WithDefault<Option, T>{*this, std::forward<T>(value)};
- }
- inline std::string join(std::string const &tok,
- std::vector<std::string> const &data) {
- std::string accum = data.empty() ? "" : data.front();
- for (size_t i = 1; i < data.size(); ++i) {
- accum += tok;
- accum += data[i];
- }
- return accum;
- }
- 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) {
- self->option_name_mapping.emplace(alias, name);
- }
- self->option_descriptions.emplace(join("/", aliases), description);
- return false;
- }
- }
- namespace program {
- template <typename Impl>
- template <typename T>
- Arguments<Impl>::Positional::operator T() const {
- return (*this) ? self->arguments.at(index) : T();
- }
- template <typename Impl>
- Arguments<Impl>::Positional::operator bool() const {
- return primed(); // TODO(samjaffe): Existance check?
- }
- template <typename Impl>
- template <typename T>
- 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 {
- if (self->primed_) { return true; }
- if (is_optional) {
- self->optional_from = std::min(self->optional_from, index);
- } else if (self->optional_from < index) {
- throw std::logic_error{
- "Cannot have required positional named '" + name +
- "' after optional positionals"};
- }
- self->argument_names.emplace(index, name);
- self->positional_descriptions.emplace(name, description);
- return false;
- }
- }
- #define arg_equals(str) !strncmp(argv[i], str, sizeof(str))
- namespace program {
- template <typename Impl>
- Arguments<Impl>::Arguments(int argc, char const * const * const argv) {
- Impl generator;
- *this = static_cast<Arguments const&>(generator);
- if (argument_names.rbegin()->first != argument_names.size() - 1) {
- throw std::logic_error{"Jagged arguments are not supported"};
- }
- primed_ = true;
- program = argv[0];
- for (size_t i = 1; i < argc; ++i) {
- if (arg_equals("--help")) {
- usage();
- std::exit(0);
- } else if (arg_equals("--")) {
- arguments.insert(arguments.end(), &argv[i+1], &argv[argc]);
- i = argc;
- } else if (argv[i][0] == '-') {
- // TODO(samjaffe): Arity
- options.emplace(option_name_mapping.at(argv[i]), argv[i+1]);
- ++i;
- } else {
- arguments.emplace_back(argv[i]);
- }
- }
- }
- 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 << "]";
- }
- std::cout << "\nPositional Arguments:\n";
- for (auto &[name, desc] : positional_descriptions) {
- std::cout << " " << name << ": " << desc << "\n";
- }
- std::cout << "Options:\n";
- 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) {
- return Option{this, name, 0, description};
- }
- template <typename Impl>
- auto Arguments<Impl>::argument(size_t index, std::string const &name,
- std::string const &description) {
- return Positional{this, index, false, name, description};
- }
- }
|