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"
15
16namespace yaze {
17namespace cli {
18namespace resources {
19
28 public:
32 struct Config {
33 std::optional<std::string> rom_path;
34 std::optional<std::string> symbols_path;
35 std::optional<std::string> project_context_path;
36 bool use_mock_rom = false;
37 std::string format = "json"; // "json" or "text"
38 bool verbose = false;
39
40 // ROM context can be provided externally (e.g., from Agent class)
42 };
43
44 explicit CommandContext(const Config& config);
46
50 absl::Status Initialize();
51
55 absl::StatusOr<Rom*> GetRom();
56
61
65 const std::string& GetFormat() const { return config_.format; }
66
70 bool IsVerbose() const { return config_.verbose; }
71
75 absl::Status EnsureLabelsLoaded(Rom* rom);
76
81 return loaded_project_.has_value() ? &loaded_project_.value() : nullptr;
82 }
83
84 private:
85 absl::Status ApplyProjectRuntimeContext();
87
89 Rom rom_storage_; // Owned ROM if loaded from file
91 nullptr; // Points to either rom_storage_ or external_rom_context
93 std::optional<project::YazeProject> loaded_project_;
94 std::optional<core::FeatureFlags::Flags> previous_feature_flags_;
95 std::optional<zelda3::CustomObjectManager::State>
98 bool initialized_ = false;
99};
100
106 public:
107 explicit ArgumentParser(const std::vector<std::string>& args);
108
112 std::optional<std::string> GetString(const std::string& name) const;
113
117 absl::StatusOr<int> GetInt(const std::string& name) const;
118
122 absl::StatusOr<int> GetHex(const std::string& name) const;
123
127 bool HasFlag(const std::string& name) const;
128
132 std::vector<std::string> GetPositional() const;
133
137 absl::Status RequireArgs(const std::vector<std::string>& required) const;
138
139 private:
140 std::vector<std::string> args_;
141
142 std::optional<std::string> FindArgValue(const std::string& name) const;
143};
144
150 public:
151 enum class Format { kJson, kText };
152
153 explicit OutputFormatter(Format format) : format_(format) {}
154
158 static absl::StatusOr<OutputFormatter> FromString(const std::string& format);
159
163 void BeginObject(const std::string& title = "");
164
168 void EndObject();
169
173 void AddField(const std::string& key, const std::string& value);
174 void AddField(const std::string& key, const char* value);
175 void AddField(const std::string& key, int value);
176 void AddField(const std::string& key, uint64_t value);
177 void AddField(const std::string& key, bool value);
178
182 void AddHexField(const std::string& key, uint64_t value, int width = 2);
183
187 void BeginArray(const std::string& key);
188
192 void EndArray();
193
197 void AddArrayItem(const std::string& item);
198
202 std::string GetOutput() const;
203
207 void Print() const;
208
212 bool IsJson() const { return format_ == Format::kJson; }
213
217 bool IsText() const { return format_ == Format::kText; }
218
219 private:
221 std::string buffer_;
223 bool first_field_ = true;
224 bool in_array_ = false;
226
227 void AddIndent();
228 std::string EscapeJson(const std::string& str) const;
229};
230
231} // namespace resources
232} // namespace cli
233} // namespace yaze
234
235#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:28
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.
Utility for consistent output formatting across commands.
void BeginArray(const std::string &key)
Begin an array.
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.
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:120