MFEM  v3.4
Finite element discretization library
optparser.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
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(OptionType _type, void *_var_ptr, const char *_short_name,
47  const char *_long_name, const char *_description, bool req)
48  : type(_type), var_ptr(_var_ptr), short_name(_short_name),
49  long_name(_long_name), description(_description), required(req) { }
50  };
51 
52  int argc;
53  char **argv;
54  Array<Option> options;
55  Array<int> option_check;
56  // error_type can be:
57  // 0 - no error
58  // 1 - print help message
59  // 2 - unrecognized option at argv[error_idx]
60  // 3 - missing argument for the last option argv[argc-1]
61  // 4 - option with index error_idx is specified multiple times
62  // 5 - invalid argument in argv[error_idx] for option in argv[error_idx-1]
63  // 6 - required option with index error_idx is missing
64  int error_type, error_idx;
65 
66  static void WriteValue(const Option &opt, std::ostream &out);
67 
68 public:
69  OptionsParser(int _argc, char *_argv[])
70  : argc(_argc), argv(_argv)
71  {
72  error_type = error_idx = 0;
73  }
74  void AddOption(bool *var, const char *enable_short_name,
75  const char *enable_long_name, const char *disable_short_name,
76  const char *disable_long_name, const char *description,
77  bool required = false)
78  {
79  options.Append(Option(ENABLE, var, enable_short_name, enable_long_name,
80  description, required));
81  options.Append(Option(DISABLE, var, disable_short_name, disable_long_name,
82  description, required));
83  }
84  void AddOption(int *var, const char *short_name, const char *long_name,
85  const char *description, bool required = false)
86  {
87  options.Append(Option(INT, var, short_name, long_name, description,
88  required));
89  }
90  void AddOption(double *var, const char *short_name, const char *long_name,
91  const char *description, bool required = false)
92  {
93  options.Append(Option(DOUBLE, var, short_name, long_name, description,
94  required));
95  }
96  void AddOption(const char **var, const char *short_name,
97  const char *long_name, const char *description,
98  bool required = false)
99  {
100  options.Append(Option(STRING, var, short_name, long_name, description,
101  required));
102  }
103  void AddOption(Array<int> * var, const char *short_name,
104  const char *long_name, const char *description,
105  bool required = false)
106  {
107  options.Append(Option(ARRAY, var, short_name, long_name, description,
108  required));
109  }
110  void AddOption(Vector * var, const char *short_name,
111  const char *long_name, const char *description,
112  bool required = false)
113  {
114  options.Append(Option(VECTOR, var, short_name, long_name, description,
115  required));
116  }
117 
118 
119  void Parse();
120  bool Good() const { return (error_type == 0); }
121  bool Help() const { return (error_type == 1); }
122  void PrintOptions(std::ostream &out) const;
123  void PrintError(std::ostream &out) const;
124  void PrintHelp(std::ostream &out) const;
125  void PrintUsage(std::ostream &out) const;
126 };
127 
128 }
129 
130 #endif
void AddOption(int *var, const char *short_name, const char *long_name, const char *description, bool required=false)
Definition: optparser.hpp:84
void PrintOptions(std::ostream &out) const
Definition: optparser.cpp:304
void PrintUsage(std::ostream &out) const
Definition: optparser.cpp:434
void PrintError(std::ostream &out) const
Definition: optparser.cpp:335
bool Good() const
Definition: optparser.hpp:120
bool Help() const
Definition: optparser.hpp:121
void AddOption(const char **var, const char *short_name, const char *long_name, const char *description, bool required=false)
Definition: optparser.hpp:96
void AddOption(Array< int > *var, const char *short_name, const char *long_name, const char *description, bool required=false)
Definition: optparser.hpp:103
void AddOption(Vector *var, const char *short_name, const char *long_name, const char *description, bool required=false)
Definition: optparser.hpp:110
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:74
void PrintHelp(std::ostream &out) const
Definition: optparser.cpp:378
void AddOption(double *var, const char *short_name, const char *long_name, const char *description, bool required=false)
Definition: optparser.hpp:90
Vector data type.
Definition: vector.hpp:48
OptionsParser(int _argc, char *_argv[])
Definition: optparser.hpp:69
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:64