yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
command_registry.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <iostream>
5#include <sstream>
6
7#include "absl/strings/str_cat.h"
8#include "absl/strings/str_format.h"
9#include "absl/strings/str_join.h"
11
12namespace yaze {
13namespace cli {
14
15namespace {
16std::string EscapeJson(const std::string& input) {
17 std::string escaped;
18 escaped.reserve(input.size());
19 for (char c : input) {
20 switch (c) {
21 case '\\\\':
22 escaped.append("\\\\");
23 break;
24 case '\"':
25 escaped.append("\\\"");
26 break;
27 case '\n':
28 escaped.append("\\n");
29 break;
30 case '\r':
31 escaped.append("\\r");
32 break;
33 case '\t':
34 escaped.append("\\t");
35 break;
36 default:
37 escaped.push_back(c);
38 }
39 }
40 return escaped;
41}
42
43void AppendStringArray(std::ostringstream& out,
44 const std::vector<std::string>& values) {
45 out << "[";
46 for (size_t i = 0; i < values.size(); ++i) {
47 if (i > 0)
48 out << ", ";
49 out << "\"" << EscapeJson(values[i]) << "\"";
50 }
51 out << "]";
52}
53} // namespace
54
56 static CommandRegistry instance;
57 static bool initialized = false;
58 if (!initialized) {
59 instance.RegisterCliCommands();
60 initialized = true;
61 }
62 return instance;
63}
64
66 std::unique_ptr<resources::CommandHandler> handler,
67 const CommandMetadata& metadata) {
68 std::string name = handler->GetName();
69
70 // Store metadata
71 metadata_[name] = metadata;
72
73 // Register aliases
74 for (const auto& alias : metadata.aliases) {
75 aliases_[alias] = name;
76 }
77
78 // Store handler
79 handlers_[name] = std::move(handler);
80}
81
83 std::vector<std::unique_ptr<resources::CommandHandler>> handlers) {
84 for (auto& handler : handlers) {
85 std::string name = handler->GetName();
86
87 if (handlers_.find(name) != handlers_.end()) {
88 continue;
89 }
90
91 // Build metadata from handler
92 auto descriptor = handler->Describe();
93 CommandMetadata metadata;
94 metadata.name = name;
95 metadata.usage = handler->GetUsage();
96 metadata.available_to_agent = true; // Most commands available to agent
97 metadata.requires_rom = handler->RequiresRom();
98 metadata.requires_grpc = false;
99
100 // Categorize and enhance metadata based on command type
101 if (name.find("resource-") == 0) {
102 metadata.category = "resource";
103 metadata.description = "Resource inspection and search";
104 if (name == "resource-list") {
105 metadata.examples = {"z3ed resource-list --type=dungeon --format=json",
106 "z3ed resource-list --type=sprite --format=table"};
107 }
108 } else if (name.find("dungeon-") == 0) {
109 metadata.category = "dungeon";
110 metadata.description = "Dungeon inspection and editing";
111 if (name == "dungeon-describe-room") {
112 metadata.examples = {
113 "z3ed dungeon-describe-room --room=5 --format=json"};
114 } else if (name == "dungeon-stream-plan") {
115 metadata.description =
116 "Read-only stream inventory, alias/overlap, and free-space "
117 "diagnostics; replacement-aware immutable move output is pending";
118 metadata.examples = {
119 "z3ed dungeon-stream-plan --kind=objects "
120 "--manifest=hack_manifest.json --rom=game.sfc --format=json",
121 "z3ed dungeon-stream-plan --kind=pot_items "
122 "--manifest=hack_manifest.json --rom=game.sfc --format=text"};
123 } else if (name == "dungeon-place-sprite") {
124 metadata.description =
125 "Place a dungeon sprite (dry-run by default, --write to apply)";
126 metadata.examples = {
127 "z3ed dungeon-place-sprite --room=0x77 --id=0xA3 --x=16 --y=21 "
128 "--subtype=4 --rom=Roms/oos168.sfc --format=json",
129 "z3ed dungeon-place-sprite --room=0x77 --id=0xA3 --x=16 --y=21 "
130 "--subtype=4 --write --rom=/tmp/oos-work.sfc --format=json"};
131 } else if (name == "dungeon-remove-sprite") {
132 metadata.description =
133 "Remove a dungeon sprite by index or exact coordinates";
134 metadata.examples = {
135 "z3ed dungeon-remove-sprite --room=0x77 --index=2 "
136 "--rom=/tmp/oos-work.sfc --format=json",
137 "z3ed dungeon-remove-sprite --room=0x77 --x=16 --y=21 --write "
138 "--rom=/tmp/oos-work.sfc --format=json"};
139 } else if (name == "dungeon-place-object") {
140 metadata.description =
141 "Place a dungeon object with immutable capacity/COW preflight "
142 "(dry-run by default)";
143 metadata.examples = {
144 "z3ed dungeon-place-object --room=0x98 --id=0x0031 --x=20 --y=20 "
145 "--size=4 --rom=/tmp/oos-work.sfc --format=json",
146 "z3ed dungeon-place-object --room=0x98 --id=0x0031 --x=20 --y=20 "
147 "--size=4 --manifest=hack_manifest.json --write "
148 "--rom=/tmp/oos-work.sfc --format=json"};
149 } else if (name == "dungeon-remove-object") {
150 metadata.description =
151 "Remove one ordinary dungeon object with exact identity guards "
152 "(dry-run by default)";
153 metadata.examples = {
154 "z3ed dungeon-remove-object --room=0x88 --index=117 "
155 "--expect-id=0x0DD --expect-x=30 --expect-y=12 "
156 "--expect-size=0 --expect-layer=0 "
157 "--rom=/tmp/oos-work.sfc --format=json",
158 "z3ed dungeon-remove-object --room=0x88 --index=117 "
159 "--expect-id=0x0DD --expect-x=30 --expect-y=12 "
160 "--expect-size=0 --expect-layer=0 "
161 "--manifest=hack_manifest.json --write "
162 "--rom=/tmp/oos-work.sfc --format=json"};
163 } else if (name == "dungeon-get-palette") {
164 metadata.description =
165 "Resolve a US/OOS room's full 8-bit palette-set ID to its shared "
166 "global dungeon palette and report every affected room; other "
167 "regional layouts fail closed";
168 metadata.examples = {
169 "z3ed dungeon-get-palette --room=0xA8 "
170 "--rom=Roms/oos168.sfc --format=json",
171 "z3ed dungeon-get-palette --room=0xA8 --index=0x07 "
172 "--rom=Roms/oos168.sfc --format=json"};
173 } else if (name == "dungeon-set-palette-color") {
174 metadata.description =
175 "Dry-run or persist one US/OOS shared dungeon palette color with "
176 "palette-set/index/color CAS, manifest ownership, exact fence, "
177 "clean-file baseline, required backup, atomic save, and external "
178 "reopen guards";
179 metadata.examples = {
180 "z3ed dungeon-set-palette-color --room=0xA8 --index=7 "
181 "--expect-palette-set=0x07 --expect-palette-index=7 "
182 "--expect-color=0x7FFF --color=0x7FFE "
183 "--manifest=Roms/hack_manifest.json "
184 "--rom=Roms/oos168.sfc --format=json",
185 "z3ed dungeon-set-palette-color --room=0xA8 --index=7 "
186 "--expect-palette-set=0x07 --expect-palette-index=7 "
187 "--expect-color=0x7FFF --color=0x7FFE "
188 "--manifest=Roms/hack_manifest.json --write "
189 "--rom=/tmp/oos-work.sfc --format=json"};
190 } else if (name == "dungeon-set-door-type") {
191 metadata.description =
192 "Change one existing door type with coordinate/CAS and manifest "
193 "guards (dry-run by default)";
194 metadata.examples = {
195 "z3ed dungeon-set-door-type --room=0xA8 --x=46 --y=58 "
196 "--type=0x00 --expect-type=0x18 "
197 "--manifest=hack_manifest.json --rom=Roms/oos168.sfc "
198 "--format=json",
199 "z3ed dungeon-set-door-type --room=0xA8 --x=46 --y=58 "
200 "--type=0x00 --expect-type=0x18 "
201 "--manifest=hack_manifest.json --write "
202 "--rom=/tmp/oos-work.sfc --format=json"};
203 } else if (name == "dungeon-list-pot-items") {
204 metadata.description =
205 "List existing pot items with raw positions and ROM addresses";
206 metadata.examples = {
207 "z3ed dungeon-list-pot-items --room=0xA8 "
208 "--rom=Roms/oos168.sfc --format=json"};
209 } else if (name == "dungeon-set-pot-item") {
210 metadata.description =
211 "Change one existing pot item with index/position/item CAS and "
212 "manifest guards (dry-run by default)";
213 metadata.examples = {
214 "z3ed dungeon-set-pot-item --room=0xA8 --index=0 "
215 "--expect-position=0x198C --expect-item=0x05 --item=0x06 "
216 "--manifest=Roms/hack_manifest.json --rom=Roms/oos168.sfc "
217 "--format=json",
218 "z3ed dungeon-set-pot-item --room=0xA8 --index=0 "
219 "--expect-position=0x198C --expect-item=0x05 --item=0x06 "
220 "--manifest=Roms/hack_manifest.json --write "
221 "--rom=/tmp/oos-work.sfc --format=json"};
222 } else if (name == "dungeon-oracle-preflight") {
223 metadata.description =
224 "Oracle ROM safety preflight: water-fill region/table, collision "
225 "maps, and optional required-room checks";
226 metadata.examples = {
227 "z3ed dungeon-oracle-preflight --rom oos168x.sfc --format=json",
228 "z3ed dungeon-oracle-preflight --rom oos168x.sfc "
229 "--required-water-fill-rooms=0x25,0x27 --format=json",
230 "z3ed dungeon-oracle-preflight --rom oos168x.sfc "
231 "--report=/tmp/preflight.json"};
232 } else if (name == "dungeon-set-collision-tile") {
233 metadata.description =
234 "Set one or more custom collision tiles in a dungeon room";
235 metadata.examples = {
236 "z3ed dungeon-set-collision-tile --room=0xB8 "
237 "--tiles=\"10,5,0xB7;50,45,0xBA\" --rom=/tmp/oos-work.sfc "
238 "--format=json",
239 "z3ed dungeon-set-collision-tile --room=0xB8 "
240 "--tiles=\"10,5,0xB7;50,45,0xBA\" --write "
241 "--rom=/tmp/oos-work.sfc --format=json"};
242 }
243 } else if (name.find("overworld-") == 0) {
244 metadata.category = "overworld";
245 metadata.description = "Overworld inspection and editing";
246 if (name == "overworld-find-tile") {
247 metadata.examples = {
248 "z3ed overworld-find-tile --tile=0x42 --format=json"};
249 }
250 } else if (name.find("project-bundle-") == 0) {
251 metadata.category = "project";
252 metadata.requires_rom = false;
253 if (name == "project-bundle-verify") {
254 metadata.description = "Project bundle verification";
255 metadata.examples = {
256 "z3ed project-bundle-verify --project MyProject.yazeproj "
257 "--format=json",
258 "z3ed project-bundle-verify --project project.yaze "
259 "--report report.json",
260 "z3ed project-bundle-verify --project MyProject.yazeproj "
261 "--check-rom-hash --format=json"};
262 } else if (name == "project-bundle-pack") {
263 metadata.description = "Pack bundle into zip archive";
264 metadata.examples = {
265 "z3ed project-bundle-pack --project MyProject.yazeproj "
266 "--out MyProject.zip --format=json",
267 "z3ed project-bundle-pack --project MyProject.yazeproj "
268 "--out MyProject.zip --overwrite"};
269 } else if (name == "project-bundle-unpack") {
270 metadata.description = "Unpack zip archive into bundle";
271 metadata.examples = {
272 "z3ed project-bundle-unpack --archive MyProject.zip "
273 "--out ./projects --format=json",
274 "z3ed project-bundle-unpack --archive MyProject.zip "
275 "--out ./projects --overwrite",
276 "z3ed project-bundle-unpack --archive MyProject.zip "
277 "--out ./projects --keep-partial-output",
278 "z3ed project-bundle-unpack --archive MyProject.zip "
279 "--out ./projects --dry-run"};
280 }
281 } else if (name.find("rom-") == 0) {
282 metadata.category = "rom";
283 metadata.description = "ROM inspection and validation";
284 if (name == "rom-info") {
285 metadata.examples = {"z3ed rom-info --rom=zelda3.sfc"};
286 } else if (name == "rom-validate") {
287 metadata.examples = {"z3ed rom-validate --rom=zelda3.sfc"};
288 } else if (name == "rom-doctor") {
289 metadata.examples = {"z3ed rom-doctor --rom=zelda3.sfc --format=json"};
290 } else if (name == "rom-diff") {
291 metadata.examples = {
292 "z3ed rom-diff --rom_a=base.sfc --rom_b=target.sfc"};
293 } else if (name == "rom-compare") {
294 metadata.examples = {
295 "z3ed rom-compare --rom=target.sfc --baseline=vanilla.sfc"};
296 }
297 } else if (name.find("emulator-") == 0) {
298 metadata.category = "emulator";
299 metadata.description = "Emulator control and debugging";
300 metadata.requires_grpc = true;
301 if (name == "emulator-set-breakpoint") {
302 metadata.examples = {
303 "z3ed emulator-set-breakpoint --address=0x83D7 --description='NMI "
304 "handler'"};
305 }
306 } else if (name.find("mesen-") == 0) {
307 metadata.category = "mesen2";
308 metadata.description = "Mesen2 socket automation and introspection";
309 metadata.requires_rom = false;
310 if (name == "mesen-state-capture") {
311 metadata.examples = {
312 "z3ed mesen-state-capture --state=foo.state "
313 "--rom-file=Roms/oos168x.sfc --scenario=d6_room_88",
314 "z3ed mesen-state-capture --state=Roms/SaveStates/d6_room_88.state "
315 "--rom-file=Roms/oos168x.sfc --slot=1 "
316 "--states-dir=~/Library/Application\\ Support/Mesen2/SaveStates"};
317 } else if (name == "mesen-state-hook") {
318 metadata.examples = {
319 "z3ed mesen-state-hook --state=foo.state "
320 "--rom-file=Roms/oos168x.sfc --scenario=d6_room_88",
321 "z3ed mesen-state-hook --state=foo.state "
322 "--rom-file=Roms/oos168x.sfc --format=json"};
323 }
324 } else if (name.find("gui-") == 0) {
325 metadata.category = "gui";
326 metadata.description = "GUI automation";
327 metadata.requires_grpc = true;
328 } else if (name.find("hex-") == 0) {
329 metadata.category = "graphics";
330 metadata.description = "Hex data manipulation";
331 } else if (name.find("palette-") == 0) {
332 metadata.category = "graphics";
333 metadata.description = "Palette operations";
334 } else if (name.find("sprite-") == 0) {
335 metadata.category = "graphics";
336 metadata.description = "Sprite operations";
337 } else if (name.find("message-") == 0 || name.find("dialogue-") == 0) {
338 metadata.category = "game";
339 metadata.description = name.find("message-") == 0 ? "Message inspection"
340 : "Dialogue inspection";
341 if (name == "message-source-sync") {
342 metadata.description =
343 "Dry-run or CAS-publish a complete canonical expanded-message "
344 "bundle and deterministic no-org Asar include";
345 metadata.examples = {
346 "z3ed message-source-sync --project=Oracle-of-Secrets.yaze "
347 "--file=/tmp/message-edits.json --format=json",
348 "z3ed message-source-sync --project=Oracle-of-Secrets.yaze "
349 "--file=/tmp/message-edits.json "
350 "--expected-source-sha256=<sha256> --write --format=json"};
351 }
352 } else if (name.find("music-") == 0) {
353 metadata.category = "game";
354 metadata.description = "Music/audio inspection";
355 } else if (name.find("oracle-") == 0) {
356 metadata.category = "oracle";
357 metadata.description = "Oracle-of-Secrets project tooling";
358 if (name == "oracle-menu-index") {
359 metadata.examples = {
360 "z3ed oracle-menu-index --project=/path/to/oracle-of-secrets "
361 "--format=json",
362 "z3ed oracle-menu-index --table=Menu_ItemCursorPositions "
363 "--missing-bins --format=json"};
364 } else if (name == "oracle-menu-set-offset") {
365 metadata.examples = {
366 "z3ed oracle-menu-set-offset "
367 "--asm=Menu/menu_select_item.asm "
368 "--table=Menu_ItemCursorPositions --index=0 --row=7 --col=2",
369 "z3ed oracle-menu-set-offset "
370 "--asm=Menu/menu_select_item.asm "
371 "--table=Menu_ItemCursorPositions --index=0 --row=7 --col=2 "
372 "--write"};
373 } else if (name == "oracle-menu-validate") {
374 metadata.examples = {
375 "z3ed oracle-menu-validate --project=/path/to/oracle-of-secrets",
376 "z3ed oracle-menu-validate --project=/path/to/oracle-of-secrets "
377 "--strict --max-row=31 --max-col=31"};
378 } else if (name == "oracle-smoke-check") {
379 metadata.examples = {
380 "z3ed oracle-smoke-check --rom oos168.sfc --format=json",
381 "z3ed oracle-smoke-check --rom oos168.sfc --strict-readiness "
382 "--format=json",
383 "z3ed oracle-smoke-check --rom oos168.sfc "
384 "--min-d6-track-rooms=4 --format=json",
385 "z3ed oracle-smoke-check --rom oos168.sfc "
386 "--report=/tmp/smoke.json"};
387 }
388 } else if (name == "simple-chat" || name == "chat") {
389 metadata.category = "agent";
390 metadata.description = "AI conversational agent";
391 metadata.available_to_agent = false; // Meta-command
392 metadata.requires_rom = false;
393 metadata.examples = {
394 "z3ed simple-chat --rom=zelda3.sfc",
395 "z3ed simple-chat \"What dungeons exist?\" --rom=zelda3.sfc"};
396 } else if (name.find("tools-") == 0) {
397 metadata.category = "tools";
398 if (name == "tools-list") {
399 metadata.description = "List available test helper tools";
400 metadata.requires_rom = false;
401 metadata.available_to_agent = true;
402 } else if (name == "tools-harness-state") {
403 metadata.description = "Generate WRAM state for test harnesses";
404 metadata.examples = {
405 "z3ed tools-harness-state --rom=zelda3.sfc --output=state.h"};
406 } else if (name == "tools-extract-values") {
407 metadata.description = "Extract vanilla ROM values";
408 metadata.examples = {"z3ed tools-extract-values --rom=zelda3.sfc"};
409 } else if (name == "tools-extract-golden") {
410 metadata.description = "Extract comprehensive golden data for testing";
411 metadata.examples = {
412 "z3ed tools-extract-golden --rom=zelda3.sfc --output=golden.h"};
413 } else if (name == "tools-patch-v3") {
414 metadata.description = "Create v3 ZSCustomOverworld patched ROM";
415 metadata.examples = {
416 "z3ed tools-patch-v3 --rom=zelda3.sfc --output=patched.sfc"};
417 } else {
418 metadata.description = "Test helper tool";
419 }
420 } else if (name.find("test-") == 0) {
421 metadata.category = "test";
422 metadata.description = "Test discovery and execution";
423 if (name == "test-list") {
424 metadata.requires_rom = false;
425 metadata.examples = {"z3ed test-list", "z3ed test-list --format json"};
426 } else if (name == "test-run") {
427 metadata.examples = {"z3ed test-run --label stable",
428 "z3ed test-run --label gui"};
429 } else if (name == "test-status") {
430 metadata.requires_rom = false;
431 metadata.examples = {"z3ed test-status --format json"};
432 }
433 } else {
434 metadata.category = "misc";
435 metadata.description = "Miscellaneous command";
436 }
437
438 // Prefer handler-provided summary if present
439 if (!descriptor.summary.empty() &&
440 descriptor.summary != "Command summary not provided.") {
441 metadata.description = descriptor.summary;
442 }
443
444 // Keep TODO reference if supplied by handler
445 if (!descriptor.todo_reference.empty() &&
446 descriptor.todo_reference != "todo#unassigned") {
447 metadata.todo_reference = descriptor.todo_reference;
448 }
449
450 Register(std::move(handler), metadata);
451 }
452}
453
454resources::CommandHandler* CommandRegistry::Get(const std::string& name) const {
455 // Check direct name
456 auto it = handlers_.find(name);
457 if (it != handlers_.end()) {
458 return it->second.get();
459 }
460
461 // Check aliases
462 auto alias_it = aliases_.find(name);
463 if (alias_it != aliases_.end()) {
464 auto handler_it = handlers_.find(alias_it->second);
465 if (handler_it != handlers_.end()) {
466 return handler_it->second.get();
467 }
468 }
469
470 return nullptr;
471}
472
474 const std::string& name) const {
475 // Resolve alias first
476 std::string canonical_name = name;
477 auto alias_it = aliases_.find(name);
478 if (alias_it != aliases_.end()) {
479 canonical_name = alias_it->second;
480 }
481
482 auto it = metadata_.find(canonical_name);
483 return (it != metadata_.end()) ? &it->second : nullptr;
484}
485
487 const std::string& category) const {
488 std::vector<std::string> result;
489 for (const auto& [name, metadata] : metadata_) {
490 if (metadata.category == category) {
491 result.push_back(name);
492 }
493 }
494 return result;
495}
496
497std::vector<std::string> CommandRegistry::GetCategories() const {
498 std::vector<std::string> categories;
499 for (const auto& [_, metadata] : metadata_) {
500 if (std::find(categories.begin(), categories.end(), metadata.category) ==
501 categories.end()) {
502 categories.push_back(metadata.category);
503 }
504 }
505 return categories;
506}
507
508std::vector<std::string> CommandRegistry::GetAgentCommands() const {
509 std::vector<std::string> result;
510 for (const auto& [name, metadata] : metadata_) {
511 if (metadata.available_to_agent) {
512 result.push_back(name);
513 }
514 }
515 return result;
516}
517
519 std::ostringstream out;
520 out << "{\n \"commands\": [\n";
521
522 bool first = true;
523 for (const auto& [_, metadata] : metadata_) {
524 if (!first)
525 out << ",\n";
526 first = false;
527
528 out << " {\n";
529 out << " \"name\": \"" << EscapeJson(metadata.name) << "\",\n";
530 out << " \"category\": \"" << EscapeJson(metadata.category) << "\",\n";
531 out << " \"description\": \"" << EscapeJson(metadata.description)
532 << "\",\n";
533 out << " \"usage\": \"" << EscapeJson(metadata.usage) << "\",\n";
534 out << " \"available_to_agent\": "
535 << (metadata.available_to_agent ? "true" : "false") << ",\n";
536 out << " \"requires_rom\": "
537 << (metadata.requires_rom ? "true" : "false") << ",\n";
538 out << " \"requires_grpc\": "
539 << (metadata.requires_grpc ? "true" : "false") << ",\n";
540 if (!metadata.todo_reference.empty()) {
541 out << " \"todo_reference\": \""
542 << EscapeJson(metadata.todo_reference) << "\",\n";
543 } else {
544 out << " \"todo_reference\": \"\",\n";
545 }
546 out << " \"aliases\": ";
547 AppendStringArray(out, metadata.aliases);
548 out << ",\n";
549 out << " \"examples\": ";
550 AppendStringArray(out, metadata.examples);
551 out << "\n";
552 out << " }";
553 }
554
555 out << "\n ]\n}";
556 return out.str();
557}
558
559std::string CommandRegistry::GenerateHelp(const std::string& name) const {
560 auto* metadata = GetMetadata(name);
561 if (!metadata) {
562 return absl::StrFormat("Command '%s' not found", name);
563 }
564
565 std::ostringstream help;
566 help << "\n\033[1;36m" << metadata->name << "\033[0m - "
567 << metadata->description << "\n\n";
568 help << "\033[1;33mUsage:\033[0m\n";
569 help << " " << metadata->usage << "\n\n";
570
571 if (!metadata->examples.empty()) {
572 help << "\033[1;33mExamples:\033[0m\n";
573 for (const auto& example : metadata->examples) {
574 help << " " << example << "\n";
575 }
576 help << "\n";
577 }
578
579 if (metadata->requires_rom) {
580 help << "\033[1;33mRequires:\033[0m ROM file (--rom=<path>)\n";
581 }
582 if (metadata->requires_grpc) {
583 help << "\033[1;33mRequires:\033[0m YAZE running with gRPC enabled\n";
584 }
585
586 if (!metadata->aliases.empty()) {
587 help << "\n\033[1;33mAliases:\033[0m "
588 << absl::StrJoin(metadata->aliases, ", ") << "\n";
589 }
590
591 return help.str();
592}
593
595 const std::string& category) const {
596 auto commands = GetCommandsInCategory(category);
597 if (commands.empty()) {
598 return absl::StrFormat("No commands in category '%s'", category);
599 }
600
601 std::ostringstream help;
602 help << "\n\033[1;36m" << category << " commands:\033[0m\n\n";
603
604 for (const auto& cmd : commands) {
605 auto* metadata = GetMetadata(cmd);
606 if (metadata) {
607 help << " \033[1;33m" << cmd << "\033[0m\n";
608 help << " " << metadata->description << "\n";
609 if (!metadata->usage.empty()) {
610 help << " Usage: " << metadata->usage << "\n";
611 }
612 help << "\n";
613 }
614 }
615
616 return help.str();
617}
618
620 std::ostringstream help;
621 help << "\n\033[1;36mAll z3ed Commands:\033[0m\n\n";
622
623 auto categories = GetCategories();
624 for (const auto& category : categories) {
625 help << GenerateCategoryHelp(category);
626 }
627
628 return help.str();
629}
630
631absl::Status CommandRegistry::Execute(const std::string& name,
632 const std::vector<std::string>& args,
633 Rom* rom_context,
634 std::string* captured_output) {
635 auto* handler = Get(name);
636 if (!handler) {
637 return absl::NotFoundError(absl::StrFormat("Command '%s' not found", name));
638 }
639
640 const bool command_help_requested =
641 std::find(args.begin(), args.end(), "--help") != args.end() ||
642 std::find(args.begin(), args.end(), "-h") != args.end();
643 if (command_help_requested) {
644 const std::string help = GenerateHelp(name);
645 if (captured_output != nullptr) {
646 *captured_output = help;
647 } else {
648 std::cout << help << "\n";
649 }
650 return absl::OkStatus();
651 }
652
653 absl::Status status = handler->Run(args, rom_context, captured_output);
654
655 // If a command was invoked without its required arguments, surface full
656 // command help in addition to the normal parser error/usage line.
657 if (!status.ok() && status.code() == absl::StatusCode::kInvalidArgument &&
658 args.empty()) {
659 const std::string help = GenerateHelp(name);
660 if (captured_output != nullptr) {
661 if (!captured_output->empty()) {
662 captured_output->append("\n\n");
663 }
664 captured_output->append(help);
665 } else {
666 std::cout << help << "\n";
667 }
668 }
669
670 return status;
671}
672
673bool CommandRegistry::HasCommand(const std::string& name) const {
674 return Get(name) != nullptr;
675}
676
680
681} // namespace cli
682} // namespace yaze
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
Definition rom.h:28
Single source of truth for all z3ed commands.
std::map< std::string, std::string > aliases_
std::vector< std::string > GetCommandsInCategory(const std::string &category) const
Get all commands in a category.
static CommandRegistry & Instance()
std::string GenerateCategoryHelp(const std::string &category) const
Generate category help text.
std::string ExportFunctionSchemas() const
Export function schemas for AI tool calling (JSON)
const CommandMetadata * GetMetadata(const std::string &name) const
Get command metadata.
std::string GenerateCompleteHelp() const
Generate complete help text (all commands)
std::vector< std::string > GetAgentCommands() const
Get all commands available to AI agents.
std::map< std::string, CommandMetadata > metadata_
absl::Status Execute(const std::string &name, const std::vector< std::string > &args, Rom *rom_context=nullptr, std::string *captured_output=nullptr)
Execute a command by name.
std::map< std::string, std::unique_ptr< resources::CommandHandler > > handlers_
resources::CommandHandler * Get(const std::string &name) const
Get a command handler by name or alias.
bool HasCommand(const std::string &name) const
Check if command exists.
std::vector< std::string > GetCategories() const
Get all categories.
void Register(std::unique_ptr< resources::CommandHandler > handler, const CommandMetadata &metadata)
Register a command handler.
std::string GenerateHelp(const std::string &name) const
Generate help text for a command.
void RegisterHandlers(std::vector< std::unique_ptr< resources::CommandHandler > > handlers)
Register a set of command handlers (idempotent)
Base class for CLI command handlers.
std::string EscapeJson(const std::string &input)
void AppendStringArray(std::ostringstream &out, const std::vector< std::string > &values)
std::vector< std::unique_ptr< resources::CommandHandler > > CreateCliCommandHandlers()
Factory function to create all CLI-level command handlers.