Start CMake modernization scaffold
This commit is contained in:
113
tools/pano_cli/main.cpp
Normal file
113
tools/pano_cli/main.cpp
Normal file
@@ -0,0 +1,113 @@
|
||||
#include "foundation/result.h"
|
||||
|
||||
#include <charconv>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <string_view>
|
||||
|
||||
namespace {
|
||||
|
||||
struct DocumentArgs {
|
||||
std::uint32_t width = 0;
|
||||
std::uint32_t height = 0;
|
||||
std::uint32_t layers = 1;
|
||||
};
|
||||
|
||||
bool parse_u32(std::string_view text, std::uint32_t& value)
|
||||
{
|
||||
const auto* begin = text.data();
|
||||
const auto* end = text.data() + text.size();
|
||||
const auto [ptr, ec] = std::from_chars(begin, end, value);
|
||||
return ec == std::errc {} && ptr == end;
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
std::uint32_t value = 0;
|
||||
if (!parse_u32(argv[++i], value)) {
|
||||
return pp::foundation::Status::invalid_argument("option value must be an unsigned integer");
|
||||
}
|
||||
|
||||
if (key == "--width") {
|
||||
args.width = value;
|
||||
} else if (key == "--height") {
|
||||
args.height = value;
|
||||
} else {
|
||||
args.layers = 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;
|
||||
}
|
||||
Reference in New Issue
Block a user