12 std::string out_filename_(
const std::string& filename) {
13 if(filename ==
"")
return "";
17 std::cout <<
"overwriting existing output file " << filename << std::endl;
19 std::cout <<
"output file " << filename <<
" exists. Proceed? [y/n] ";
22 if(answer !=
'y' && answer !=
'Y')
throw std::runtime_error(
"not overwriting output file, exiting");
28 std::string out_dirname_(
const std::string& dirname) {
29 if(dirname ==
"")
return "";
34 if(!
is_directory(dirname))
throw std::runtime_error(
"file exists at " + dirname);
45 else throw std::logic_error(
"args_list not initialized (get_args was not called)");
49 void get_args(
int argc,
const char* argv[],
const std::string& usage) {
50 if(argc >= 2 && std::string(argv[1]) ==
"--help") {
51 std::cout <<
"usage: " << argv[0] <<
" " << usage << std::endl;
52 std::exit(EXIT_SUCCESS);
54 args_list::instance = std::make_unique<args_list>(argc, argv, usage);
59 const char* batch_mode_env = std::getenv(
"LICORNEA_BATCH_MODE");
60 if(batch_mode_env ==
nullptr)
return false;
61 else return (std::string(batch_mode_env) ==
"1");
65 executable_name_(argv[0]),
69 for(
int i = 1; i < argc; ++i) args_.push_back(argv[i]);
73 std::cerr <<
"usage: " << executable_name_ <<
' ' << usage_ << std::endl;
74 if(! error.empty()) std::cerr <<
" " << error << std::endl;
75 std::exit(EXIT_FAILURE);
79 if(arg_index_ >= args_.size())
81 return args_.at(arg_index_++);
85 if(arg_index_ >= args_.size())
87 return (val == args_.at(arg_index_));
91 return (arg_index_ < args_.size());
106 if(filename ==
"-")
throw std::runtime_error(
"expected filename argument, not -");
107 return out_filename_(filename);
112 return out_filename_(def);
115 return out_filename_(def);;
123 if(dirname ==
"-")
throw std::runtime_error(
"expected dirname argument, not -");
124 return out_dirname_(dirname);
129 return out_dirname_(def);
132 return out_dirname_(def);
141 long i = std::strtol(str, &str_end, 10);
142 if(str_end == str)
args().
usage_fail(
"`" + std::string(str) +
"` is not an integer");
149 double f = std::strtod(str, &str_end);
150 if(str_end == str)
args().
usage_fail(
"`" + std::string(str) +
"` is not a real");
154 std::string
enum_arg(
const std::vector<std::string>& options) {
156 for(
const std::string& option : options)
157 if(str == option)
return option;
163 return (str == expected);
169 auto j_idx = explode_from_string<int>(
',', str);
171 if(j_idx.size() == 2) idx.
y = j_idx[1];
args_list(int argc, const char *argv[], const std::string &usage)
bool file_exists(const std::string &filename)
bool bool_arg(const std::string &expected)
bool has_next_arg() const
void usage_fail(const std::string &error="") const
std::string in_filename_arg()
std::string enum_arg(const std::vector< std::string > &options)
bool is_directory(const std::string &filename)
static std::unique_ptr< args_list > instance
std::string out_filename_opt_arg(const std::string &def)
std::string out_dirname_arg()
std::string out_dirname_opt_arg(const std::string &def)
std::string to_string(const T &)
std::string out_filename_arg()
void make_directory(const std::string &dirname)
bool next_arg_is(const std::string &val) const
void make_parent_directories(const std::string &filename)
view_index view_index_arg()
void get_args(int argc, const char *argv[], const std::string &usage)