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
3namespace yaze {
4namespace cli {
5namespace resources {
6
8 Descriptor descriptor;
9 descriptor.display_name = GetName(); // Use GetName() for display
10 descriptor.summary = "Command summary not provided.";
11 descriptor.todo_reference = "todo#unassigned";
12 return descriptor;
13}
14
15} // namespace resources
16} // namespace cli
17} // namespace yaze
18
19#include <iostream>
20
21#include "absl/strings/str_format.h"
23#include "util/macro.h"
24
25namespace yaze {
26namespace cli {
27namespace resources {
28
29absl::Status CommandHandler::Run(const std::vector<std::string>& args,
30 Rom* rom_context,
31 std::string* captured_output) {
32 // 1. Parse arguments
33 ArgumentParser parser(args);
34
35 // 2. Validate arguments
36 auto validation_status = ValidateArgs(parser);
37 if (!validation_status.ok()) {
38 std::cerr << "Error: " << validation_status.message() << "\n\n";
39 std::cerr << "Usage: " << GetUsage() << "\n";
40 return validation_status;
41 }
42
43 // 3. Get format string
44 std::string format_str =
45 parser.GetString("format").value_or(GetDefaultFormat());
46
47 // 4. Create output formatter
48 auto formatter_or = OutputFormatter::FromString(format_str);
49 if (!formatter_or.ok()) {
50 return formatter_or.status();
51 }
52 OutputFormatter formatter = std::move(formatter_or.value());
53
54 // 5. Setup command context
56 config.external_rom_context = rom_context;
57 config.format = format_str;
58
59 // Check for --rom override
60 if (auto rom_path = parser.GetString("rom"); rom_path.has_value()) {
61 config.rom_path = *rom_path;
62 }
63
64 // Check for --mock-rom flag
65 config.use_mock_rom = parser.HasFlag("mock-rom");
66
67 CommandContext context(config);
68
69 // 6. Get ROM (loads if needed) - only if command requires it
70 Rom* rom = nullptr;
71 if (RequiresRom()) {
72 ASSIGN_OR_RETURN(rom, context.GetRom());
73
74 // 7. Ensure labels are loaded if required
75 if (RequiresLabels()) {
77 }
78 }
79
80 // 8. Begin output formatting
81 formatter.BeginObject(GetOutputTitle());
82
83 // 9. Execute command business logic
84 auto execute_status = Execute(rom, parser, formatter);
85 if (!execute_status.ok()) {
86 return execute_status;
87 }
88
89 // 10. Finalize and print output
90 formatter.EndObject();
91
92 if (captured_output) {
93 *captured_output = formatter.GetOutput();
94 } else {
95 formatter.Print();
96 }
97
98 return absl::OkStatus();
99}
100
101} // namespace resources
102} // namespace cli
103} // 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 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.