SuperTuxKart
command_line.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2011-2015 Joerg Henrichs
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 3
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 
20 #ifndef HEADER_COMMAND_LINE_HPP
21 #define HEADER_COMMAND_LINE_HPP
22 
23 #include "utils/string_utils.hpp"
24 
25 #include <string>
26 #include <vector>
27 
46 {
47 private:
49  static std::vector<std::string> m_argv;
50 
52  static std::string m_exec_name;
53 
54  // ------------------------------------------------------------------------
63  static bool has(const std::string &option, void *t, const char* const format)
64  {
65  if(m_argv.size()==0) return false;
66 
67  std::string equal=option+"="+format;
68  std::vector<std::string>::iterator i;
69  for(i=m_argv.begin(); i<m_argv.end(); i++)
70  {
71  if(sscanf(i->c_str(), equal.c_str(), t)==1)
72  {
73  m_argv.erase(i);
74  return true;
75  }
76  }
77  return false;
78  } // has
79 
80 public:
81  static void init(unsigned int argc, char *argv[]);
82  static void addArgsFromUserConfig();
83  static void reportInvalidParameters();
84  static bool has(const std::string &option);
85  // ------------------------------------------------------------------------
93  template<typename T> static bool has(const std::string& option, T* value)
94  {
95  std::string equal = option + "=";
96  std::vector<std::string>::iterator i;
97  for (i = m_argv.begin(); i < m_argv.end(); i++)
98  {
99  if (i->compare(0, equal.size(), equal) == 0)
100  {
101  std::string result =
102  i->substr(equal.size(), std::string::npos);
103  if (StringUtils::fromString(result, *value))
104  {
105  m_argv.erase(i);
106  return true;
107  }
108  }
109  }
110  return false;
111  }
112  // ------------------------------------------------------------------------
121  static bool has(const std::string& option, std::string* value)
122  {
123  std::string equal = option + "=";
124  std::vector<std::string>::iterator i;
125  for (i = m_argv.begin(); i < m_argv.end(); i++)
126  {
127  if (i->compare(0, equal.size(), equal) == 0)
128  {
129  *value = i->substr(equal.size(), std::string::npos);
130  m_argv.erase(i);
131  return true;
132  }
133  }
134  return false;
135  } // has<std::string>
136  // ------------------------------------------------------------------------
138  static const std::string& getExecName() { return m_exec_name; }
139 }; // CommandLine
140 #endif
A small class to manage the 'argv' parameters of a program.
Definition: command_line.hpp:46
static void init(unsigned int argc, char *argv[])
The constructor takes the standard C arguments argc and argv and stores the information internally.
Definition: command_line.cpp:37
static bool has(const std::string &option, T *value)
Searches for an option 'option=XX'.
Definition: command_line.hpp:93
static bool has(const std::string &option, void *t, const char *const format)
Searches for an option 'option=XX'.
Definition: command_line.hpp:63
static const std::string & getExecName()
Returns the name of the executable.
Definition: command_line.hpp:138
static std::vector< std::string > m_argv
The array with all command line options.
Definition: command_line.hpp:49
static void reportInvalidParameters()
Reports any parameters that have not been handled yet to be an error.
Definition: command_line.cpp:86
static bool has(const std::string &option, std::string *value)
Searches for an option 'option=XX'.
Definition: command_line.hpp:121
static std::string m_exec_name
Name of the executable.
Definition: command_line.hpp:52