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