deargui-vpl/examples/blueprints-example/blueprints_cli.cpp

318 lines
9.3 KiB
C++

#include "blueprints_cli.h"
#include <cstdio>
#include <cstdlib>
// ============================================================================
// Helper Functions (Global)
// ============================================================================
std::string GetStringArg(const ArgsMap& args, const std::string& key, const std::string& defaultValue)
{
auto it = args.find(key);
if (it != args.end() && it->second.Type == ArgValue::Type::String)
return it->second.String;
return defaultValue;
}
bool GetBoolArg(const ArgsMap& args, const std::string& key, bool defaultValue)
{
auto it = args.find(key);
if (it != args.end() && it->second.Type == ArgValue::Type::Bool)
return it->second.Bool;
return defaultValue;
}
int GetIntArg(const ArgsMap& args, const std::string& key, int defaultValue)
{
auto it = args.find(key);
if (it != args.end() && it->second.Type == ArgValue::Type::Int)
return static_cast<int>(it->second.Int);
return defaultValue;
}
double GetDoubleArg(const ArgsMap& args, const std::string& key, double defaultValue)
{
auto it = args.find(key);
if (it != args.end() && it->second.Type == ArgValue::Type::Double)
return it->second.Double;
return defaultValue;
}
// ============================================================================
// BlueprintsCLI Implementation
// ============================================================================
BlueprintsCLI::BlueprintsCLI(const ArgsMap& args)
: m_Args(args)
{
// Set graph filename if provided
std::string filename = GetStringArg("file", "");
if (!filename.empty()) {
SetGraphFilename(filename);
}
}
int BlueprintsCLI::Execute()
{
std::string command = GetStringArg("command", "help");
if (command == "validate") {
return CommandValidate();
}
else if (command == "export") {
return CommandExport();
}
else if (command == "execute") {
return CommandExecute();
}
else if (command == "info") {
return CommandInfo();
}
else if (command == "help") {
return CommandHelp();
}
else {
PrintError("Unknown command: " + command);
CommandHelp();
return 1;
}
}
// ============================================================================
// Command Implementations
// ============================================================================
int BlueprintsCLI::CommandValidate()
{
std::string filename = GetGraphFilename();
if (filename.empty()) {
PrintError("No graph file specified. Use --file <path>");
return 1;
}
PrintInfo("Validating: " + filename);
// Create root container
auto* container = AddRootContainer(filename);
if (!container) {
PrintError("Failed to create container");
return 1;
}
// Load graph
if (!LoadGraph(filename)) {
PrintError("Failed to load graph: " + GetLastError());
return 1;
}
auto info = GetGraphInfo();
PrintSuccess("Graph loaded successfully");
printf(" Nodes: %s\n", info["nodes"].c_str());
printf(" Links: %s\n", info["links"].c_str());
// Validate
if (!ValidateGraph()) {
PrintError("Validation failed: " + GetLastError());
return 1;
}
PrintSuccess("Validation passed");
return 0;
}
int BlueprintsCLI::CommandExport()
{
std::string filename = GetGraphFilename();
std::string output = GetStringArg("output", "");
std::string format = GetStringArg("format", "json");
if (filename.empty()) {
PrintError("No graph file specified. Use --file <path>");
return 1;
}
if (output.empty()) {
PrintError("No output file specified. Use --output <path>");
return 1;
}
PrintInfo("Exporting: " + filename + " -> " + output + " (" + format + ")");
// Create root container and load
auto* container = AddRootContainer(filename);
if (!container) {
PrintError("Failed to create container");
return 1;
}
if (!LoadGraph(filename)) {
PrintError("Failed to load graph: " + GetLastError());
return 1;
}
// Export based on format
if (format == "json") {
// For JSON, just save to output file
if (SaveGraph(output)) {
PrintSuccess("Exported to JSON: " + output);
return 0;
} else {
PrintError("Failed to export: " + GetLastError());
return 1;
}
}
else {
PrintError("Format '" + format + "' not yet implemented");
printf(" Supported formats: json\n");
printf(" Planned: xml, yaml, dot\n");
return 1;
}
}
int BlueprintsCLI::CommandExecute()
{
std::string filename = GetGraphFilename();
if (filename.empty()) {
PrintError("No graph file specified. Use --file <path>");
return 1;
}
PrintInfo("Executing: " + filename);
// Create root container and load
auto* container = AddRootContainer(filename);
if (!container) {
PrintError("Failed to create container");
return 1;
}
if (!LoadGraph(filename)) {
PrintError("Failed to load graph: " + GetLastError());
return 1;
}
// Execute
if (!ExecuteGraph()) {
PrintError("Execution failed: " + GetLastError());
return 1;
}
PrintSuccess("Execution completed");
return 0;
}
int BlueprintsCLI::CommandInfo()
{
std::string filename = GetGraphFilename();
if (filename.empty()) {
PrintError("No graph file specified. Use --file <path>");
return 1;
}
PrintInfo("Graph info: " + filename);
// Create root container and load
auto* container = AddRootContainer(filename);
if (!container) {
PrintError("Failed to create container");
return 1;
}
if (!LoadGraph(filename)) {
PrintError("Failed to load graph: " + GetLastError());
return 1;
}
auto info = GetGraphInfo();
printf("Nodes: %s\n", info["nodes"].c_str());
printf("Links: %s\n", info["links"].c_str());
// Print node types
printf("\nNode Types:\n");
for (const auto& [key, value] : info) {
if (key.find("type_") == 0) {
std::string type = key.substr(5); // Remove "type_" prefix
printf(" %s: %s\n", type.c_str(), value.c_str());
}
}
return 0;
}
int BlueprintsCLI::CommandHelp()
{
printf("Blueprints CLI - Node graph manipulation tool\n");
printf("\n");
printf("Usage:\n");
printf(" blueprints-example-console --headless --file <graph.json> --command <cmd> [options]\n");
printf("\n");
printf("Commands:\n");
printf(" validate Validate graph structure\n");
printf(" export Export to different format\n");
printf(" execute Execute graph (headless)\n");
printf(" info Display graph information\n");
printf(" help Show this help message\n");
printf("\n");
printf("Options:\n");
printf(" --file <path> Path to graph JSON file (required)\n");
printf(" --output <path> Output file path (for export)\n");
printf(" --format <fmt> Output format (json, xml, yaml, dot)\n");
printf("\n");
printf("Examples:\n");
printf(" # Validate a graph\n");
printf(" blueprints-example-console --headless --file graph.json --command validate\n");
printf("\n");
printf(" # Get graph info\n");
printf(" blueprints-example-console --headless --file graph.json --command info\n");
printf("\n");
printf(" # Export to JSON\n");
printf(" blueprints-example-console --headless --file graph.json --command export --output out.json\n");
printf("\n");
printf(" # Execute graph\n");
printf(" blueprints-example-console --headless --file graph.json --command execute\n");
printf("\n");
return 0;
}
// ============================================================================
// Output Helpers
// ============================================================================
void BlueprintsCLI::PrintError(const std::string& message)
{
fprintf(stderr, "ERROR: %s\n", message.c_str());
fflush(stderr);
}
void BlueprintsCLI::PrintSuccess(const std::string& message)
{
printf("SUCCESS: %s\n", message.c_str());
fflush(stdout);
}
void BlueprintsCLI::PrintInfo(const std::string& message)
{
printf("INFO: %s\n", message.c_str());
fflush(stdout);
}
// ============================================================================
// Member Helper Functions
// ============================================================================
std::string BlueprintsCLI::GetStringArg(const std::string& key, const std::string& defaultValue) const
{
return ::GetStringArg(m_Args, key, defaultValue);
}
bool BlueprintsCLI::GetBoolArg(const std::string& key, bool defaultValue) const
{
return ::GetBoolArg(m_Args, key, defaultValue);
}