MFEM  v4.6.0
Finite element discretization library
optparser.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2023, Lawrence Livermore National Security, LLC. Produced
2 // at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3 // LICENSE and NOTICE for details. LLNL-CODE-806117.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability visit https://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details.
11 
12 #ifndef MFEM_OPTPARSER
13 #define MFEM_OPTPARSER
14 
15 #include "../config/config.hpp"
16 #include "array.hpp"
17 
18 namespace mfem
19 {
20 
21 class Vector;
22 
23 /** Class for parsing command-line options.
24 
25  The class is initialized with argc and argv, and new options are added with
26  the AddOption method. Currently options of type bool, int, double, char*,
27  mfem::Array<int>, and mfem::Vector are supported.
28 
29  See the MFEM examples for sample use.
30 */
32 {
33 public:
35 
36 private:
37  struct Option
38  {
39  OptionType type;
40  void *var_ptr;
41  const char *short_name;
42  const char *long_name;
43  const char *description;
44  bool required;
45 
46  Option() = default;
47 
48  Option(OptionType type_, void *var_ptr_, const char *short_name_,
49  const char *long_name_, const char *description_, bool req)
50  : type(type_), var_ptr(var_ptr_), short_name(short_name_),
51  long_name(long_name_), description(description_), required(req) { }
52  };
53 
54  int argc;
55  char **argv;
56  Array<Option> options;
57  Array<int> option_check;
58  // error_type can be:
59  // 0 - no error
60  // 1 - print help message
61  // 2 - unrecognized option at argv[error_idx]
62  // 3 - missing argument for the last option argv[argc-1]
63  // 4 - option with index error_idx is specified multiple times
64  // 5 - invalid argument in argv[error_idx] for option in argv[error_idx-1]
65  // 6 - required option with index error_idx is missing
66  int error_type, error_idx;
67 
68  static void WriteValue(const Option &opt, std::ostream &out);
69 
70 public:
71 
72  /// Construct a command line option parser with 'argc_' and 'argv_'.
73  OptionsParser(int argc_, char *argv_[])
74  : argc(argc_), argv(argv_)
75  {
76  error_type = error_idx = 0;
77  }
78 
79  /** @brief Add a boolean option and set 'var' to receive the value.
80  Enable/disable tags are used to set the bool to true/false
81  respectively. */
82  void AddOption(bool *var, const char *enable_short_name,
83  const char *enable_long_name, const char *disable_short_name,
84  const char *disable_long_name, const char *description,
85  bool required = false)
86  {
87  options.Append(Option(ENABLE, var, enable_short_name, enable_long_name,
88  description, required));
89  options.Append(Option(DISABLE, var, disable_short_name, disable_long_name,
90  description, required));
91  }
92 
93  /// Add an integer option and set 'var' to receive the value.
94  void AddOption(int *var, const char *short_name, const char *long_name,
95  const char *description, bool required = false)
96  {
97  options.Append(Option(INT, var, short_name, long_name, description,
98  required));
99  }
100 
101  /// Add a double option and set 'var' to receive the value.
102  void AddOption(double *var, const char *short_name, const char *long_name,
103  const char *description, bool required = false)
104  {
105  options.Append(Option(DOUBLE, var, short_name, long_name, description,
106  required));
107  }
108 
109  /// Add a string (char*) option and set 'var' to receive the value.
110  void AddOption(const char **var, const char *short_name,
111  const char *long_name, const char *description,
112  bool required = false)
113  {
114  options.Append(Option(STRING, var, short_name, long_name, description,
115  required));
116  }
117 
118  /// Add a string (std::string) option and set 'var' to receive the value.
119  void AddOption(std::string *var, const char *short_name,
120  const char *long_name, const char *description,
121  bool required = false)
122  {
123  options.Append(Option(STD_STRING, var, short_name, long_name, description,
124  required));
125  }
126 
127  /** Add an integer array (separated by spaces) option and set 'var' to
128  receive the values. */
129  void AddOption(Array<int> * var, const char *short_name,
130  const char *long_name, const char *description,
131  bool required = false)
132  {
133  options.Append(Option(ARRAY, var, short_name, long_name, description,
134  required));
135  }
136 
137  /** Add a vector (doubles separated by spaces) option and set 'var' to
138  receive the values. */
139  void AddOption(Vector * var, const char *short_name,
140  const char *long_name, const char *description,
141  bool required = false)
142  {
143  options.Append(Option(VECTOR, var, short_name, long_name, description,
144  required));
145  }
146 
147  /** @brief Parse the command-line options.
148  Note that this function expects all the options provided through the
149  command line to have a corresponding AddOption. In particular, this
150  function cannot be used for partial parsing. */
151  void Parse();
152 
153  /// Parse the command line options, and exit with an error if the options
154  /// cannot be parsed successfully. The selected options are printed to the
155  /// given stream (defaulting to mfem::out).
156  void ParseCheck(std::ostream &out = mfem::out);
157 
158  /// Return true if the command line options were parsed successfully.
159  bool Good() const { return (error_type == 0); }
160 
161  /// Return true if we are flagged to print the help message.
162  bool Help() const { return (error_type == 1); }
163 
164  /// Print the options
165  void PrintOptions(std::ostream &out) const;
166 
167  /// Print the error message
168  void PrintError(std::ostream &out) const;
169 
170  /// Print the help message
171  void PrintHelp(std::ostream &out) const;
172 
173  /// Print the usage message
174  void PrintUsage(std::ostream &out) const;
175 };
176 
177 }
178 
179 #endif
void AddOption(int *var, const char *short_name, const char *long_name, const char *description, bool required=false)
Add an integer option and set &#39;var&#39; to receive the value.
Definition: optparser.hpp:94
void PrintOptions(std::ostream &out) const
Print the options.
Definition: optparser.cpp:331
void PrintUsage(std::ostream &out) const
Print the usage message.
Definition: optparser.cpp:462
void PrintError(std::ostream &out) const
Print the error message.
Definition: optparser.cpp:362
bool Good() const
Return true if the command line options were parsed successfully.
Definition: optparser.hpp:159
bool Help() const
Return true if we are flagged to print the help message.
Definition: optparser.hpp:162
void AddOption(const char **var, const char *short_name, const char *long_name, const char *description, bool required=false)
Add a string (char*) option and set &#39;var&#39; to receive the value.
Definition: optparser.hpp:110
OptionsParser(int argc_, char *argv_[])
Construct a command line option parser with &#39;argc_&#39; and &#39;argv_&#39;.
Definition: optparser.hpp:73
void Parse()
Parse the command-line options. Note that this function expects all the options provided through the ...
Definition: optparser.cpp:151
void AddOption(Array< int > *var, const char *short_name, const char *long_name, const char *description, bool required=false)
Definition: optparser.hpp:129
void AddOption(std::string *var, const char *short_name, const char *long_name, const char *description, bool required=false)
Add a string (std::string) option and set &#39;var&#39; to receive the value.
Definition: optparser.hpp:119
void AddOption(Vector *var, const char *short_name, const char *long_name, const char *description, bool required=false)
Definition: optparser.hpp:139
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
Definition: globals.hpp:66
void AddOption(bool *var, const char *enable_short_name, const char *enable_long_name, const char *disable_short_name, const char *disable_long_name, const char *description, bool required=false)
Add a boolean option and set &#39;var&#39; to receive the value. Enable/disable tags are used to set the bool...
Definition: optparser.hpp:82
void PrintHelp(std::ostream &out) const
Print the help message.
Definition: optparser.cpp:405
void AddOption(double *var, const char *short_name, const char *long_name, const char *description, bool required=false)
Add a double option and set &#39;var&#39; to receive the value.
Definition: optparser.hpp:102
Vector data type.
Definition: vector.hpp:58
void ParseCheck(std::ostream &out=mfem::out)
Definition: optparser.cpp:255