Project Malmo  0.37.0
ArgumentParser.h
1 // --------------------------------------------------------------------------------------------------
2 // Copyright (c) 2016 Microsoft Corporation
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5 // associated documentation files (the "Software"), to deal in the Software without restriction,
6 // including without limitation the rights to use, copy, modify, merge, publish, distribute,
7 // sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in all copies or
11 // substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14 // NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 // --------------------------------------------------------------------------------------------------
19 
20 #ifndef _ARGUMENTPARSER_H_
21 #define _ARGUMENTPARSER_H_
22 
23 // Boost:
24 #include <boost/program_options.hpp>
25 
26 // STL:
27 #include <string>
28 #include <vector>
29 
30 namespace malmo
31 {
34  {
35  public:
36 
39  ArgumentParser(const std::string& title);
40 
42 
46  void parseArgs(int argc, const char** argv);
47 
52  void parse(const std::vector< std::string >& args);
53 
58  void addOptionalIntArgument(const std::string& name, const std::string& description, int defaultValue);
59 
64  void addOptionalFloatArgument(const std::string& name, const std::string& description, double defaultValue);
65 
70  void addOptionalStringArgument(const std::string& name, const std::string& description, const std::string& defaultValue);
71 
75  void addOptionalFlag(const std::string& name, const std::string& description);
76 
79  std::string getUsage() const;
80 
84  bool receivedArgument(const std::string& name) const;
85 
89  int getIntArgument(const std::string& name) const;
90 
94  double getFloatArgument(const std::string& name) const;
95 
99  std::string getStringArgument(const std::string& name) const;
100 
101  private:
102 
103  boost::program_options::options_description spec; // the specifications of the options we expect
104  boost::program_options::variables_map opts; // the names and values of the options we received
105  };
106 }
107 
108 #endif
A general purpose command-line argument parser.
Definition: ArgumentParser.h:34
void addOptionalFlag(const std::string &name, const std::string &description)
Specify a boolean flag that can be given on the command line.
std::string getUsage() const
Gets a string that describes the current set of options we expect.
void addOptionalFloatArgument(const std::string &name, const std::string &description, double defaultValue)
Specify a floating-point argument that can be given on the command line.
void addOptionalStringArgument(const std::string &name, const std::string &description, const std::string &defaultValue)
Specify a string argument that can be given on the command line.
void addOptionalIntArgument(const std::string &name, const std::string &description, int defaultValue)
Specify an integer argument that can be given on the command line.
double getFloatArgument(const std::string &name) const
Retrieves the value of a named floating-point argument.
std::string getStringArgument(const std::string &name) const
Retrieves the value of a named string argument.
int getIntArgument(const std::string &name) const
Retrieves the value of a named integer argument.
ArgumentParser(const std::string &title)
Construct an argument parser.
void parseArgs(int argc, const char **argv)
Parses a list of strings given in the C style. Throws std::exception if parsing fails.
bool receivedArgument(const std::string &name) const
Gets whether a named argument was parsed on the command-line arguments.
void parse(const std::vector< std::string > &args)
Parses a list of strings.