1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
27 // File: vogl_command_line_params.h
30 #include "vogl_core.h"
32 // TODO: Get rid of std::multimap! vogl::map can replace this.
37 // Returns an array of command line parameters - uses platform specific methods to retrieve them.
38 dynamic_string_array get_command_line_params();
40 // Returns an array of command line parameters given main()'s argc/argv.
41 dynamic_string_array get_command_line_params(int argc, char *argv[]);
43 // Returns the command line passed to the app as a single string.
44 // On systems where this isn't trivial, this function combines together the separate arguments, quoting and adding spaces as needed.
45 dynamic_string get_command_line();
47 // Returns the command line passed to the app as a single string.
48 // On systems where this isn't trivial, this function combines together the separate arguments, quoting and adding spaces as needed.
49 dynamic_string get_command_line(int argc, char *argv[]);
51 // Returns true if the specified command line parameter was specified to the app.
52 bool check_for_command_line_param(const char *pParam);
54 // Splits a string containing the app's command line parameters into an array of seperate parameters.
55 bool split_command_line_params(const char *p, dynamic_string_array ¶ms);
57 // TODO: Add help text, and a function to print a tool's valid arguments.
58 struct command_line_param_desc
62 bool m_support_listing_file;
66 void dump_command_line_info(uint n, const command_line_param_desc *pDesc, const char *prefix = NULL);
68 // I'm pretty torn about this class's design. I flip flop between hating it to tolerating it on every update.
69 class command_line_params
75 : m_split_param_index(0), m_modifier(0)
79 dynamic_string_array m_values;
80 uint m_split_param_index;
84 // TODO: Get rid of std::multimap!
85 typedef std::multimap<dynamic_string, param_value> param_map;
86 typedef param_map::const_iterator param_map_const_iterator;
87 typedef param_map::iterator param_map_iterator;
89 command_line_params();
96 : m_skip_first_param(true),
97 m_single_minus_params(true),
98 m_double_minus_params(false),
99 m_ignore_non_params(false),
100 m_ignore_unrecognized_params(false),
101 m_fail_on_non_params(false),
102 m_pParam_ignore_prefix(NULL),
103 m_pParam_accept_prefix(NULL)
107 bool m_skip_first_param;
108 bool m_single_minus_params;
109 bool m_double_minus_params;
110 bool m_ignore_non_params;
111 bool m_ignore_unrecognized_params;
112 bool m_fail_on_non_params; // if true, parsing fails if an non-option argument is specified, excluding the first argument which is the program's path
113 const char *m_pParam_ignore_prefix;
114 const char *m_pParam_accept_prefix;
117 bool parse(const dynamic_string_array ¶ms, uint total_param_descs, const command_line_param_desc *pParam_desc, const parse_config &config);
118 bool parse(const char *pCmd_line, uint total_param_descs, const command_line_param_desc *pParam_desc, const parse_config &config);
120 const dynamic_string_array &get_split_param_array() const
124 bool is_split_param_an_option(uint split_param_array_index) const;
126 const param_map &get_param_map() const
131 uint get_num_params() const
133 return static_cast<uint>(m_param_map.size());
136 param_map_const_iterator begin() const
138 return m_param_map.begin();
140 param_map_const_iterator end() const
142 return m_param_map.end();
145 bool find(const char *pKey, param_map_const_iterator &begin, param_map_const_iterator &end) const;
147 // Returns the # of command line params matching key (use "" key string for regular/non-option params)
148 uint get_count(const char *pKey) const;
150 // Returns end() if param cannot be found, or index is out of range.
151 param_map_const_iterator get_param(const char *pKey, uint key_index) const;
153 bool has_key(const char *pKey) const
155 return get_param(pKey, 0) != end();
158 bool has_value(const char *pKey, uint key_index) const;
159 uint get_num_values(const char *pKey, uint key_index) const;
161 bool get_value_as_bool(const char *pKey, uint key_index = 0, bool def = false) const;
163 // *pSuccess will be set to false if the value can't parse, or if the key isn't found, otherwise true (even if the value had to be clamped to the specified range)
164 int get_value_as_int(const char *pKey, uint key_index = 0, int def = 0, int l = cINT32_MIN, int h = cINT32_MAX, uint value_index = 0, bool *pSuccess = NULL) const;
165 int64_t get_value_as_int64(const char *pKey, uint key_index = 0, int64_t def = 0, int64_t l = cINT64_MIN, int64_t h = cINT64_MAX, uint value_index = 0, bool *pSuccess = NULL) const;
166 uint get_value_as_uint(const char *pKey, uint key_index = 0, uint def = 0, uint l = 0, uint h = cUINT32_MAX, uint value_index = 0, bool *pSuccess = NULL) const;
167 uint64_t get_value_as_uint64(const char *pKey, uint key_index = 0, uint64_t def = 0, uint64_t l = 0, uint64_t h = cUINT64_MAX, uint value_index = 0, bool *pSuccess = NULL) const;
168 float get_value_as_float(const char *pKey, uint key_index = 0, float def = 0.0f, float l = -math::cNearlyInfinite, float h = math::cNearlyInfinite, uint value_index = 0, bool *pSuccess = NULL) const;
170 bool get_value_as_string(dynamic_string &value, const char *pKey, uint key_index = 0, const char *pDef = "", uint value_index = 0) const;
171 dynamic_string get_value_as_string(const char *pKey, uint key_index = 0, const char *pDef = "", uint value_index = 0) const;
172 const dynamic_string &get_value_as_string_or_empty(const char *pKey, uint key_index = 0, uint value_index = 0) const;
175 dynamic_string_array m_params;
177 param_map m_param_map;
179 static bool load_string_file(const char *pFilename, dynamic_string_array &strings);
182 extern command_line_params g_command_line_params;