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"
10#include "app/rom.h"
11
12namespace yaze {
13namespace cli {
14namespace resources {
15
24 public:
28 struct Config {
29 std::optional<std::string> rom_path;
30 bool use_mock_rom = false;
31 std::string format = "json"; // "json" or "text"
32 bool verbose = false;
33
34 // ROM context can be provided externally (e.g., from Agent class)
36 };
37
38 explicit CommandContext(const Config& config);
39 ~CommandContext() = default;
40
44 absl::Status Initialize();
45
49 absl::StatusOr<Rom*> GetRom();
50
54 const std::string& GetFormat() const { return config_.format; }
55
59 bool IsVerbose() const { return config_.verbose; }
60
64 absl::Status EnsureLabelsLoaded(Rom* rom);
65
66 private:
68 Rom rom_storage_; // Owned ROM if loaded from file
69 Rom* active_rom_ = nullptr; // Points to either rom_storage_ or external_rom_context
70 bool initialized_ = false;
71};
72
78 public:
79 explicit ArgumentParser(const std::vector<std::string>& args);
80
84 std::optional<std::string> GetString(const std::string& name) const;
85
89 absl::StatusOr<int> GetInt(const std::string& name) const;
90
94 absl::StatusOr<int> GetHex(const std::string& name) const;
95
99 bool HasFlag(const std::string& name) const;
100
104 std::vector<std::string> GetPositional() const;
105
109 absl::Status RequireArgs(const std::vector<std::string>& required) const;
110
111 private:
112 std::vector<std::string> args_;
113
114 std::optional<std::string> FindArgValue(const std::string& name) const;
115};
116
122 public:
123 enum class Format {
124 kJson,
125 kText
126 };
127
128 explicit OutputFormatter(Format format) : format_(format) {}
129
133 static absl::StatusOr<OutputFormatter> FromString(const std::string& format);
134
138 void BeginObject(const std::string& title = "");
139
143 void EndObject();
144
148 void AddField(const std::string& key, const std::string& value);
149 void AddField(const std::string& key, int value);
150 void AddField(const std::string& key, uint64_t value);
151 void AddField(const std::string& key, bool value);
152
156 void AddHexField(const std::string& key, uint64_t value, int width = 2);
157
161 void BeginArray(const std::string& key);
162
166 void EndArray();
167
171 void AddArrayItem(const std::string& item);
172
176 std::string GetOutput() const;
177
181 void Print() const;
182
186 bool IsJson() const { return format_ == Format::kJson; }
187
191 bool IsText() const { return format_ == Format::kText; }
192
193 private:
195 std::string buffer_;
197 bool first_field_ = true;
198 bool in_array_ = false;
200
201 void AddIndent();
202 std::string EscapeJson(const std::string& str) const;
203};
204
205} // namespace resources
206} // namespace cli
207} // namespace yaze
208
209#endif // YAZE_SRC_CLI_SERVICE_RESOURCES_COMMAND_CONTEXT_H_
210
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 > FindArgValue(const std::string &name) 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.
absl::StatusOr< Rom * > GetRom()
Get the ROM instance (loads if not already loaded)
absl::Status EnsureLabelsLoaded(Rom *rom)
Ensure resource labels are loaded.
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.
Main namespace for the application.
Configuration for command context.