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);
161 const std::string& description,
162 const std::vector<int> &defaults) {
163 std::vector<double> default_value;
165 for (
auto v : defaults) {
166 default_value.push_back(v);
169 RealList input(option, description, default_value);
170 std::vector<int> result;
172 const double eps = 1e-6;
173 for (
auto v : input.
value()) {
174 if (fabs(v - floor(v)) > eps) {
176 "Can't process '%s': (%f is not an integer).",
179 result.push_back(
static_cast<int>(v));
190 const std::string& option,
191 const std::string& description,
192 const std::string& units,
193 double default_value) {
201 double result = strtod(input->c_str(), &endptr);
202 if (*endptr !=
'\0') {
208 input->c_str(), units.c_str());
209 e.
add_context(
"processing the command-line option %s",
214 this->
set(result,
true);
216 this->
set(default_value,
false);
222 const std::string& description,
223 const std::vector<double> &default_value) {
225 std::vector<double> result = default_value;
229 for (
const auto &p :
split(input,
',')) {
240 bool Bool(
const std::string& option,
241 const std::string& description) {
246 void deprecated(
const std::string &old_name,
const std::string &new_name) {
248 String option(old_name,
"no description",
"default",
253 " Please use '%s' instead.",
254 old_name.c_str(), new_name.c_str());
261 String option(name,
"no description",
"default");
264 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
IntegerList(const std::string &option, const std::string &description, const std::vector< int > &defaults)
const int & operator[](size_t index) const
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)
std::vector< int > m_value
void set(std::string new_value, bool new_flag)
RealList(const std::string &option, const std::string &description, const std::vector< double > &default_value)
const double & operator[](size_t index) 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.
double parse_number(const std::string &input)
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)
std::vector< std::string > split(const std::string &input, char separator)
Transform a separator-separated list (a string) into a vector of strings.