24#include "pism/util/error_handling.hh"
25#include "pism/util/Logger.hh"
26#include "pism/util/Units.hh"
27#include "pism/util/pism_utilities.hh"
28#include "pism/util/pism_options.hh"
34 const std::string& description) {
42 const std::string& description,
43 const std::string& default_value,
45 int errcode =
process(option, description, default_value, argument_flag);
54 const std::string& description,
55 const std::string& default_value,
61 PetscBool flag = PETSC_FALSE;
64 ierr = PetscOptionsGetString(NULL,
70 PISM_CHK(ierr,
"PetscOptionsGetString");
72 std::string result =
string;
74 if (flag == PETSC_TRUE) {
80 "command line option '%s'\n"
82 "requires an argument.",
83 option.c_str(), description.c_str());
86 this->
set(result,
true);
89 this->
set(default_value,
false);
96 const std::string& description,
97 const std::string& choices,
98 const std::string& default_value) {
100 if (choices.empty()) {
104 std::string list =
"[" + choices +
"]";
105 std::string long_description = description +
" Choose one of " + list;
115 std::string word = input;
117 size_t n = word.find(
':');
118 if (
n != std::string::npos) {
124 auto choices_set =
set_split(choices,
',');
127 if (choices_set.find(word) != choices_set.end()) {
128 this->
set(word,
true);
131 "invalid %s argument: '%s'. Please choose one of %s.\n",
132 option.c_str(), word.c_str(), list.c_str());
137 const std::string& description,
140 String input(option, description,
149 e.
add_context(
"processing command-line option '%s %s'",
150 option.c_str(), input->c_str());
153 this->
set(
static_cast<int>(result),
true);
155 this->
set(
static_cast<int>(default_value),
false);
160 const std::string& option,
161 const std::string& description,
162 const std::string& units,
163 double default_value) {
171 double result = strtod(input->c_str(), &endptr);
172 if (*endptr !=
'\0') {
178 input->c_str(), units.c_str());
179 e.
add_context(
"processing the command-line option %s",
184 this->
set(result,
true);
186 this->
set(default_value,
false);
190bool Bool(
const std::string& option,
191 const std::string& description) {
190bool Bool(
const std::string& option, {
…}
196void deprecated(
const std::string &old_name,
const std::string &new_name) {
198 String option(old_name,
"no description",
"default",
203 " Please use '%s' instead.",
204 old_name.c_str(), new_name.c_str());
196void deprecated(
const std::string &old_name,
const std::string &new_name) {
…}
211 String option(name,
"no description",
"default");
214 log.
message(1,
"PISM WARNING: ignoring command-line option '%s'.\n",
void message(int threshold, const char format[],...) const __attribute__((format(printf
Print a message to the log.
void add_context(const std::string &message)
Add a message providing some context. This way we can (sort of) get a stack trace even though C++ exc...
static RuntimeError formatted(const ErrorLocation &location, const char format[],...) __attribute__((format(printf
build a RuntimeError with a formatted message
Integer(const std::string &option, const std::string &description, int default_value)
Keyword(const std::string &option, const std::string &description, const std::string &choices, const std::string &default_value)
void set(std::string new_value, bool new_flag)
operator std::string() const
Real(std::shared_ptr< units::System > system, const std::string &option, const std::string &description, const std::string &units, double default_value)
String(const std::string &option, const std::string &description)
int process(const std::string &option, const std::string &description, const std::string &default_value, ArgumentFlag flag)
#define PISM_CHK(errcode, name)
#define PISM_ERROR_LOCATION
static const int TEMPORARY_STRING_LENGTH
void deprecated(const std::string &old_name, const std::string &new_name)
Stop if an option old_name is set, printing a message that new_name should be used instead.
bool Bool(const std::string &option, const std::string &description)
void ignored(const Logger &log, const std::string &name)
Print a warning telling the user that an option was ignored.
void forbidden(const std::string &name)
Stop if an option name is set.
double convert(System::Ptr system, double input, const std::string &spec1, const std::string &spec2)
Convert a quantity from unit1 to unit2.
std::string printf(const char *format,...)
std::set< std::string > set_split(const std::string &input, char separator)
Transform a separator-separated list (a string) into a set of strings.
long int parse_integer(const std::string &input)