options.cpp
1#include "itch/app/options.hpp"2#include <algorithm>3#include <stdexcept>45namespace itch {6namespace {7constexpr const char* usage =8 "live: itch_replay --live --listen IPv4:PORT --source IPv4:PORT --recovery IPv4:PORT --session "9 "TEN_BYTES --symbol SYMBOL [--output CSV] [--depth N] [--check-book] [--idle-timeout-ms N] "10 "[--recovery-timeout-ms N] [--retry-ms N] [--max-buffer-messages N] [--max-buffer-bytes N]\n"11 "usage: itch_replay --input FILE --symbol SYMBOL [--output CSV] [--verify] [--depth N] "12 "[--check-book] [--allow-implicit-eof] [--with-offset] [--checkpoint-out FILE] "13 "[--checkpoint-every-frames N] [--stop-after-frame N] [--resume FILE] [--resume-csv]";1415uint64_t parse_frame(const std::string& value, const std::string& option = "--stop-after-frame") {16 if (value.empty() || !std::all_of(value.begin(), value.end(), [](char c) {17 return c >= '0' && c <= '9';18 })) {19 throw std::runtime_error(option + " must be a nonnegative integer");20 }21 try {22 const auto n = std::stoull(value);23 if (n == UINT64_MAX) {24 throw std::out_of_range("frame");25 }26 return n;27 } catch (const std::exception&) {28 throw std::runtime_error(option + " must be a nonnegative integer below UINT64_MAX");29 }30}3132size_t parse_depth(const std::string& value) {33 if (value.empty() || !std::all_of(value.begin(), value.end(), [](char c) {34 return c >= '0' && c <= '9';35 })) {36 throw std::runtime_error("--depth must be an integer from 1 to 1000");37 }38 unsigned long long requested = 0;39 try {40 requested = std::stoull(value);41 } catch (const std::exception&) {42 throw std::runtime_error("--depth must be an integer from 1 to 1000");43 }44 if (requested == 0 || requested > 1000) {45 throw std::runtime_error("--depth must be an integer from 1 to 1000");46 }47 return static_cast<size_t>(requested);48}4950// Presence tracking belongs to CLI parsing, not to the library configuration.51struct ParsedOptions {52 Options options;53 bool network_options = false;54};5556class Arguments {57 public:58 Arguments(int argc, char** argv) : argc_(argc), argv_(argv) {}5960 bool next(std::string& argument) {61 if (index_ >= argc_) {62 return false;63 }64 argument = argv_[index_++];65 return true;66 }6768 std::string value() {69 if (index_ >= argc_) {70 throw std::runtime_error(usage);71 }72 return argv_[index_++];73 }7475 private:76 int argc_;77 char** argv_;78 int index_ = 1;79};8081bool parse_shared_option(const std::string& arg, Arguments& args, Options& options) {82 if (arg == "--symbol") {83 options.book.target = args.value();84 } else if (arg == "--output") {85 options.output.path = args.value();86 } else if (arg == "--check-book") {87 options.book.check_book = true;88 } else if (arg == "--depth") {89 options.book.max_depth = parse_depth(args.value());90 } else {91 return false;92 }93 return true;94}9596bool parse_replay_option(const std::string& arg, Arguments& args, ReplayOptions& options) {97 if (arg == "--input") {98 options.path = args.value();99 } else if (arg == "--verify") {100 options.verify = true;101 } else if (arg == "--allow-implicit-eof") {102 options.allow_implicit_eof = true;103 } else if (arg == "--with-offset") {104 options.with_offset = true;105 } else {106 return false;107 }108 return true;109}110111bool parse_checkpoint_option(const std::string& arg, Arguments& args, CheckpointOptions& options) {112 if (arg == "--checkpoint-out") {113 options.path = args.value();114 } else if (arg == "--resume") {115 options.resume_path = args.value();116 } else if (arg == "--resume-csv") {117 options.resume_csv = true;118 } else if (arg == "--checkpoint-every-frames") {119 options.every_frames = parse_frame(args.value(), arg);120 if (!options.every_frames) {121 throw std::runtime_error("--checkpoint-every-frames must be positive");122 }123 } else if (arg == "--stop-after-frame") {124 options.stop_after_frame = parse_frame(args.value(), arg);125 } else {126 return false;127 }128 return true;129}130131bool parse_network_option(const std::string& arg, Arguments& args, LiveOptions& options) {132 if (arg == "--listen") {133 options.listen = args.value();134 } else if (arg == "--source") {135 options.source = args.value();136 } else if (arg == "--recovery") {137 options.recovery = args.value();138 } else if (arg == "--session") {139 options.session = args.value();140 } else if (arg == "--idle-timeout-ms" || arg == "--recovery-timeout-ms" ||141 arg == "--retry-ms" || arg == "--max-buffer-messages" ||142 arg == "--max-buffer-bytes") {143 const auto value = parse_frame(args.value(), arg);144 if (!value || value > 1000000000) {145 throw std::runtime_error(arg + " must be 1..1000000000");146 }147 if (arg == "--idle-timeout-ms") {148 options.idle_ms = value;149 } else if (arg == "--recovery-timeout-ms") {150 options.recovery_ms = value;151 } else if (arg == "--retry-ms") {152 options.retry_ms = value;153 } else if (arg == "--max-buffer-messages") {154 options.max_messages = value;155 } else {156 options.max_bytes = value;157 }158 } else {159 return false;160 }161 return true;162}163164ParsedOptions parse_arguments(int argc, char** argv) {165 ParsedOptions parsed;166 Arguments args(argc, argv);167 std::string arg;168 while (args.next(arg)) {169 if (arg == "--live") {170 parsed.options.live = true;171 } else if (parse_shared_option(arg, args, parsed.options) ||172 parse_replay_option(arg, args, parsed.options.replay) ||173 parse_checkpoint_option(arg, args, parsed.options.checkpoint)) {174 // The matching group consumed the option and any required value.175 } else if (parse_network_option(arg, args, parsed.options.network)) {176 parsed.network_options = true;177 } else {178 throw std::runtime_error(usage);179 }180 }181 return parsed;182}183184void resolve_defaults(Options& options) {185 if (options.live) {186 return;187 }188 // Saving is requested by a destination, an interval, or an explicit stop.189 // CSV continuation never enables checkpoint writes on its own.190 if (options.checkpoint.path.empty() && !options.checkpoint.resume_path.empty() &&191 (options.checkpoint.every_frames || options.checkpoint.stop_after_frame)) {192 options.checkpoint.path = options.checkpoint.resume_path;193 }194}195196void validate_shared_options(const Options& options) {197 if ((!options.live && options.replay.path.empty()) || options.book.target.empty() ||198 options.book.target.size() > 8) {199 throw std::runtime_error(usage);200 }201}202203void validate_mode_options(const ParsedOptions& parsed) {204 const auto& options = parsed.options;205 if (options.live) {206 if (!options.replay.path.empty() || options.replay.verify ||207 options.replay.allow_implicit_eof || options.replay.with_offset ||208 !options.checkpoint.path.empty() || !options.checkpoint.resume_path.empty() ||209 options.checkpoint.resume_csv || options.checkpoint.stop_after_frame ||210 options.checkpoint.every_frames) {211 throw std::runtime_error(212 "live mode cannot use file replay/checkpoint/verify/offset options");213 }214 if (options.network.listen.empty() || options.network.source.empty() ||215 options.network.recovery.empty() || options.network.session.size() != 10) {216 throw std::runtime_error("--live requires --listen IPv4:PORT --source IPv4:PORT "217 "--recovery IPv4:PORT --session TEN_BYTES");218 }219 } else if (parsed.network_options) {220 throw std::runtime_error("network options require --live");221 }222}223224void validate_checkpoint_options(const Options& options) {225 if (options.checkpoint.resume_csv) {226 if (options.checkpoint.resume_path.empty() || options.output.path.empty()) {227 throw std::runtime_error("--resume-csv requires --resume and --output");228 }229 }230 if (options.checkpoint.every_frames && options.checkpoint.path.empty()) {231 throw std::runtime_error("--checkpoint-every-frames requires --checkpoint-out");232 }233 if (options.checkpoint.stop_after_frame && options.checkpoint.path.empty()) {234 throw std::runtime_error("--stop-after-frame requires --checkpoint-out");235 }236}237238} // namespace239240Options parse_options(int argc, char** argv) {241 auto parsed = parse_arguments(argc, argv);242 resolve_defaults(parsed.options);243 validate_shared_options(parsed.options);244 validate_mode_options(parsed);245 validate_checkpoint_options(parsed.options);246 return parsed.options;247}248249} // namespace itch