yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
command_handler.cc
Go to the documentation of this file.
2
3#include <iostream>
4#include <utility>
5
6#include "util/macro.h"
7
8namespace yaze {
9namespace cli {
10namespace resources {
11
12absl::Status CommandHandler::Run(const std::vector<std::string>& args,
13 Rom* rom_context,
14 std::string* captured_output) {
15 // 1. Parse arguments
16 ArgumentParser parser(args);
17
18 // 2. Validate arguments
19 auto validation_status = ValidateArgs(parser);
20 if (!validation_status.ok()) {
21 std::cerr << "Error: " << validation_status.message() << "\n\n";
22 std::cerr << "Usage: " << GetUsage() << "\n";
23 return validation_status;
24 }
25
26 // 3. Get format string
27 std::string format_str =
28 parser.GetString("format").value_or(GetDefaultFormat());
29
30 // 4. Create output formatter
31 auto formatter_or = OutputFormatter::FromString(format_str);
32 if (!formatter_or.ok()) {
33 return formatter_or.status();
34 }
35 OutputFormatter formatter = std::move(formatter_or.value());
36
37 // 5. Setup command context
39 config.external_rom_context = rom_context;
40 config.format = format_str;
41 config.verbose = parser.HasFlag("verbose");
42
43 // Check for --rom override
44 if (auto rom_path = parser.GetString("rom"); rom_path.has_value()) {
45 config.rom_path = *rom_path;
46 }
47
48 // Check for --mock-rom flag
49 config.use_mock_rom = parser.HasFlag("mock-rom");
50
51 CommandContext context(config);
52
53 // 6. Get ROM (loads if needed) - only if command requires it
54 Rom* rom = nullptr;
55 if (RequiresRom()) {
56 ASSIGN_OR_RETURN(rom, context.GetRom());
57 SetRomContext(rom);
58
59 // 7. Ensure labels are loaded if required
60 if (RequiresLabels()) {
62 }
63 }
64
65 // 8. Begin output formatting
66 formatter.BeginObject(GetOutputTitle());
67
68 // 9. Execute command business logic
69 auto execute_status = Execute(rom, parser, formatter);
70 if (!execute_status.ok()) {
71 return execute_status;
72 }
73
74 // 10. Finalize and print output
75 formatter.EndObject();
76
77 if (captured_output) {
78 *captured_output = formatter.GetOutput();
79 } else {
80 formatter.Print();
81 }
82
83 return absl::OkStatus();
84}
85
87 Descriptor descriptor;
88 descriptor.display_name = GetName(); // Use GetName() for display.
89 descriptor.summary = "Command summary not provided.";
90 descriptor.todo_reference = "todo#unassigned";
91 return descriptor;
92}
93
94} // namespace resources
95} // namespace cli
96} // 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:24
Utility for parsing common CLI argument patterns.
std::optional< std::string > GetString(const std::string &name) const
Parse a named argument (e.g., –format=json or –format json)
bool HasFlag(const std::string &name) const
Check if a flag is present.
Encapsulates common context for CLI command execution.
absl::StatusOr< Rom * > GetRom()
Get the ROM instance (loads if not already loaded)
absl::Status EnsureLabelsLoaded(Rom *rom)
Ensure resource labels are loaded.
virtual bool RequiresLabels() const
Check if the command requires ROM labels.
virtual std::string GetUsage() const =0
Get the command usage string.
virtual std::string GetName() const =0
Get the command name.
virtual void SetRomContext(Rom *rom)
Set the ROM context for tools that need ROM access. Default implementation stores the ROM pointer for...
virtual absl::Status Execute(Rom *rom, const ArgumentParser &parser, OutputFormatter &formatter)=0
Execute the command business logic.
absl::Status Run(const std::vector< std::string > &args, Rom *rom_context, std::string *captured_output=nullptr)
Execute the command.
virtual std::string GetOutputTitle() const
Get the output title for formatting.
virtual std::string GetDefaultFormat() const
Get the default output format ("json" or "text")
virtual bool RequiresRom() const
Check if the command requires a loaded ROM.
virtual Descriptor Describe() const
Provide metadata for TUI/help summaries.
virtual absl::Status ValidateArgs(const ArgumentParser &parser)=0
Validate command arguments.
Utility for consistent output formatting across commands.
std::string GetOutput() const
Get the formatted output.
static absl::StatusOr< OutputFormatter > FromString(const std::string &format)
Create formatter from string ("json" or "text")
void BeginObject(const std::string &title="")
Start a JSON object or text section.
void EndObject()
End a JSON object or text section.
void Print() const
Print the formatted output to stdout.
#define ASSIGN_OR_RETURN(type_variable_name, expression)
Definition macro.h:62
#define RETURN_IF_ERROR(expr)
Definition snes.cc:22
Configuration for command context.