Setup routing.

This commit is contained in:
2026-08-07 14:21:44 +01:00
parent fdb67b449d
commit 717c048d6f
7 changed files with 74 additions and 22 deletions
+4 -15
View File
@@ -2,18 +2,6 @@
#include "commands/ed/ed.h"
namespace crib::cli {
struct Command {
void (*run)(int, char **);
std::string (*summary)();
void (*help)();
};
struct cli_error : std::runtime_error {
int exit_code;
cli_error(std::string msg, int code)
: std::runtime_error(std::move(msg)), exit_code(code) {}
};
static const std::unordered_map<std::string, Command> commands = {
{"ed",
{crib::commands::ed::run,
@@ -45,9 +33,10 @@ void help() {
std::cout << " " << name << " - " << cmd.summary() << "\n";
}
int execute(const std::string &name, const Command &cmd, int argc, char **argv) {
int execute(const std::string &name, const Command &cmd, int argc, char *argv[]) {
try {
cmd.run(argc - 1, argv + 1);
std::vector<std::string> args(argv, argv + argc);
cmd.run(args);
return 0;
} catch (const cli_error &e) {
std::cerr << "Error running " << name << ": " << e.what() << '\n';
@@ -69,7 +58,7 @@ int run(int argc, char *argv[]) {
if (argc > 1)
if (auto it = commands.find(argv[1]); it != commands.end())
return execute(it->first, it->second, argc, argv);
return execute(it->first, it->second, argc - 1, argv + 1);
if (argc > 1 && std::string(argv[1]) == "--help") {
help();
+1 -1
View File
@@ -5,7 +5,7 @@ std::string summary() {
return "A POSIX-compliant line editor.";
}
void help() {}
void run(int, char *[]) {
void run(std::vector<std::string> args) {
return;
}
} // namespace crib::commands::ed
+2 -2
View File
@@ -55,8 +55,8 @@ std::string Vase::to_string(Range range) {
return out;
}
LineIterator Vase::iterate(uint64_t line) {
return LineIterator(root, line, Direction::Forward);
Iterator Vase::iterate(uint64_t line) {
return Iterator(root, line);
}
bool Vase::undo() {