]> git.cworth.org Git - vogl/blob - src/voglcore/vogl_command_line_params.h
Initial vogl checkin
[vogl] / src / voglcore / vogl_command_line_params.h
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 // File: vogl_command_line_params.h
28 #pragma once
29
30 #include "vogl_core.h"
31
32 // TODO: Get rid of std::multimap! vogl::map can replace this.
33 #include <map>
34
35 namespace vogl
36 {
37     // Returns an array of command line parameters - uses platform specific methods to retrieve them.
38     dynamic_string_array get_command_line_params();
39
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[]);
42
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();
46
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[]);
50
51     // Returns true if the specified command line parameter was specified to the app.
52     bool check_for_command_line_param(const char *pParam);
53
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 &params);
56
57     // TODO: Add help text, and a function to print a tool's valid arguments.
58     struct command_line_param_desc
59     {
60         const char *m_pName;
61         uint m_num_values;
62         bool m_support_listing_file;
63         const char *m_pDesc;
64     };
65
66     void dump_command_line_info(uint n, const command_line_param_desc *pDesc, const char *prefix = NULL);
67
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
70     {
71     public:
72         struct param_value
73         {
74             inline param_value()
75                 : m_split_param_index(0), m_modifier(0)
76             {
77             }
78
79             dynamic_string_array m_values;
80             uint m_split_param_index;
81             int8 m_modifier;
82         };
83
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;
88
89         command_line_params();
90
91         void clear();
92
93         struct parse_config
94         {
95             parse_config()
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)
104             {
105             }
106
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;
115         };
116
117         bool parse(const dynamic_string_array &params, 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);
119
120         const dynamic_string_array &get_split_param_array() const
121         {
122             return m_params;
123         }
124         bool is_split_param_an_option(uint split_param_array_index) const;
125
126         const param_map &get_param_map() const
127         {
128             return m_param_map;
129         }
130
131         uint get_num_params() const
132         {
133             return static_cast<uint>(m_param_map.size());
134         }
135
136         param_map_const_iterator begin() const
137         {
138             return m_param_map.begin();
139         }
140         param_map_const_iterator end() const
141         {
142             return m_param_map.end();
143         }
144
145         bool find(const char *pKey, param_map_const_iterator &begin, param_map_const_iterator &end) const;
146
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;
149
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;
152
153         bool has_key(const char *pKey) const
154         {
155             return get_param(pKey, 0) != end();
156         }
157
158         bool has_value(const char *pKey, uint key_index) const;
159         uint get_num_values(const char *pKey, uint key_index) const;
160
161         bool get_value_as_bool(const char *pKey, uint key_index = 0, bool def = false) const;
162
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;
169
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;
173
174     private:
175         dynamic_string_array m_params;
176
177         param_map m_param_map;
178
179         static bool load_string_file(const char *pFilename, dynamic_string_array &strings);
180     };
181
182     extern command_line_params g_command_line_params;
183
184 } // namespace vogl