TrueReality  v0.1.1912
ArgumentParser.cpp
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2021 Acid Rain Studios LLC
4 *
5 * This library is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License as published by the Free
7 * Software Foundation; either version 3.0 of the License, or (at your option)
8 * any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * @author Maxim Serebrennik
20 */
21 
22 #include <trUtil/ArgumentParser.h>
23 
24 namespace trUtil
25 {
28  {
29  mParam = new osg::ArgumentParser::Parameter(value);
30  }
31 
34  {
35  mParam = new osg::ArgumentParser::Parameter(value);
36  }
37 
40  {
41  mParam = new osg::ArgumentParser::Parameter(value);
42  }
43 
46  {
47  mParam = new osg::ArgumentParser::Parameter(value);
48  }
49 
52  {
53  mParam = new osg::ArgumentParser::Parameter(value);
54  }
55 
58  {
59  mParam = new osg::ArgumentParser::Parameter(value);
60  }
61 
64  {
65  mParam = new osg::ArgumentParser::Parameter(param);
66  }
67 
70  {
71  if (mParam != nullptr)
72  {
73  delete mParam;
74  mParam = nullptr;
75  }
76  }
77 
80  {
81  mParam = param.mParam;
82  return *this;
83  }
84 
86  inline bool ArgumentParser::Parameter::Valid(const char* str) const
87  {
88  return mParam->valid(str);
89  }
90 
92  bool ArgumentParser::Parameter::Assign(const char* str)
93  {
94  return mParam->assign(str);
95  }
96 
98  ArgumentParser::Parameter::operator osg::ArgumentParser::Parameter() const
99  {
100  return *mParam;
101  }
102 
104  ArgumentParser::Parameter::operator osg::ArgumentParser::Parameter&()
105  {
106  return *mParam;
107  }
108 
110  ArgumentParser::Parameter::operator const osg::ArgumentParser::Parameter&() const
111  {
112  return *mParam;
113  }
114 
116  ArgumentParser::Parameter::operator osg::ArgumentParser::Parameter*()
117  {
118  return mParam;
119  }
120 
122  ArgumentParser::ArgumentParser(int* argc, char **argv)
123  {
124  mArgParser = std::make_unique<osg::ArgumentParser>(argc, argv);
125  mAppUsage = std::make_unique<ApplicationUsage>(mArgParser->getApplicationUsage());
126  }
127 
130  {
131  }
132 
135  {
136  return *mArgParser.get();
137  }
138 
140  const osg::ArgumentParser & ArgumentParser::GetOSGArgumentParser() const
141  {
142  return *mArgParser.get();
143  }
144 
146  bool ArgumentParser::IsOption(const char* str) const
147  {
148  return mArgParser->isOption(str);
149  }
150 
152  bool ArgumentParser::IsString(const char* str) const
153  {
154  return mArgParser->isString(str);
155  }
156 
158  bool ArgumentParser::IsNumber(const char* str) const
159  {
160  return mArgParser->isNumber(str);
161  }
162 
164  bool ArgumentParser::IsBool(const char* str) const
165  {
166  return mArgParser->isBool(str);
167  }
168 
171  {
172  mAppUsage.reset(usage);
173  mArgParser->setApplicationUsage(mAppUsage.get()->operator osg::ApplicationUsage *());
174  }
175 
178  {
179  return mAppUsage.get();
180  }
181 
184  {
185  return mAppUsage.get();
186  }
187 
190  {
191  return mArgParser->argc();
192  }
193 
196  {
197  return mArgParser->argv();
198  }
199 
202  {
203  return mArgParser->getApplicationName();
204  }
205 
207  int ArgumentParser::Find(const std::string& str) const
208  {
209  return mArgParser->find(str);
210  }
211 
213  bool ArgumentParser::IsOption(int pos) const
214  {
215  return mArgParser->isOption(pos);
216  }
217 
219  bool ArgumentParser::IsString(int pos) const
220  {
221  return mArgParser->isString(pos);
222  }
223 
225  bool ArgumentParser::IsNumber(int pos) const
226  {
227  return mArgParser->isNumber(pos);
228  }
229 
232  {
233  return mArgParser->containsOptions();
234  }
235 
237  void ArgumentParser::Remove(int pos, int num)
238  {
239  mArgParser->remove(pos, num);
240  }
241 
243  bool ArgumentParser::Match(int pos, const std::string& str) const
244  {
245  return mArgParser->match(pos, str);
246  }
247 
249  bool ArgumentParser::Read(const std::string& str)
250  {
251  return mArgParser->read(str);
252  }
253 
255  bool ArgumentParser::Read(const std::string& str, Parameter value1)
256  {
257  return mArgParser->read(str, value1);
258  }
259 
261  bool ArgumentParser::Read(const std::string& str, Parameter value1, Parameter value2)
262  {
263  return mArgParser->read(str, value1, value2);
264  }
265 
267  bool ArgumentParser::Read(const std::string& str, Parameter value1, Parameter value2, Parameter value3)
268  {
269  return mArgParser->read(str, value1, value2, value3);
270  }
271 
273  bool ArgumentParser::Read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4)
274  {
275  return mArgParser->read(str, value1, value2, value3, value4);
276  }
277 
279  bool ArgumentParser::Read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5)
280  {
281  return mArgParser->read(str, value1, value2, value3, value4, value5);
282  }
283 
285  bool ArgumentParser::Read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6)
286  {
287  return mArgParser->read(str, value1, value2, value3, value4, value5, value6);
288  }
289 
291  bool ArgumentParser::Read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7)
292  {
293  return mArgParser->read(str, value1, value2, value3, value4, value5, value6, value7);
294  }
295 
297  bool ArgumentParser::Read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7, Parameter value8)
298  {
299  return mArgParser->read(str, value1, value2, value3, value4, value5, value6, value7, value8);
300  }
301 
303  bool ArgumentParser::Read(int pos, const std::string& str)
304  {
305  return mArgParser->read(pos, str);
306  }
307 
309  bool ArgumentParser::Read(int pos, const std::string& str, Parameter value1)
310  {
311  return mArgParser->read(pos, str, value1);
312  }
313 
315  bool ArgumentParser::Read(int pos, const std::string& str, Parameter value1, Parameter value2)
316  {
317  return mArgParser->read(pos, str, value1, value2);
318  }
319 
321  bool ArgumentParser::Read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3)
322  {
323  return mArgParser->read(pos, str, value1, value2, value3);
324  }
325 
327  bool ArgumentParser::Read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4)
328  {
329  return mArgParser->read(pos, str, value1, value2, value3, value4);
330  }
331 
333  bool ArgumentParser::Read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5)
334  {
335  return mArgParser->read(pos, str, value1, value2, value3, value4, value5);
336  }
337 
339  bool ArgumentParser::Read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6)
340  {
341  return mArgParser->read(pos, str, value1, value2, value3, value4, value5, value6);
342  }
343 
345  bool ArgumentParser::Read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7)
346  {
347  return mArgParser->read(pos, str, value1, value2, value3, value4, value5, value6, value7);
348  }
349 
351  bool ArgumentParser::Read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7, Parameter value8)
352  {
353  return mArgParser->read(pos, str, value1, value2, value3, value4, value5, value6, value7, value8);
354  }
355 
358  {
359  return mArgParser->errors(static_cast<osg::ArgumentParser::ErrorSeverity>(severity));
360  }
361 
363  void ArgumentParser::ReportError(const std::string& message, ErrorSeverity severity)
364  {
365  mArgParser->reportError(message, static_cast<osg::ArgumentParser::ErrorSeverity>(severity));
366  }
367 
370  {
371  mArgParser->reportRemainingOptionsAsUnrecognized(static_cast<osg::ArgumentParser::ErrorSeverity>(severity));
372  }
373 
375  void ArgumentParser::WriteErrorMessages(std::ostream & output, ErrorSeverity severity)
376  {
377  mArgParser->writeErrorMessages(output, static_cast<osg::ArgumentParser::ErrorSeverity>(severity));
378  }
379 
382  {
383  osg::ApplicationUsage::Type ret = mArgParser->readHelpType();
384 
385  switch (ret)
386  {
387  case osg::ApplicationUsage::NO_HELP:
389  break;
390  case osg::ApplicationUsage::COMMAND_LINE_OPTION:
392  break;
393  case osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE:
395  break;
396  case osg::ApplicationUsage::KEYBOARD_MOUSE_BINDING:
398  break;
399  default:
401  }
402  }
403 
405  void ArgumentParser::SetApplicationName(const std::string& name)
406  {
407  mAppUsage->SetApplicationName(name);
408  }
409 
411  void ArgumentParser::SetDescription(const std::string& desc)
412  {
413  mAppUsage->SetDescription(desc);
414  }
415 
417  const std::string& ArgumentParser::GetDescription() const
418  {
419  return mAppUsage->GetDescription();
420  }
421 
423  void ArgumentParser::AddUsageExplanation(ApplicationUsage::Type type, const std::string& option, const std::string& explanation)
424  {
425  mAppUsage->AddUsageExplanation(type, option, explanation);
426  }
427 
429  void ArgumentParser::SetCommandLineUsage(const std::string& explanation)
430  {
431  mAppUsage->SetCommandLineUsage(explanation);
432  }
433 
435  const std::string & ArgumentParser::GetCommandLineUsage() const
436  {
437  return mAppUsage->GetCommandLineUsage();
438  }
439 
441  void ArgumentParser::AddCommandLineOption(const std::string& option, const std::string& explanation, const std::string& defaultValue)
442  {
443  mAppUsage->AddCommandLineOption(option, explanation, defaultValue);
444  }
445 
448  {
449  mAppUsage->SetCommandLineOptions(usageMap);
450  }
451 
454  {
455  return mAppUsage->GetCommandLineOptions();
456  }
457 
460  {
461  mAppUsage->SetCommandLineOptionsDefaults(usageMap);
462  }
463 
466  {
467  return mAppUsage->GetCommandLineOptionsDefaults();
468  }
469 
471  void ArgumentParser::AddEnvironmentalVariable(const std::string& option, const std::string& explanation, const std::string& defaultValue)
472  {
473  mAppUsage->AddEnvironmentalVariable(option, explanation, defaultValue);
474  }
475 
478  {
479  mAppUsage->SetEnvironmentalVariables(usageMap);
480  }
481 
484  {
485  return mAppUsage->GetEnvironmentalVariables();
486  }
487 
490  {
491  mAppUsage->SetEnvironmentalVariablesDefaults(usageMap);
492  }
493 
496  {
497  return mAppUsage->GetEnvironmentalVariablesDefaults();
498  }
499 
501  void ArgumentParser::AddKeyboardMouseBinding(const std::string& prefix, int key, const std::string& explanation)
502  {
503  mAppUsage->AddKeyboardMouseBinding(prefix, key, explanation);
504  }
505 
507  void ArgumentParser::AddKeyboardMouseBinding(int key, const std::string& explanation)
508  {
509  mAppUsage->AddKeyboardMouseBinding(key, explanation);
510  }
511 
513  void ArgumentParser::AddKeyboardMouseBinding(const std::string& option, const std::string& explanation)
514  {
515  mAppUsage->AddKeyboardMouseBinding(option, explanation);
516  }
517 
520  {
521  mAppUsage->SetKeyboardMouseBindings(usageMap);
522  }
523 
526  {
527  return mAppUsage->GetKeyboardMouseBindings();
528  }
529 
531  void ArgumentParser::GetFormattedString(std::string& str, const ApplicationUsage::UsageMap& usageMap, unsigned int widthOfOutput, bool showDefaults, const ApplicationUsage::UsageMap& usageMapDef)
532  {
533  mAppUsage->GetFormattedString(str, usageMap, widthOfOutput, showDefaults, usageMapDef);
534  }
535 
537  void ArgumentParser::Write(std::ostream& output, const ApplicationUsage::UsageMap& usageMap, unsigned int widthOfOutput, bool showDefaults, const ApplicationUsage::UsageMap& usageMapDef)
538  {
539  mAppUsage->Write(output, usageMap, widthOfOutput, showDefaults, usageMapDef);
540  }
541 
543  void ArgumentParser::Write(std::ostream& output, ApplicationUsage::Type type, unsigned int widthOfOutput, bool showDefaults)
544  {
545  mAppUsage->Write(output, type, widthOfOutput, showDefaults);
546  }
547 
549  void ArgumentParser::WriteEnvironmentSettings(std::ostream& output)
550  {
551  mAppUsage->WriteEnvironmentSettings(output);
552  }
553 }
ApplicationUsage::Type ReadHelpType()
This convenience method handles help requests on the command line.
void ReportError(const std::string &message, ErrorSeverity severity=ErrorSeverity::CRITICAL)
Report an error message by adding to the ErrorMessageMap.
void WriteErrorMessages(std::ostream &output, ErrorSeverity severity=ErrorSeverity::BENIGN)
Write error messages to the given ostream, if at or above the given severity.
void AddKeyboardMouseBinding(const std::string &prefix, int key, const std::string &explanation)
Adds a keyboard mouse binding usage.
const std::string & GetCommandLineUsage() const
Gets command line usage.
void GetFormattedString(std::string &str, const ApplicationUsage::UsageMap &usageMap, unsigned int widthOfOutput=80, bool showDefaults=false, const ApplicationUsage::UsageMap &usageMapDef=ApplicationUsage::UsageMap())
Gets formatted string.
void Write(std::ostream &output, const ApplicationUsage::UsageMap &usageMap, unsigned int widthOfOutput=80, bool showDefaults=false, const ApplicationUsage::UsageMap &usageMapDef=ApplicationUsage::UsageMap())
Writes out all the settings to the screen.
bool ContainsOptions() const
Query if this object contains options.
osg::ArgumentParser & GetOSGArgumentParser()
Gets internal osg argument parser.
ApplicationUsage * GetApplicationUsage()
Gets application usage.
const ApplicationUsage::UsageMap & GetCommandLineOptions() const
Gets command line options.
const ApplicationUsage::UsageMap & GetEnvironmentalVariablesDefaults() const
Gets environmental variables usage default usage map.
char ** GetArgv()
Return the argument array.
void AddCommandLineOption(const std::string &option, const std::string &explanation, const std::string &defaultValue="")
Adds a command line option.
const ApplicationUsage::UsageMap & GetCommandLineOptionsDefaults() const
Gets command line options defaults.
bool IsOption(const char *str) const
Return true if the specified string is an option in the form -option or –option. ...
bool IsBool(const char *str) const
Return true if specified parameter is a bool.
~ArgumentParser()
Destructor.
osg::ArgumentParser::Parameter * mParam
void SetCommandLineOptions(const ApplicationUsage::UsageMap &usageMap)
Sets command line options.
void AddEnvironmentalVariable(const std::string &option, const std::string &explanation, const std::string &defaultValue="")
Adds an environmental variable usage explanation.
void WriteEnvironmentSettings(std::ostream &output)
Writes out the environment settings.
bool Match(int pos, const std::string &str) const
Return true if the specified argument matches the given string.
void Remove(int pos, int num=1)
Remove one or more arguments from the argv argument list, and decrement the argc respectively.
void SetCommandLineOptionsDefaults(const ApplicationUsage::UsageMap &usageMap)
Sets command line options defaults.
bool IsString(const char *str) const
Return true if string is non-NULL and not an option in the form -option or –option.
bool IsNumber(const char *str) const
Return true if specified parameter is a number.
const std::string & GetDescription() const
Gets the description.
void SetEnvironmentalVariablesDefaults(const ApplicationUsage::UsageMap &usageMap)
Sets environmental variables usage default usage map.
std::unique_ptr< ApplicationUsage > mAppUsage
const ApplicationUsage::UsageMap & GetEnvironmentalVariables() const
Gets environmental variables usage.
An application usage class for the argument parser.
Parameter(bool &value)
Constructor.
void ReportRemainingOptionsAsUnrecognized(ErrorSeverity severity=ErrorSeverity::BENIGN)
For each remaining option, report it as unrecognized.
void AddUsageExplanation(ApplicationUsage::Type type, const std::string &option, const std::string &explanation)
Adds an usage explanation.
bool Errors(ErrorSeverity severity=ErrorSeverity::BENIGN) const
Return the error flag, true if an error has occurred when reading arguments.
void SetEnvironmentalVariables(const ApplicationUsage::UsageMap &usageMap)
Sets environmental variables usage.
std::string GetApplicationName() const
Return the application name, as specified by argv[0].
bool Assign(const char *str)
Assigns the given string value to the parameter.
std::map< std::string, std::string > UsageMap
The message to command usage map.
Namespace that holds various utility classes for the engine.
Definition: SmrtPtr.h:208
void SetDescription(const std::string &desc)
If non-empty, the Description is typically shown by the Help Handler as text on the Help display (whi...
Parameter & operator=(const Parameter &param)
Assignment operator.
void SetKeyboardMouseBindings(const ApplicationUsage::UsageMap &usageMap)
Sets keyboard mouse bindings usage.
void SetApplicationUsage(ApplicationUsage *usage)
Sets application usage.
void SetApplicationName(const std::string &name)
Sets the Applications Name.
int Find(const std::string &str) const
Return the position of an occurrence of a string in the argument list.
const ApplicationUsage::UsageMap & GetKeyboardMouseBindings() const
Gets keyboard mouse bindings usage.
bool Read(const std::string &str)
Search for an occurrence of a string in the argument list.
bool Valid(const char *str) const
Checks if this parameter is Valid.
std::unique_ptr< osg::ArgumentParser > mArgParser
The argument parser.
void SetCommandLineUsage(const std::string &explanation)
Sets command line usage.
ArgumentParser(int *argc, char **argv)
Default constructor that takes the argc and argv from the main function for command line parsing...
int & GetArgc()
Return the argument count.