// // cli.cxx // tax-manager // // Created by Sam Jaffe on 10/8/20. // Copyright © 2020 Sam Jaffe. All rights reserved. // #include "cli/cli.h" #include namespace cli { cli::cli(std::string const & prompt, std::istream &in) : in_(in), prompt_(prompt), eof_(false) { register_callback("quit", [this] { eof_ = true; }); register_callback("help", [this] { for (auto & [k, v] : arguments_) { std::cout << k; for (size_t i = 0; i < v; ++i) { std::cout << " arg" << i; } std::cout << "\n"; } }); } cli::cli(std::istream &in) : cli("cli::cli :> ", in) {} bool cli::active() const { return !eof_ && (std::cout << prompt_).good(); } void cli::run() const { std::string command; while (active() && std::getline(in_, command)) { auto split = string_utils::EscapedTokenizer(" ", {'"', "\\\""}); auto views = split(command); command = views.front(); views.erase(views.begin()); if (!callbacks_.count(command)) { std::cerr << "Unknown command: " << command << "\n"; continue; } callbacks_.at(command)(views); } } }