#include "foundation/parse.h" #include "foundation/result.h" #include #include #include namespace { struct DocumentArgs { std::uint32_t width = 0; std::uint32_t height = 0; std::uint32_t layers = 1; }; void print_error(std::string_view command, std::string_view message) { std::cout << "{\"ok\":false,\"command\":\"" << command << "\",\"error\":\"" << message << "\"}\n"; } void print_help() { std::cout << "pano_cli commands:\n" << " create-document --width N --height N [--layers N]\n" << " --help\n"; } pp::foundation::Status parse_document_args(int argc, char** argv, DocumentArgs& args) { for (int i = 2; i < argc; ++i) { const std::string_view key(argv[i]); if (key == "--width" || key == "--height" || key == "--layers") { if (i + 1 >= argc) { return pp::foundation::Status::invalid_argument("missing value for option"); } const auto value = pp::foundation::parse_u32(argv[++i]); if (!value) { return value.status(); } if (key == "--width") { args.width = value.value(); } else if (key == "--height") { args.height = value.value(); } else { args.layers = value.value(); } } else { return pp::foundation::Status::invalid_argument("unknown option"); } } if (args.width == 0 || args.height == 0) { return pp::foundation::Status::invalid_argument("width and height must be greater than zero"); } if (args.layers == 0) { return pp::foundation::Status::invalid_argument("layer count must be greater than zero"); } return pp::foundation::Status::success(); } int create_document(int argc, char** argv) { DocumentArgs args; const auto status = parse_document_args(argc, argv, args); if (!status.ok()) { print_error("create-document", status.message); return 2; } std::cout << "{\"ok\":true,\"command\":\"create-document\",\"document\":{" << "\"width\":" << args.width << ",\"height\":" << args.height << ",\"layers\":" << args.layers << "}}\n"; return 0; } } int main(int argc, char** argv) { if (argc < 2) { print_help(); return 1; } const std::string_view command(argv[1]); if (command == "--help" || command == "-h") { print_help(); return 0; } if (command == "create-document") { return create_document(argc, argv); } print_error(command, "unknown command"); return 2; }