| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- //
- // cli.cxx
- // tax-manager
- //
- // Created by Sam Jaffe on 10/8/20.
- // Copyright © 2020 Sam Jaffe. All rights reserved.
- //
- #include "cli.h"
- #include <iostream>
- namespace {
- cli::args_t tokenize(std::string const & search, std::string const & token,
- bool ignore_empty = false) {
- cli::args_t rval;
- size_t i = 0;
- for (size_t n = search.find(token); n != std::string::npos;
- i = n + 1, n = search.find(token, i)) {
- if (i == n && ignore_empty) continue;
- rval.emplace_back(&search[i], n - i);
- }
- rval.emplace_back(&search[i], search.size() - i);
- return rval;
- }
- }
- namespace cli {
- cli::cli() : callbacks_(), eof_(false) {
- register_callback("quit", [this] { eof_ = true; });
- }
- void cli::run() const {
- std::string command;
- while (!eof_ && std::getline(std::cin, command).good()) {
- auto views = tokenize(command, " ");
- command = views.front();
- views.erase(views.begin());
- if (!callbacks_.count(command)) continue; // TODO: Error message
- callbacks_.at(command)(views);
- }
- }
- template <> std::string cli::from_string(std::string const & str) {
- return str;
- }
- template <> int cli::from_string(std::string const & str) {
- return std::stoi(str);
- }
- }
|