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-get-palette") {
150 metadata.description =
151 "Resolve a US/OOS room's full 8-bit palette-set ID to its shared "
152 "global dungeon palette and report every affected room; other "
153 "regional layouts fail closed";
154 metadata.examples = {
155 "z3ed dungeon-get-palette --room=0xA8 "
156 "--rom=Roms/oos168.sfc --format=json",
157 "z3ed dungeon-get-palette --room=0xA8 --index=0x07 "
158 "--rom=Roms/oos168.sfc --format=json"};
159 } else if (name == "dungeon-set-palette-color") {
160 metadata.description =
161 "Dry-run or persist one US/OOS shared dungeon palette color with "
162 "palette-set/index/color CAS, manifest ownership, exact fence, "
163 "clean-file baseline, required backup, atomic save, and external "
164 "reopen guards";
165 metadata.examples = {
166 "z3ed dungeon-set-palette-color --room=0xA8 --index=7 "
167 "--expect-palette-set=0x07 --expect-palette-index=7 "
168 "--expect-color=0x7FFF --color=0x7FFE "
169 "--manifest=Roms/hack_manifest.json "
170 "--rom=Roms/oos168.sfc --format=json",
171 "z3ed dungeon-set-palette-color --room=0xA8 --index=7 "
172 "--expect-palette-set=0x07 --expect-palette-index=7 "
173 "--expect-color=0x7FFF --color=0x7FFE "
174 "--manifest=Roms/hack_manifest.json --write "
175 "--rom=/tmp/oos-work.sfc --format=json"};
176 } else if (name == "dungeon-set-door-type") {
177 metadata.description =
178 "Change one existing door type with coordinate/CAS and manifest "
179 "guards (dry-run by default)";
180 metadata.examples = {
181 "z3ed dungeon-set-door-type --room=0xA8 --x=46 --y=58 "
182 "--type=0x00 --expect-type=0x18 "
183 "--manifest=hack_manifest.json --rom=Roms/oos168.sfc "
184 "--format=json",
185 "z3ed dungeon-set-door-type --room=0xA8 --x=46 --y=58 "
186 "--type=0x00 --expect-type=0x18 "
187 "--manifest=hack_manifest.json --write "
188 "--rom=/tmp/oos-work.sfc --format=json"};
189 } else if (name == "dungeon-list-pot-items") {
190 metadata.description =
191 "List existing pot items with raw positions and ROM addresses";
192 metadata.examples = {
193 "z3ed dungeon-list-pot-items --room=0xA8 "
194 "--rom=Roms/oos168.sfc --format=json"};
195 } else if (name == "dungeon-set-pot-item") {
196 metadata.description =
197 "Change one existing pot item with index/position/item CAS and "
198 "manifest guards (dry-run by default)";
199 metadata.examples = {
200 "z3ed dungeon-set-pot-item --room=0xA8 --index=0 "
201 "--expect-position=0x198C --expect-item=0x05 --item=0x06 "
202 "--manifest=Roms/hack_manifest.json --rom=Roms/oos168.sfc "
203 "--format=json",
204 "z3ed dungeon-set-pot-item --room=0xA8 --index=0 "
205 "--expect-position=0x198C --expect-item=0x05 --item=0x06 "
206 "--manifest=Roms/hack_manifest.json --write "
207 "--rom=/tmp/oos-work.sfc --format=json"};
208 } else if (name == "dungeon-oracle-preflight") {
209 metadata.description =
210 "Oracle ROM safety preflight: water-fill region/table, collision "
211 "maps, and optional required-room checks";
212 metadata.examples = {
213 "z3ed dungeon-oracle-preflight --rom oos168x.sfc --format=json",
214 "z3ed dungeon-oracle-preflight --rom oos168x.sfc "
215 "--required-water-fill-rooms=0x25,0x27 --format=json",
216 "z3ed dungeon-oracle-preflight --rom oos168x.sfc "
217 "--report=/tmp/preflight.json"};
218 } else if (name == "dungeon-set-collision-tile") {
219 metadata.description =
220 "Set one or more custom collision tiles in a dungeon room";
221 metadata.examples = {
222 "z3ed dungeon-set-collision-tile --room=0xB8 "
223 "--tiles=\"10,5,0xB7;50,45,0xBA\" --rom=/tmp/oos-work.sfc "
224 "--format=json",
225 "z3ed dungeon-set-collision-tile --room=0xB8 "
226 "--tiles=\"10,5,0xB7;50,45,0xBA\" --write "
227 "--rom=/tmp/oos-work.sfc --format=json"};
228 }
229 } else if (name.find("overworld-") == 0) {
230 metadata.category = "overworld";
231 metadata.description = "Overworld inspection and editing";
232 if (name == "overworld-find-tile") {
233 metadata.examples = {
234 "z3ed overworld-find-tile --tile=0x42 --format=json"};
235 }
236 } else if (name.find("project-bundle-") == 0) {
237 metadata.category = "project";
238 metadata.requires_rom = false;
239 if (name == "project-bundle-verify") {
240 metadata.description = "Project bundle verification";
241 metadata.examples = {
242 "z3ed project-bundle-verify --project MyProject.yazeproj "
243 "--format=json",
244 "z3ed project-bundle-verify --project project.yaze "
245 "--report report.json",
246 "z3ed project-bundle-verify --project MyProject.yazeproj "
247 "--check-rom-hash --format=json"};
248 } else if (name == "project-bundle-pack") {
249 metadata.description = "Pack bundle into zip archive";
250 metadata.examples = {
251 "z3ed project-bundle-pack --project MyProject.yazeproj "
252 "--out MyProject.zip --format=json",
253 "z3ed project-bundle-pack --project MyProject.yazeproj "
254 "--out MyProject.zip --overwrite"};
255 } else if (name == "project-bundle-unpack") {
256 metadata.description = "Unpack zip archive into bundle";
257 metadata.examples = {
258 "z3ed project-bundle-unpack --archive MyProject.zip "
259 "--out ./projects --format=json",
260 "z3ed project-bundle-unpack --archive MyProject.zip "
261 "--out ./projects --overwrite",
262 "z3ed project-bundle-unpack --archive MyProject.zip "
263 "--out ./projects --keep-partial-output",
264 "z3ed project-bundle-unpack --archive MyProject.zip "
265 "--out ./projects --dry-run"};
266 }
267 } else if (name.find("rom-") == 0) {
268 metadata.category = "rom";
269 metadata.description = "ROM inspection and validation";
270 if (name == "rom-info") {
271 metadata.examples = {"z3ed rom-info --rom=zelda3.sfc"};
272 } else if (name == "rom-validate") {
273 metadata.examples = {"z3ed rom-validate --rom=zelda3.sfc"};
274 } else if (name == "rom-doctor") {
275 metadata.examples = {"z3ed rom-doctor --rom=zelda3.sfc --format=json"};
276 } else if (name == "rom-diff") {
277 metadata.examples = {
278 "z3ed rom-diff --rom_a=base.sfc --rom_b=target.sfc"};
279 } else if (name == "rom-compare") {
280 metadata.examples = {
281 "z3ed rom-compare --rom=target.sfc --baseline=vanilla.sfc"};
282 }
283 } else if (name.find("emulator-") == 0) {
284 metadata.category = "emulator";
285 metadata.description = "Emulator control and debugging";
286 metadata.requires_grpc = true;
287 if (name == "emulator-set-breakpoint") {
288 metadata.examples = {
289 "z3ed emulator-set-breakpoint --address=0x83D7 --description='NMI "
290 "handler'"};
291 }
292 } else if (name.find("mesen-") == 0) {
293 metadata.category = "mesen2";
294 metadata.description = "Mesen2 socket automation and introspection";
295 metadata.requires_rom = false;
296 if (name == "mesen-state-capture") {
297 metadata.examples = {
298 "z3ed mesen-state-capture --state=foo.state "
299 "--rom-file=Roms/oos168x.sfc --scenario=d6_room_88",
300 "z3ed mesen-state-capture --state=Roms/SaveStates/d6_room_88.state "
301 "--rom-file=Roms/oos168x.sfc --slot=1 "
302 "--states-dir=~/Library/Application\\ Support/Mesen2/SaveStates"};
303 } else if (name == "mesen-state-hook") {
304 metadata.examples = {
305 "z3ed mesen-state-hook --state=foo.state "
306 "--rom-file=Roms/oos168x.sfc --scenario=d6_room_88",
307 "z3ed mesen-state-hook --state=foo.state "
308 "--rom-file=Roms/oos168x.sfc --format=json"};
309 }
310 } else if (name.find("gui-") == 0) {
311 metadata.category = "gui";
312 metadata.description = "GUI automation";
313 metadata.requires_grpc = true;
314 } else if (name.find("hex-") == 0) {
315 metadata.category = "graphics";
316 metadata.description = "Hex data manipulation";
317 } else if (name.find("palette-") == 0) {
318 metadata.category = "graphics";
319 metadata.description = "Palette operations";
320 } else if (name.find("sprite-") == 0) {
321 metadata.category = "graphics";
322 metadata.description = "Sprite operations";
323 } else if (name.find("message-") == 0 || name.find("dialogue-") == 0) {
324 metadata.category = "game";
325 metadata.description = name.find("message-") == 0 ? "Message inspection"
326 : "Dialogue inspection";
327 if (name == "message-source-sync") {
328 metadata.description =
329 "Dry-run or CAS-publish a complete canonical expanded-message "
330 "bundle and deterministic no-org Asar include";
331 metadata.examples = {
332 "z3ed message-source-sync --project=Oracle-of-Secrets.yaze "
333 "--file=/tmp/message-edits.json --format=json",
334 "z3ed message-source-sync --project=Oracle-of-Secrets.yaze "
335 "--file=/tmp/message-edits.json "
336 "--expected-source-sha256=<sha256> --write --format=json"};
337 }
338 } else if (name.find("music-") == 0) {
339 metadata.category = "game";
340 metadata.description = "Music/audio inspection";
341 } else if (name.find("oracle-") == 0) {
342 metadata.category = "oracle";
343 metadata.description = "Oracle-of-Secrets project tooling";
344 if (name == "oracle-menu-index") {
345 metadata.examples = {
346 "z3ed oracle-menu-index --project=/path/to/oracle-of-secrets "
347 "--format=json",
348 "z3ed oracle-menu-index --table=Menu_ItemCursorPositions "
349 "--missing-bins --format=json"};
350 } else if (name == "oracle-menu-set-offset") {
351 metadata.examples = {
352 "z3ed oracle-menu-set-offset "
353 "--asm=Menu/menu_select_item.asm "
354 "--table=Menu_ItemCursorPositions --index=0 --row=7 --col=2",
355 "z3ed oracle-menu-set-offset "
356 "--asm=Menu/menu_select_item.asm "
357 "--table=Menu_ItemCursorPositions --index=0 --row=7 --col=2 "
358 "--write"};
359 } else if (name == "oracle-menu-validate") {
360 metadata.examples = {
361 "z3ed oracle-menu-validate --project=/path/to/oracle-of-secrets",
362 "z3ed oracle-menu-validate --project=/path/to/oracle-of-secrets "
363 "--strict --max-row=31 --max-col=31"};
364 } else if (name == "oracle-smoke-check") {
365 metadata.examples = {
366 "z3ed oracle-smoke-check --rom oos168.sfc --format=json",
367 "z3ed oracle-smoke-check --rom oos168.sfc --strict-readiness "
368 "--format=json",
369 "z3ed oracle-smoke-check --rom oos168.sfc "
370 "--min-d6-track-rooms=4 --format=json",
371 "z3ed oracle-smoke-check --rom oos168.sfc "
372 "--report=/tmp/smoke.json"};
373 }
374 } else if (name == "simple-chat" || name == "chat") {
375 metadata.category = "agent";
376 metadata.description = "AI conversational agent";
377 metadata.available_to_agent = false; // Meta-command
378 metadata.requires_rom = false;
379 metadata.examples = {
380 "z3ed simple-chat --rom=zelda3.sfc",
381 "z3ed simple-chat \"What dungeons exist?\" --rom=zelda3.sfc"};
382 } else if (name.find("tools-") == 0) {
383 metadata.category = "tools";
384 if (name == "tools-list") {
385 metadata.description = "List available test helper tools";
386 metadata.requires_rom = false;
387 metadata.available_to_agent = true;
388 } else if (name == "tools-harness-state") {
389 metadata.description = "Generate WRAM state for test harnesses";
390 metadata.examples = {
391 "z3ed tools-harness-state --rom=zelda3.sfc --output=state.h"};
392 } else if (name == "tools-extract-values") {
393 metadata.description = "Extract vanilla ROM values";
394 metadata.examples = {"z3ed tools-extract-values --rom=zelda3.sfc"};
395 } else if (name == "tools-extract-golden") {
396 metadata.description = "Extract comprehensive golden data for testing";
397 metadata.examples = {
398 "z3ed tools-extract-golden --rom=zelda3.sfc --output=golden.h"};
399 } else if (name == "tools-patch-v3") {
400 metadata.description = "Create v3 ZSCustomOverworld patched ROM";
401 metadata.examples = {
402 "z3ed tools-patch-v3 --rom=zelda3.sfc --output=patched.sfc"};
403 } else {
404 metadata.description = "Test helper tool";
405 }
406 } else if (name.find("test-") == 0) {
407 metadata.category = "test";
408 metadata.description = "Test discovery and execution";
409 if (name == "test-list") {
410 metadata.requires_rom = false;
411 metadata.examples = {"z3ed test-list", "z3ed test-list --format json"};
412 } else if (name == "test-run") {
413 metadata.examples = {"z3ed test-run --label stable",
414 "z3ed test-run --label gui"};
415 } else if (name == "test-status") {
416 metadata.requires_rom = false;
417 metadata.examples = {"z3ed test-status --format json"};
418 }
419 } else {
420 metadata.category = "misc";
421 metadata.description = "Miscellaneous command";
422 }
423
424 // Prefer handler-provided summary if present
425 if (!descriptor.summary.empty() &&
426 descriptor.summary != "Command summary not provided.") {
427 metadata.description = descriptor.summary;
428 }
429
430 // Keep TODO reference if supplied by handler
431 if (!descriptor.todo_reference.empty() &&
432 descriptor.todo_reference != "todo#unassigned") {
433 metadata.todo_reference = descriptor.todo_reference;
434 }
435
436 Register(std::move(handler), metadata);
437 }
438}
439
440resources::CommandHandler* CommandRegistry::Get(const std::string& name) const {
441 // Check direct name
442 auto it = handlers_.find(name);
443 if (it != handlers_.end()) {
444 return it->second.get();
445 }
446
447 // Check aliases
448 auto alias_it = aliases_.find(name);
449 if (alias_it != aliases_.end()) {
450 auto handler_it = handlers_.find(alias_it->second);
451 if (handler_it != handlers_.end()) {
452 return handler_it->second.get();
453 }
454 }
455
456 return nullptr;
457}
458
460 const std::string& name) const {
461 // Resolve alias first
462 std::string canonical_name = name;
463 auto alias_it = aliases_.find(name);
464 if (alias_it != aliases_.end()) {
465 canonical_name = alias_it->second;
466 }
467
468 auto it = metadata_.find(canonical_name);
469 return (it != metadata_.end()) ? &it->second : nullptr;
470}
471
473 const std::string& category) const {
474 std::vector<std::string> result;
475 for (const auto& [name, metadata] : metadata_) {
476 if (metadata.category == category) {
477 result.push_back(name);
478 }
479 }
480 return result;
481}
482
483std::vector<std::string> CommandRegistry::GetCategories() const {
484 std::vector<std::string> categories;
485 for (const auto& [_, metadata] : metadata_) {
486 if (std::find(categories.begin(), categories.end(), metadata.category) ==
487 categories.end()) {
488 categories.push_back(metadata.category);
489 }
490 }
491 return categories;
492}
493
494std::vector<std::string> CommandRegistry::GetAgentCommands() const {
495 std::vector<std::string> result;
496 for (const auto& [name, metadata] : metadata_) {
497 if (metadata.available_to_agent) {
498 result.push_back(name);
499 }
500 }
501 return result;
502}
503
505 std::ostringstream out;
506 out << "{\n \"commands\": [\n";
507
508 bool first = true;
509 for (const auto& [_, metadata] : metadata_) {
510 if (!first)
511 out << ",\n";
512 first = false;
513
514 out << " {\n";
515 out << " \"name\": \"" << EscapeJson(metadata.name) << "\",\n";
516 out << " \"category\": \"" << EscapeJson(metadata.category) << "\",\n";
517 out << " \"description\": \"" << EscapeJson(metadata.description)
518 << "\",\n";
519 out << " \"usage\": \"" << EscapeJson(metadata.usage) << "\",\n";
520 out << " \"available_to_agent\": "
521 << (metadata.available_to_agent ? "true" : "false") << ",\n";
522 out << " \"requires_rom\": "
523 << (metadata.requires_rom ? "true" : "false") << ",\n";
524 out << " \"requires_grpc\": "
525 << (metadata.requires_grpc ? "true" : "false") << ",\n";
526 if (!metadata.todo_reference.empty()) {
527 out << " \"todo_reference\": \""
528 << EscapeJson(metadata.todo_reference) << "\",\n";
529 } else {
530 out << " \"todo_reference\": \"\",\n";
531 }
532 out << " \"aliases\": ";
533 AppendStringArray(out, metadata.aliases);
534 out << ",\n";
535 out << " \"examples\": ";
536 AppendStringArray(out, metadata.examples);
537 out << "\n";
538 out << " }";
539 }
540
541 out << "\n ]\n}";
542 return out.str();
543}
544
545std::string CommandRegistry::GenerateHelp(const std::string& name) const {
546 auto* metadata = GetMetadata(name);
547 if (!metadata) {
548 return absl::StrFormat("Command '%s' not found", name);
549 }
550
551 std::ostringstream help;
552 help << "\n\033[1;36m" << metadata->name << "\033[0m - "
553 << metadata->description << "\n\n";
554 help << "\033[1;33mUsage:\033[0m\n";
555 help << " " << metadata->usage << "\n\n";
556
557 if (!metadata->examples.empty()) {
558 help << "\033[1;33mExamples:\033[0m\n";
559 for (const auto& example : metadata->examples) {
560 help << " " << example << "\n";
561 }
562 help << "\n";
563 }
564
565 if (metadata->requires_rom) {
566 help << "\033[1;33mRequires:\033[0m ROM file (--rom=<path>)\n";
567 }
568 if (metadata->requires_grpc) {
569 help << "\033[1;33mRequires:\033[0m YAZE running with gRPC enabled\n";
570 }
571
572 if (!metadata->aliases.empty()) {
573 help << "\n\033[1;33mAliases:\033[0m "
574 << absl::StrJoin(metadata->aliases, ", ") << "\n";
575 }
576
577 return help.str();
578}
579
581 const std::string& category) const {
582 auto commands = GetCommandsInCategory(category);
583 if (commands.empty()) {
584 return absl::StrFormat("No commands in category '%s'", category);
585 }
586
587 std::ostringstream help;
588 help << "\n\033[1;36m" << category << " commands:\033[0m\n\n";
589
590 for (const auto& cmd : commands) {
591 auto* metadata = GetMetadata(cmd);
592 if (metadata) {
593 help << " \033[1;33m" << cmd << "\033[0m\n";
594 help << " " << metadata->description << "\n";
595 if (!metadata->usage.empty()) {
596 help << " Usage: " << metadata->usage << "\n";
597 }
598 help << "\n";
599 }
600 }
601
602 return help.str();
603}
604
606 std::ostringstream help;
607 help << "\n\033[1;36mAll z3ed Commands:\033[0m\n\n";
608
609 auto categories = GetCategories();
610 for (const auto& category : categories) {
611 help << GenerateCategoryHelp(category);
612 }
613
614 return help.str();
615}
616
617absl::Status CommandRegistry::Execute(const std::string& name,
618 const std::vector<std::string>& args,
619 Rom* rom_context,
620 std::string* captured_output) {
621 auto* handler = Get(name);
622 if (!handler) {
623 return absl::NotFoundError(absl::StrFormat("Command '%s' not found", name));
624 }
625
626 const bool command_help_requested =
627 std::find(args.begin(), args.end(), "--help") != args.end() ||
628 std::find(args.begin(), args.end(), "-h") != args.end();
629 if (command_help_requested) {
630 const std::string help = GenerateHelp(name);
631 if (captured_output != nullptr) {
632 *captured_output = help;
633 } else {
634 std::cout << help << "\n";
635 }
636 return absl::OkStatus();
637 }
638
639 absl::Status status = handler->Run(args, rom_context, captured_output);
640
641 // If a command was invoked without its required arguments, surface full
642 // command help in addition to the normal parser error/usage line.
643 if (!status.ok() && status.code() == absl::StatusCode::kInvalidArgument &&
644 args.empty()) {
645 const std::string help = GenerateHelp(name);
646 if (captured_output != nullptr) {
647 if (!captured_output->empty()) {
648 captured_output->append("\n\n");
649 }
650 captured_output->append(help);
651 } else {
652 std::cout << help << "\n";
653 }
654 }
655
656 return status;
657}
658
659bool CommandRegistry::HasCommand(const std::string& name) const {
660 return Get(name) != nullptr;
661}
662
666
667} // namespace cli
668} // 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.