MFEM  v4.1.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-2020, 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  OptionsParser(int _argc, char *_argv[])
72  : argc(_argc), argv(_argv)
73  {
74  error_type = error_idx = 0;
75  }
76  void AddOption(bool *var, const char *enable_short_name,
77  const char *enable_long_name, const char *disable_short_name,
78  const char *disable_long_name, const char *description,
79  bool required = false)
80  {
81  options.Append(Option(ENABLE, var, enable_short_name, enable_long_name,
82  description, required));
83  options.Append(Option(DISABLE, var, disable_short_name, disable_long_name,
84  description, required));
85  }
86  void AddOption(int *var, const char *short_name, const char *long_name,
87  const char *description, bool required = false)
88  {
89  options.Append(Option(INT, var, short_name, long_name, description,
90  required));
91  }
92  void AddOption(double *var, const char *short_name, const char *long_name,
93  const char *description, bool required = false)
94  {
95  options.Append(Option(DOUBLE, var, short_name, long_name, description,
96  required));
97  }
98  void AddOption(const char **var, const char *short_name,
99  const char *long_name, const char *description,
100  bool required = false)
101  {
102  options.Append(Option(STRING, var, short_name, long_name, description,
103  required));
104  }
105  void AddOption(Array<int> * var, const char *short_name,
106  const char *long_name, const char *description,
107  bool required = false)
108  {
109  options.Append(Option(ARRAY, var, short_name, long_name, description,
110  required));
111  }
112  void AddOption(Vector * var, const char *short_name,
113  const char *long_name, const char *description,
114  bool required = false)
115  {
116  options.Append(Option(VECTOR, var, short_name, long_name, description,
117  required));
118  }
119 
120  /** Parse the command-line options. Note that this function expects all the
121  options provided through the command line to have a corresponding
122  AddOption. In particular, this function cannot be used for partial
123  parsing. */
124  void Parse();
125  bool Good() const { return (error_type == 0); }
126  bool Help() const { return (error_type == 1); }
127  void PrintOptions(std::ostream &out) const;
128  void PrintError(std::ostream &out) const;
129  void PrintHelp(std::ostream &out) const;
130  void PrintUsage(std::ostream &out) const;
131 };
132 
133 }
134 
135 #endif
bool Help() const
Definition: optparser.hpp:126
void AddOption(int *var, const char *short_name, const char *long_name, const char *description, bool required=false)
Definition: optparser.hpp:86
void PrintHelp(std::ostream &out) const
Definition: optparser.cpp:378
void AddOption(const char **var, const char *short_name, const char *long_name, const char *description, bool required=false)
Definition: optparser.hpp:98
int Append(const T &el)
Append element to array, resize if necessary.
Definition: array.hpp:707
void AddOption(Array< int > *var, const char *short_name, const char *long_name, const char *description, bool required=false)
Definition: optparser.hpp:105
void AddOption(Vector *var, const char *short_name, const char *long_name, const char *description, bool required=false)
Definition: optparser.hpp:112
void PrintUsage(std::ostream &out) const
Definition: optparser.cpp:434
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)
Definition: optparser.hpp:76
void PrintError(std::ostream &out) const
Definition: optparser.cpp:335
void PrintOptions(std::ostream &out) const
Definition: optparser.cpp:304
void AddOption(double *var, const char *short_name, const char *long_name, const char *description, bool required=false)
Definition: optparser.hpp:92
Vector data type.
Definition: vector.hpp:48
OptionsParser(int _argc, char *_argv[])
Definition: optparser.hpp:71
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
bool Good() const
Definition: optparser.hpp:125