MFEM  v4.3.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
optparser.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2021, 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 an integer array (separated by spaces) option and set 'var' to
119  receive the values. */
120  void AddOption(Array<int> * var, const char *short_name,
121  const char *long_name, const char *description,
122  bool required = false)
123  {
124  options.Append(Option(ARRAY, var, short_name, long_name, description,
125  required));
126  }
127 
128  /** Add a vector (doubles separated by spaces) option and set 'var' to
129  receive the values. */
130  void AddOption(Vector * var, const char *short_name,
131  const char *long_name, const char *description,
132  bool required = false)
133  {
134  options.Append(Option(VECTOR, var, short_name, long_name, description,
135  required));
136  }
137 
138  /** @brief Parse the command-line options.
139  Note that this function expects all the options provided through the
140  command line to have a corresponding AddOption. In particular, this
141  function cannot be used for partial parsing. */
142  void Parse();
143 
144  /// Parse the command line options, and exit with an error if the options
145  /// cannot be parsed successfully. The selected options are printed to the
146  /// given stream (defaulting to mfem::out).
147  void ParseCheck(std::ostream &out = mfem::out);
148 
149  /// Return true if the command line options were parsed successfully.
150  bool Good() const { return (error_type == 0); }
151 
152  /// Return true if we are flagged to print the help message.
153  bool Help() const { return (error_type == 1); }
154 
155  /// Print the options
156  void PrintOptions(std::ostream &out) const;
157 
158  /// Print the error message
159  void PrintError(std::ostream &out) const;
160 
161  /// Print the help message
162  void PrintHelp(std::ostream &out) const;
163 
164  /// Print the usage message
165  void PrintUsage(std::ostream &out) const;
166 };
167 
168 }
169 
170 #endif
bool Help() const
Return true if we are flagged to print the help message.
Definition: optparser.hpp:153
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 PrintHelp(std::ostream &out) const
Print the help message.
Definition: optparser.cpp:401
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:150
int Append(const T &el)
Append element &#39;el&#39; to array, resize if necessary.
Definition: array.hpp:746
void AddOption(Array< int > *var, const char *short_name, const char *long_name, const char *description, bool required=false)
Definition: optparser.hpp:120
void AddOption(Vector *var, const char *short_name, const char *long_name, const char *description, bool required=false)
Definition: optparser.hpp:130
void PrintUsage(std::ostream &out) const
Print the usage message.
Definition: optparser.cpp:457
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 PrintError(std::ostream &out) const
Print the error message.
Definition: optparser.cpp:358
void PrintOptions(std::ostream &out) const
Print the options.
Definition: optparser.cpp:327
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:60
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 ParseCheck(std::ostream &out=mfem::out)
Definition: optparser.cpp:251
bool Good() const
Return true if the command line options were parsed successfully.
Definition: optparser.hpp:150