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 = GetUsage();
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
20
21#include <iostream>
22
23#include "absl/strings/str_format.h"
24#include "util/macro.h"
25
26namespace yaze {
27namespace cli {
28namespace resources {
29
30absl::Status CommandHandler::Run(const std::vector<std::string>& args,
31 Rom* rom_context) {
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 = parser.GetString("format").value_or(GetDefaultFormat());
45
46 // 4. Create output formatter
47 auto formatter_or = OutputFormatter::FromString(format_str);
48 if (!formatter_or.ok()) {
49 return formatter_or.status();
50 }
51 OutputFormatter formatter = std::move(formatter_or.value());
52
53 // 5. Setup command context
55 config.external_rom_context = rom_context;
56 config.format = format_str;
57
58 // Check for --rom override
59 if (auto rom_path = parser.GetString("rom"); rom_path.has_value()) {
60 config.rom_path = *rom_path;
61 }
62
63 // Check for --mock-rom flag
64 config.use_mock_rom = parser.HasFlag("mock-rom");
65
66 CommandContext context(config);
67
68 // 6. Get ROM (loads if needed)
69 ASSIGN_OR_RETURN(Rom* rom, context.GetRom());
70
71 // 7. Ensure labels are loaded if required
72 if (RequiresLabels()) {
74 }
75
76 // 8. Begin output formatting
77 formatter.BeginObject(GetOutputTitle());
78
79 // 9. Execute command business logic
80 auto execute_status = Execute(rom, parser, formatter);
81 if (!execute_status.ok()) {
82 return execute_status;
83 }
84
85 // 10. Finalize and print output
86 formatter.EndObject();
87 formatter.Print();
88
89 return absl::OkStatus();
90}
91
92} // namespace resources
93} // namespace cli
94} // namespace yaze
95
The Rom class is used to load, save, and modify Rom data.
Definition rom.h:71
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.
absl::Status Run(const std::vector< std::string > &args, Rom *rom_context)
Execute the command.
virtual std::string GetUsage() const =0
Get the command usage string.
virtual absl::Status Execute(Rom *rom, const ArgumentParser &parser, OutputFormatter &formatter)=0
Execute the command business logic.
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 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.
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 RETURN_IF_ERROR(expression)
Definition macro.h:53
#define ASSIGN_OR_RETURN(type_variable_name, expression)
Definition macro.h:61
Main namespace for the application.
Configuration for command context.