|
| 1 | +#include "arguments.hpp" |
| 2 | + |
| 3 | +#include <iostream> |
| 4 | +#include <string> |
| 5 | + |
| 6 | +namespace po = boost::program_options; |
| 7 | + |
| 8 | +auto validator_uint_greater_equal(const char* const option_name, uint64_t min) { |
| 9 | + return [option_name, min](auto n) { |
| 10 | + if (n < min) { |
| 11 | + throw po::validation_error( |
| 12 | + po::validation_error::invalid_option_value, option_name, std::to_string(static_cast<uint64_t>(n))); |
| 13 | + } |
| 14 | + }; |
| 15 | +} |
| 16 | + |
| 17 | +auto validator_uint_between(const char* const option_name, uint64_t min, uint64_t max) { |
| 18 | + return [option_name, min, max](auto n) { |
| 19 | + if (n < min || n > max) { |
| 20 | + throw po::validation_error( |
| 21 | + po::validation_error::invalid_option_value, option_name, std::to_string(static_cast<uint64_t>(n))); |
| 22 | + } |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +void validate_option_existence(const po::variables_map& args, const char* const option_name) { |
| 27 | + if (args.count(option_name) == 0) { |
| 28 | + // TODO(jessef): better exception |
| 29 | + throw po::validation_error(po::validation_error::at_least_one_value_required, option_name); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +void mode_validate_chase(const po::variables_map& args) { |
| 34 | + validate_option_existence(args, "initsize"); |
| 35 | + validate_option_existence(args, "initstart"); |
| 36 | + validate_option_existence(args, "initcount"); |
| 37 | +} |
| 38 | + |
| 39 | +void mode_validate_pounce(const po::variables_map& args) { validate_option_existence(args, "initfile"); } |
| 40 | + |
| 41 | +po::variables_map parse_arguments(int argc, char** argv) { |
| 42 | + po::options_description general_options("General options"); |
| 43 | + po::options_description chase_options("Chase-mode options"); |
| 44 | + po::options_description pounce_options("Pounce-mode options"); |
| 45 | + |
| 46 | + // clang-format off |
| 47 | + general_options.add_options() |
| 48 | + ("help,h", po::bool_switch(), |
| 49 | + "Print help text") |
| 50 | + ("version,v", po::bool_switch(), |
| 51 | + "Print program version") |
| 52 | + ("chase,c", po::bool_switch(), |
| 53 | + "Chase mode (range search)") |
| 54 | + ("pounce,p", po::bool_switch(), |
| 55 | + "Pounce mode (file search)") |
| 56 | + ("outfile,o", po::value<std::string>()->default_value("./output.postresult")->value_name("path.postresult"), |
| 57 | + "Path to output file") |
| 58 | + ("maxsize,x", po::value<uint64_t>()->default_value(static_cast<uint64_t>(1e9), "10^9")->value_name("size"), |
| 59 | + "Maximum tape length to evaluate each initial condition to (0 = no limit)") |
| 60 | + ("maxoutsize,z", po::value<uint64_t>()->value_name("size"), |
| 61 | + "Maximum tape length to include in output file entries (0 = never write final tapes; omit for no limit)") |
| 62 | + ("maxsteps,m", po::value<uint64_t>()->default_value(static_cast<uint64_t>(1e10), "10^10")->value_name("steps"), |
| 63 | + "Maximum number of steps to evaluate each initial condition to (0 = no limit)") |
| 64 | + ("timeout,t", po::value<uint32_t>()->default_value(0)->value_name("secs"), |
| 65 | + "Total execution time constraint (seconds) (0 = no limit)"); |
| 66 | + |
| 67 | + chase_options.add_options() |
| 68 | + ("cribfile,f", po::value<std::string>()->value_name("path.postcrib"), |
| 69 | + "Path to crib file (list of known sequences)") |
| 70 | + ("initsize,l", po::value<uint16_t>()->value_name("size") |
| 71 | + ->notifier(validator_uint_between("initsize", 1, 64)), |
| 72 | + "Size of initial condition tapes") |
| 73 | + ("initstart,s", po::value<uint64_t>()->value_name("start"), |
| 74 | + "Starting initial condition tape (as decimal integer)") |
| 75 | + ("initcount,n", po::value<uint64_t>()->default_value(1)->value_name("count") |
| 76 | + ->notifier(validator_uint_greater_equal("initcount", 1)), |
| 77 | + "Number of initial condition tapes to evaluate") |
| 78 | + ("initoffset,e", po::value<uint64_t>()->default_value(0)->value_name("offset"), |
| 79 | + "Shifts starting condition by offset * count (for use with zero-indexed array jobs)"); |
| 80 | + |
| 81 | + pounce_options.add_options() |
| 82 | + ("initfile,i", po::value<std::string>()->value_name("path.postinit"), |
| 83 | + "Path to file of initial conditions"); |
| 84 | + // clang-format on |
| 85 | + |
| 86 | + po::options_description all_options; |
| 87 | + all_options.add(general_options).add(chase_options).add(pounce_options); |
| 88 | + |
| 89 | + po::variables_map args; |
| 90 | + po::store(po::command_line_parser(argc, argv).options(all_options).run(), args); |
| 91 | + po::notify(args); |
| 92 | + |
| 93 | + if (args["help"].as<bool>()) { |
| 94 | + std::cout << all_options << "\n"; |
| 95 | + return args; |
| 96 | + } |
| 97 | + |
| 98 | + if (args["version"].as<bool>()) { |
| 99 | +#if defined(POST_TAG_VERSION_MAJOR) && defined(POST_TAG_VERSION_MINOR) && defined(POST_TAG_VERSION_PATCH) |
| 100 | + printf("wolfram-postproject v%u.%u.%u =^._.^=\n", |
| 101 | + POST_TAG_VERSION_MAJOR, |
| 102 | + POST_TAG_VERSION_MINOR, |
| 103 | + POST_TAG_VERSION_PATCH); |
| 104 | +#else |
| 105 | + printf("wolfram-postproject unknown version =^._.^=\n"); |
| 106 | +#endif |
| 107 | + |
| 108 | + return args; |
| 109 | + } |
| 110 | + |
| 111 | + if (args["chase"].as<bool>() && args["pounce"].as<bool>()) { |
| 112 | + throw std::runtime_error("Only one of --chase or --pounce may be specified"); |
| 113 | + } else if (args["chase"].as<bool>()) { |
| 114 | + mode_validate_chase(args); |
| 115 | + } else if (args["pounce"].as<bool>()) { |
| 116 | + mode_validate_pounce(args); |
| 117 | + } else { |
| 118 | + throw std::runtime_error("One of --chase or --pounce must be specified"); |
| 119 | + } |
| 120 | + |
| 121 | + return args; |
| 122 | +} |
0 commit comments