yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
command_context.h
Go to the documentation of this file.
1#ifndef YAZE_SRC_CLI_SERVICE_RESOURCES_COMMAND_CONTEXT_H_
2#define YAZE_SRC_CLI_SERVICE_RESOURCES_COMMAND_CONTEXT_H_
3
4#include <optional>
5#include <string>
6#include <vector>
7
8#include "absl/status/status.h"
9#include "absl/status/statusor.h"
11#include "core/features.h"
12#include "core/project.h"
13#include "rom/rom.h"
16
17namespace yaze {
18namespace cli {
19namespace resources {
20
29 public:
33 struct Config {
34 std::optional<std::string> rom_path;
35 std::optional<std::string> symbols_path;
36 std::optional<std::string> project_context_path;
37 bool use_mock_rom = false;
38 std::string format = "json"; // "json" or "text"
39 bool verbose = false;
40
41 // ROM context can be provided externally (e.g., from Agent class)
43 };
44
45 explicit CommandContext(const Config& config);
47
51 absl::Status Initialize();
52
56 absl::StatusOr<Rom*> GetRom();
57
62
66 const std::string& GetFormat() const { return config_.format; }
67
71 bool IsVerbose() const { return config_.verbose; }
72
76 absl::Status EnsureLabelsLoaded(Rom* rom);
77
82 return loaded_project_.has_value() ? &loaded_project_.value() : nullptr;
83 }
84
85 private:
86 absl::Status ApplyProjectRuntimeContext();
88
90 Rom rom_storage_; // Owned ROM if loaded from file
92 nullptr; // Points to either rom_storage_ or external_rom_context
94 std::optional<project::YazeProject> loaded_project_;
95 std::optional<core::FeatureFlags::Flags> previous_feature_flags_;
96 std::optional<zelda3::ResourceLabelProvider::State> previous_label_state_;
97 std::optional<zelda3::CustomObjectManager::State>
100 bool initialized_ = false;
101};
102
108 public:
109 explicit ArgumentParser(const std::vector<std::string>& args);
110
114 std::optional<std::string> GetString(const std::string& name) const;
115
119 absl::StatusOr<int> GetInt(const std::string& name) const;
120
124 absl::StatusOr<int> GetHex(const std::string& name) const;
125
129 bool HasFlag(const std::string& name) const;
130
134 std::vector<std::string> GetPositional() const;
135
139 absl::Status RequireArgs(const std::vector<std::string>& required) const;
140
141 private:
142 std::vector<std::string> args_;
143
144 std::optional<std::string> FindArgValue(const std::string& name) const;
145};
146
152 public:
153 enum class Format { kJson, kText };
154
155 explicit OutputFormatter(Format format) : format_(format) {}
156
160 static absl::StatusOr<OutputFormatter> FromString(const std::string& format);
161
165 void BeginObject(const std::string& title = "");
166
170 void EndObject();
171
175 void AddField(const std::string& key, const std::string& value);
176 void AddField(const std::string& key, const char* value);
177 void AddField(const std::string& key, int value);
178 void AddField(const std::string& key, uint64_t value);
179 void AddField(const std::string& key, bool value);
180
184 void AddHexField(const std::string& key, uint64_t value, int width = 2);
185
189 void BeginArray(const std::string& key);
190
194 void EndArray();
195
199 void AddArrayItem(const std::string& item);
200
205 void AddRawJsonField(const std::string& key, const std::string& json);
206
211 void AddRawText(const std::string& text);
212
216 std::string GetOutput() const;
217
221 void Print() const;
222
226 bool IsJson() const { return format_ == Format::kJson; }
227
231 bool IsText() const { return format_ == Format::kText; }
232
233 private:
234 struct JsonContext {
235 enum class Type { kObject, kArray };
237 bool first = true;
238 int item_count = 0;
239 };
240
242 std::string buffer_;
244 std::vector<JsonContext> json_stack_;
245
246 void AddIndent();
248 void AddJsonFieldPrefix(const std::string& key);
249 std::string EscapeJson(const std::string& str) const;
250};
251
252} // namespace resources
253} // namespace cli
254} // namespace yaze
255
256#endif // YAZE_SRC_CLI_SERVICE_RESOURCES_COMMAND_CONTEXT_H_
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
Utility for parsing common CLI argument patterns.
std::optional< std::string > FindArgValue(const std::string &name) const
std::vector< std::string > args_
ArgumentParser(const std::vector< std::string > &args)
std::vector< std::string > GetPositional() const
Get all remaining positional arguments.
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.
absl::Status RequireArgs(const std::vector< std::string > &required) const
Validate that required arguments are present.
absl::StatusOr< int > GetHex(const std::string &name) const
Parse a hex integer argument.
absl::StatusOr< int > GetInt(const std::string &name) const
Parse an integer argument (supports hex with 0x prefix)
Encapsulates common context for CLI command execution.
const std::string & GetFormat() const
Get the output format ("json" or "text")
bool IsVerbose() const
Check if verbose mode is enabled.
std::optional< project::YazeProject > loaded_project_
absl::StatusOr< Rom * > GetRom()
Get the ROM instance (loads if not already loaded)
absl::Status EnsureLabelsLoaded(Rom *rom)
Ensure resource labels are loaded.
project::YazeProject * GetProjectContext()
Returns loaded project context when –project-context was used.
emu::debug::SymbolProvider * GetSymbolProvider()
Get the SymbolProvider instance.
std::optional< core::FeatureFlags::Flags > previous_feature_flags_
std::optional< zelda3::CustomObjectManager::State > previous_custom_object_state_
emu::debug::SymbolProvider symbol_provider_
absl::Status Initialize()
Initialize the context and load ROM if needed.
std::optional< zelda3::ResourceLabelProvider::State > previous_label_state_
Utility for consistent output formatting across commands.
void AddJsonFieldPrefix(const std::string &key)
void BeginArray(const std::string &key)
Begin an array.
void AddRawText(const std::string &text)
Append preformatted text (for example an aligned table). Ignored in JSON mode.
void AddRawJsonField(const std::string &key, const std::string &json)
Add a field whose value is already-serialized JSON (numbers, nested arrays, null)....
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 AddArrayItem(const std::string &item)
Add an item to current array.
std::vector< JsonContext > json_stack_
void BeginObject(const std::string &title="")
Start a JSON object or text section.
void EndObject()
End a JSON object or text section.
std::string EscapeJson(const std::string &str) const
void AddField(const std::string &key, const std::string &value)
Add a key-value pair.
bool IsJson() const
Check if using JSON format.
bool IsText() const
Check if using text format.
void AddHexField(const std::string &key, uint64_t value, int width=2)
Add a hex-formatted field.
void Print() const
Print the formatted output to stdout.
Provider for symbol (label) resolution in disassembly.
Configuration for command context.
std::optional< std::string > project_context_path
std::optional< std::string > symbols_path
Modern project structure with comprehensive settings consolidation.
Definition project.h:290