#include "app/Cli.hpp"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "spec/AppSpec.hpp"
namespace dp {
CliOptions CliOptions::parse(int argc, char** argv) {
CliOptions o;
auto next = [&](int& i, const char* flag) -> const char* {
if (i + 1 >= argc) {
o.error = std::string("missing value for ") + flag;
return "";
}
return argv[++i];
};
for (int i = 1; i < argc; ++i) {
const char* a = argv[i];
if (!std::strcmp(a, "--screen")) o.screen = next(i, a);
else if (!std::strcmp(a, "--screenshot")) o.screenshot = next(i, a);
else if (!std::strcmp(a, "--script")) o.script = next(i, a);
else if (!std::strcmp(a, "--base-url")) o.baseUrl = next(i, a);
else if (!std::strcmp(a, "--mock")) o.mock = true;
else if (!std::strcmp(a, "--as-user")) o.asUser = true;
else if (!std::strcmp(a, "--list-screens")) o.listScreens = true;
else if (!std::strcmp(a, "--dump-a11y")) o.dumpLabels = true;
else if (!std::strcmp(a, "--bench")) o.bench = std::atoi(next(i, a));
else if (!std::strcmp(a, "--scale")) o.scale = static_cast<float>(std::atof(next(i, a)));
else if (!std::strcmp(a, "--wait")) o.wait = std::atof(next(i, a));
else if (!std::strcmp(a, "--dark")) o.dark = 1;
else if (!std::strcmp(a, "--light")) o.dark = 0;
else if (!std::strcmp(a, "--fit-screen")) {
const int v = std::atoi(next(i, a));
if (v >= spec::AppSpec::kFitPercentMin && v <= spec::AppSpec::kFitPercentMax) o.fitScreen = v;
else o.error = "bad --fit-screen (expected a percentage between 20 and 100)";
} else if (!std::strcmp(a, "--size")) {
const char* v = next(i, a);
float w = 0, h = 0;
if (std::sscanf(v, "%fx%f", &w, &h) == 2 && w >= 200 && h >= 200 && w <= 4000 && h <= 4000) {
o.width = w;
o.height = h;
} else {
o.error = "bad --size (expected WxH between 200 and 4000)";
}
} else if (!std::strcmp(a, "--help") || !std::strcmp(a, "-h")) o.help = true;
else o.error = std::string("unknown option ") + a;
}
return o;
}
std::string CliOptions::usage() {
return "usage: devplaceapp [--mock] [--as-user] [--screen ID] [--screenshot FILE.png] [--script FILE]\n"
" [--size WxH] [--fit-screen PERCENT] [--scale N] [--dark|--light] [--wait SECONDS] [--base-url URL]\n"
" [--list-screens] [--dump-a11y] [--bench N]\n";
}
} // namespace dp