|
|
@@ -7,13 +7,12 @@
|
|
|
//
|
|
|
|
|
|
#include "cli/cli.h"
|
|
|
-
|
|
|
-#include <string_utils/tokenizer.h>
|
|
|
+#include "cli/tape.h"
|
|
|
|
|
|
namespace cli {
|
|
|
|
|
|
cli::cli(std::string const & prompt, std::istream &in)
|
|
|
- : in_(in), prompt_(prompt), eof_(false) {
|
|
|
+ : in_(&in), prompt_(prompt), eof_(false) {
|
|
|
register_callback("quit", [this] { eof_ = true; });
|
|
|
register_callback("help", [this] {
|
|
|
for (auto & [k, v] : arguments_) {
|
|
|
@@ -32,18 +31,28 @@ bool cli::active() const {
|
|
|
return !eof_ && (std::cout << prompt_).good();
|
|
|
}
|
|
|
|
|
|
-void cli::run() const {
|
|
|
+void cli::run(std::string &command) const {
|
|
|
+ auto views = tokenize_(command);
|
|
|
+ command = views.front();
|
|
|
+ views.erase(views.begin());
|
|
|
+ if (!callbacks_.count(command)) {
|
|
|
+ std::cerr << "Unknown command: " << command << "\n";
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ callbacks_.at(command)(views);
|
|
|
+}
|
|
|
+
|
|
|
+void cli::run_interactive() const {
|
|
|
+ Tape tape(128, [this](std::string line) { run(line); });
|
|
|
+ while (active()) {
|
|
|
+ tape.readch();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void cli::run_stream() 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);
|
|
|
+ while (active() && std::getline(*in_, command)) {
|
|
|
+ run(command);
|
|
|
}
|
|
|
}
|
|
|
|