Manticore  Version 2.0alpha
Physics of Molecular Clouds
CommandLine.h
Go to the documentation of this file.
1 
8 #ifndef MUTILS_COMMANDLINE_H
9 #define MUTILS_COMMANDLINE_H
10 
11 #include <cctype>
12 #include "mutils/util.h"
13 
14 namespace mutils {
15 
16 // Forward reference.
17 class Options;
18 
48 {
49 public:
51  typedef std::vector<std::string> Arguments;
52 
69  struct Option {
70  char shortName;
71  std::string longName;
72  std::string valueName;
73  std::string scopeName;
74  std::string sectName;
75  std::string keyName;
76  std::string helpText;
77 
89  std::string keyword() const {
90  std::string shortStr(1, shortName);
91  std::string key = (!keyName.empty() ? keyName :
92  longName.empty() ? shortStr : longName);
93  if (keyName == nullStr || keyName.empty()) {
94  std::string::size_type pos = 0;
95  while (pos < key.size()) {
96  key[pos] = toupper(key[pos]);
97  pos = key.find_first_of('-', pos);
98  if (pos != npos) { key.erase(pos, 1); }
99  }
100  }
101  return key;
102  }
103 
105  std::string source() const {
106  std::string src = "CommandLine[";
107  if (! longName.empty()) {
108  src += "--";
109  src += longName;
110  } else {
111  src += '-';
112  src += toupper(shortName);
113  }
114  src += ']';
115  return src;
116  }
117  };
118 
120  CommandLine() : validOpts_(nullptr) { }
121 
124 
126  void init(int argc, char *argv[], const std::vector<Option> *opts);
127 
129  const std::string& driver() const { return driver_; }
130 
132  const Arguments& arguments() const { return args_; }
133 
135  unsigned getOption(const Option &option, Arguments *values = nullptr,
136  unsigned nMax = -1) const;
137 
139  unsigned getOption(char shortName, Arguments *values = nullptr,
140  unsigned nMax = -1) const;
141 
143  unsigned getOption(const std::string &longName,
144  Arguments *values = nullptr,
145  unsigned nMax = -1) const;
146 
148  size_t popOption(const Option &option);
149 
151  size_t popOption(char shortName);
152 
154  size_t popOption(const std::string &longName);
155 
157  long getInteger(char shortName) const;
158 
160  long getInteger(char shortName, long defValue) const;
161 
163  double getDouble(char shortName, double defValue) const;
164 
166  std::string getString(char shortName, const std::string &defValue) const;
167 
169  void merge(Options &options) const;
170 
172  void usage(FILE *f, const std::vector<Option> &opts,
173  const char *args = "",
174  bool quiet = false, bool basename = true) const;
175 
176 private:
178  typedef std::multimap<char, std::string> ShortMap;
179 
181  typedef std::multimap<std::string, std::string> LongMap;
182 
184  bool parseShort(const char *name, const char *next);
185 
187  bool parseLong(const char *name, const char *next);
188 
190  const std::vector<Option> *validOpts_;
191 
194 
197 
199  std::string driver_;
200 
203 };
204 
205 } // namespace mutils
206 
207 #endif // MUTILS_COMMANDLINE_H
std::string valueName
Value name (for --help).
Definition: CommandLine.h:72
unsigned getOption(const Option &option, Arguments *values=nullptr, unsigned nMax=-1) const
Retrieves all copies of a command line option.
Definition: CommandLine.cc:197
Command line option specification.
Definition: CommandLine.h:69
const Arguments & arguments() const
Returns remaining arguments (read-only).
Definition: CommandLine.h:132
Command line options and arguments.
Definition: CommandLine.h:47
std::string longName
Long option name.
Definition: CommandLine.h:71
~CommandLine()
Destructor.
Definition: CommandLine.h:123
std::string getString(char shortName, const std::string &defValue) const
Retrieves unique string option.
Definition: CommandLine.cc:392
std::string scopeName
Associated Options scope name.
Definition: CommandLine.h:73
std::vector< std::string > Arguments
Command line arguments type.
Definition: CommandLine.h:51
void merge(Options &options) const
Merges command line into options dictionary.
std::multimap< std::string, std::string > LongMap
Long option map type.
Definition: CommandLine.h:181
size_t popOption(const Option &option)
Removes and destroys all copies of a command line option.
Definition: CommandLine.cc:290
const std::string nullStr(1, 0)
const std::vector< Option > * validOpts_
Recognized command line options.
Definition: CommandLine.h:190
bool parseLong(const char *name, const char *next)
Processes a long option (possibly with a value).
Definition: CommandLine.cc:25
std::multimap< char, std::string > ShortMap
Short option map type.
Definition: CommandLine.h:178
bool parseShort(const char *name, const char *next)
Processes one or more short options (the last possibly with a value).
Definition: CommandLine.cc:83
The MathUtils miscellaneous utilities library.
MathUtils package.
Definition: CommandLine.cc:10
std::string::size_type npos
Definition: statics.cc:24
std::string driver_
Invoked driver program name (argv[0] value).
Definition: CommandLine.h:199
std::string helpText
Option description (for --help).
Definition: CommandLine.h:76
std::string sectName
Associated Options section name.
Definition: CommandLine.h:74
void init(int argc, char *argv[], const std::vector< Option > *opts)
Initializes new command line input.
Definition: CommandLine.cc:151
std::string keyword() const
Standard Options keyword name for this option.
Definition: CommandLine.h:89
const std::string & driver() const
Returns invoked driver program name (read-only).
Definition: CommandLine.h:129
LongMap longOpts_
Long option map.
Definition: CommandLine.h:196
std::string keyName
Associated Options keyword name.
Definition: CommandLine.h:75
void usage(FILE *f, const std::vector< Option > &opts, const char *args="", bool quiet=false, bool basename=true) const
Summarizes command line usage (including options).
Definition: CommandLine.cc:411
std::string source() const
Command line option source information (for Options::MetaValues).
Definition: CommandLine.h:105
long getInteger(char shortName) const
Retrieves unique decimal integer option.
Definition: CommandLine.cc:341
ShortMap shortOpts_
Short option map.
Definition: CommandLine.h:193
char shortName
Short option name.
Definition: CommandLine.h:70
double getDouble(char shortName, double defValue) const
Retrieves unique floating point option.
Definition: CommandLine.cc:376
Arguments args_
Trailing command line arguments.
Definition: CommandLine.h:202
CommandLine()
Default constructor.
Definition: CommandLine.h:120