PISM, A Parallel Ice Sheet Model  stable v2.1-1-g6902d5502 committed by Ed Bueler on 2023-12-20 08:38:27 -0800
pism_options.cc
Go to the documentation of this file.
1 // Copyright (C) 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021, 2023 PISM Authors
2 //
3 // This file is part of PISM.
4 //
5 // PISM is free software; you can redistribute it and/or modify it under the
6 // terms of the GNU General Public License as published by the Free Software
7 // Foundation; either version 3 of the License, or (at your option) any later
8 // version.
9 //
10 // PISM is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with PISM; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 #include <cstring>
20 
21 #include "pism/util/pism_options.hh"
22 #include "pism/util/pism_utilities.hh"
23 #include "pism/pism_config.hh"
24 
25 #include "pism/util/Logger.hh"
26 
27 namespace pism {
28 
29 //! \brief Print a usage message.
30 void show_usage(const Logger &log, const std::string &execname, const std::string &usage) {
31  log.message(1,
32  "%s is a PISM (http://www.pism.io) executable.\n"
33  "Options cheat-sheet:\n",
34  execname.c_str());
35  log.message(1, usage);
36  log.message(1,
37  "Parallel run using N processes (typical case): mpiexec -n N %s ...\n"
38  "For more help with PISM:\n"
39  " 1. download PDF User's Manual or read the online version on the website:\n"
40  " http://www.pism.io\n"
41  " 2. read browser for technical details:\n"
42  " http://www.pism.io/doxygen\n"
43  " 3. view issues/bugs at source host: https://github.com/pism/pism/issues\n"
44  " 4. do '%s -help | grep foo' to see PISM and PETSc options with 'foo'.\n"
45  " 5. email for help: uaf-pism@alaska.edu\n",
46  execname.c_str(), execname.c_str());
47 }
48 
49 //! @brief In a single call a driver program can provide a usage string to
50 //! the user and check if required options are given, and if not, end.
52  const std::string &execname,
53  const std::vector<std::string> &required_options,
54  const std::string &usage) {
55  const bool keep_running = false;
56  const bool terminate = true;
57 
58  log.message(2, "%s %s\n", execname.c_str(), pism::revision);
59 
60  if (options::Bool("-version", "stop after printing print PISM version")) {
61  log.message(2, pism::version());
62  return terminate;
63  }
64 
65  if (options::Bool("-usage", "print PISM usage")) {
66  show_usage(log, execname, usage);
67  return terminate;
68  }
69 
70  // go through list of required options, and if not given, fail
71  bool req_absent = false;
72  for (const auto &opt : required_options) {
73  if (not options::Bool(opt, "a required option")) {
74  req_absent = true;
75  log.error("PISM ERROR: option %s required\n", opt.c_str());
76  }
77  }
78 
79  if (req_absent) {
80  log.error("\n");
81  show_usage(log, execname, usage);
82  return terminate;
83  }
84 
85  // show usage message with -help, but don't stop
86  if (options::Bool("-help", "print help on all options")) {
87  show_usage(log, execname, usage);
88  }
89  return keep_running;
90 }
91 
92 } // end of namespace pism
void message(int threshold, const char format[],...) const __attribute__((format(printf
Print a message to the log.
Definition: Logger.cc:49
void error(const char format[],...) const __attribute__((format(printf
Print an error message to the log.
Definition: Logger.cc:77
A basic logging class.
Definition: Logger.hh:40
bool Bool(const std::string &option, const std::string &description)
Definition: options.cc:240
void show_usage(const Logger &log, const std::string &execname, const std::string &usage)
Print a usage message.
Definition: pism_options.cc:30
std::string version()
bool show_usage_check_req_opts(const Logger &log, const std::string &execname, const std::vector< std::string > &required_options, const std::string &usage)
In a single call a driver program can provide a usage string to the user and check if required option...
Definition: pism_options.cc:51