TrueReality  v0.1.1912
ArgumentParser.h
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 #pragma once
23 
24 #include <trUtil/Export.h>
25 
27 #include <trUtil/ArgumentParser.h>
28 
29 #include <osg/ArgumentParser>
30 #include <osg/ref_ptr>
31 
32 #include <memory>
33 #include <map>
34 
40 namespace trUtil
41 {
48  {
49  public:
50 
51  enum class ErrorSeverity
52  {
53  BENIGN = osg::ArgumentParser::ErrorSeverity::BENIGN,
54  CRITICAL = osg::ArgumentParser::ErrorSeverity::CRITICAL
55  };
56 
57  using ErrorMessageMap = std::map<std::string, ErrorSeverity>;
58 
60  {
61  public:
62 
70  Parameter(bool& value);
71 
79  Parameter(float& value);
80 
88  Parameter(double& value);
89 
97  Parameter(int& value);
98 
106  Parameter(unsigned int& value);
107 
115  Parameter(std::string& value);
116 
124  Parameter(const Parameter& param);
125 
131  ~Parameter();
132 
142  Parameter& operator = (const Parameter& param);
143 
153  bool Valid(const char* str) const;
154 
164  bool Assign(const char* str);
165 
173  operator osg::ArgumentParser::Parameter() const;
174 
182  operator osg::ArgumentParser::Parameter& ();
183 
191  operator const osg::ArgumentParser::Parameter& () const;
192 
200  operator osg::ArgumentParser::Parameter* ();
201 
202 
203  protected:
204 
205  osg::ArgumentParser::Parameter* mParam;
206  };
207 
216  ArgumentParser(int* argc, char **argv);
217 
223  ~ArgumentParser();
224 
232  osg::ArgumentParser& GetOSGArgumentParser();
233 
241  const osg::ArgumentParser& GetOSGArgumentParser() const;
242 
253  bool IsOption(const char* str) const;
254 
265  bool IsString(const char* str) const;
266 
276  bool IsNumber(const char* str) const;
277 
287  bool IsBool(const char* str) const;
288 
296  void SetApplicationUsage(ApplicationUsage* usage);
297 
305  ApplicationUsage* GetApplicationUsage();
306 
314  const ApplicationUsage* GetApplicationUsage() const;
315 
323  int& GetArgc();
324 
332  char** GetArgv();
333 
341  std::string GetApplicationName() const;
342 
353  int Find(const std::string& str) const;
354 
365  bool IsOption(int pos) const;
366 
376  bool IsString(int pos) const;
377 
387  bool IsNumber(int pos) const;
388 
396  bool ContainsOptions() const;
397 
407  void Remove(int pos, int num = 1);
408 
419  bool Match(int pos, const std::string& str) const;
420 
431  bool Read(const std::string& str);
432 
444  bool Read(const std::string& str, Parameter value1);
445 
458  bool Read(const std::string& str, Parameter value1, Parameter value2);
459 
473  bool Read(const std::string& str, Parameter value1, Parameter value2, Parameter value3);
474 
489  bool Read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4);
490 
506  bool Read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5);
507 
524  bool Read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6);
525 
543  bool Read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7);
544 
563  bool Read(const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7, Parameter value8);
564 
577  bool Read(int pos, const std::string& str);
578 
592  bool Read(int pos, const std::string& str, Parameter value1);
593 
608  bool Read(int pos, const std::string& str, Parameter value1, Parameter value2);
609 
625  bool Read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3);
626 
643  bool Read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4);
644 
662  bool Read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5);
663 
682  bool Read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6);
683 
703  bool Read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7);
704 
725  bool Read(int pos, const std::string& str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7, Parameter value8);
726 
736  bool Errors(ErrorSeverity severity = ErrorSeverity::BENIGN) const;
737 
746  void ReportError(const std::string& message, ErrorSeverity severity = ErrorSeverity::CRITICAL);
747 
755  void ReportRemainingOptionsAsUnrecognized(ErrorSeverity severity = ErrorSeverity::BENIGN);
756 
765  void WriteErrorMessages(std::ostream& output, ErrorSeverity severity = ErrorSeverity::BENIGN);
766 
777  ApplicationUsage::Type ReadHelpType();
778 
786  void SetApplicationName(const std::string& name);
787 
796  void SetDescription(const std::string& desc);
797 
805  const std::string& GetDescription() const;
806 
816  void AddUsageExplanation(ApplicationUsage::Type type, const std::string& option, const std::string& explanation);
817 
825  void SetCommandLineUsage(const std::string& explanation);
826 
834  const std::string& GetCommandLineUsage() const;
835 
845  void AddCommandLineOption(const std::string& option, const std::string& explanation, const std::string &defaultValue = "");
846 
854  void SetCommandLineOptions(const ApplicationUsage::UsageMap& usageMap);
855 
863  const ApplicationUsage::UsageMap& GetCommandLineOptions() const;
864 
872  void SetCommandLineOptionsDefaults(const ApplicationUsage::UsageMap& usageMap);
873 
881  const ApplicationUsage::UsageMap& GetCommandLineOptionsDefaults() const;
882 
892  void AddEnvironmentalVariable(const std::string& option, const std::string& explanation, const std::string& defaultValue = "");
893 
901  void SetEnvironmentalVariables(const ApplicationUsage::UsageMap& usageMap);
902 
910  const ApplicationUsage::UsageMap& GetEnvironmentalVariables() const;
911 
919  void SetEnvironmentalVariablesDefaults(const ApplicationUsage::UsageMap& usageMap);
920 
928  const ApplicationUsage::UsageMap& GetEnvironmentalVariablesDefaults() const;
929 
939  void AddKeyboardMouseBinding(const std::string& prefix, int key, const std::string& explanation);
940 
949  void AddKeyboardMouseBinding(int key, const std::string& explanation);
950 
959  void AddKeyboardMouseBinding(const std::string& option, const std::string& explanation);
960 
968  void SetKeyboardMouseBindings(const ApplicationUsage::UsageMap& usageMap);
969 
977  const ApplicationUsage::UsageMap& GetKeyboardMouseBindings() const;
978 
990  void GetFormattedString(std::string& str, const ApplicationUsage::UsageMap& usageMap, unsigned int widthOfOutput = 80, bool showDefaults = false, const ApplicationUsage::UsageMap& usageMapDef = ApplicationUsage::UsageMap());
991 
1003  void Write(std::ostream& output, const ApplicationUsage::UsageMap& usageMap, unsigned int widthOfOutput = 80, bool showDefaults = false, const ApplicationUsage::UsageMap& usageMapDef = ApplicationUsage::UsageMap());
1004 
1015  void Write(std::ostream& output, ApplicationUsage::Type type = ApplicationUsage::Type::COMMAND_LINE_OPTION, unsigned int widthOfOutput = 80, bool showDefaults = false);
1016 
1024  void WriteEnvironmentSettings(std::ostream& output);
1025 
1026  protected:
1027 
1029  std::unique_ptr<osg::ArgumentParser> mArgParser;
1030  std::unique_ptr<ApplicationUsage> mAppUsage;
1031  };
1032 }
bool TR_UTIL_EXPORT Match(const char *wildCards, const char *str)
Matches.
osg::ArgumentParser::Parameter * mParam
A a command line argument parser class that helps to create, manage, and pass commands to other syste...
std::map< std::string, ErrorSeverity > ErrorMessageMap
std::unique_ptr< ApplicationUsage > mAppUsage
An application usage class for the argument parser.
N Remove(N number, B bits)
Remove the "bits" from "number".
Definition: Bits.h:101
A class that represents date time utility.
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
std::unique_ptr< osg::ArgumentParser > mArgParser
The argument parser.