|
1 /* |
|
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #ifndef __COMMAND_LINE_H__ |
|
20 #define __COMMAND_LINE_H__ |
|
21 #include "CmdGlobals.h" |
|
22 #ifdef __WIN__ |
|
23 #pragma warning(disable:4786) |
|
24 #endif |
|
25 #include <string> |
|
26 #include <map> |
|
27 #include <set> |
|
28 |
|
29 using namespace std; |
|
30 typedef pair<string, string> mapentry; |
|
31 |
|
32 /** |
|
33 * The class CommandLine handles the command line parsing and represents |
|
34 * the options for the program in an easy way. |
|
35 */ |
|
36 class CommandLine { |
|
37 |
|
38 // constructors & destructors |
|
39 public: |
|
40 /** |
|
41 * Constructor |
|
42 */ |
|
43 CommandLine(); |
|
44 |
|
45 /** |
|
46 * Constructor |
|
47 * @param args commanline argument as pointer to string |
|
48 * @param argc argument count |
|
49 */ |
|
50 CommandLine(char** args, int argc); |
|
51 |
|
52 /** |
|
53 * Destructor |
|
54 */ |
|
55 ~CommandLine(); |
|
56 |
|
57 // public methods |
|
58 public: |
|
59 /** |
|
60 * Returns the parameter value for a given parameter. |
|
61 * |
|
62 * @param parm parameter to get |
|
63 * @return parameter value |
|
64 * @exception throw an exception if illegal parameter is requested. |
|
65 */ |
|
66 string getParameter(string parm); |
|
67 |
|
68 /** |
|
69 * Get parameters |
|
70 * @return map of parameters |
|
71 */ |
|
72 const map <string, string>& getParameters(); |
|
73 |
|
74 /** |
|
75 * Check if the asked parameter is given |
|
76 * @param parm parameter name |
|
77 * @return true if the asked parameter is given |
|
78 */ |
|
79 bool parameterExists(const string& parm); |
|
80 |
|
81 /** |
|
82 * Validate parameters. Returns a string containing mismatching/insufficiently |
|
83 * specified parameters if there are any mismatches. Otherwise returns a string |
|
84 * of zero length. |
|
85 * @return error string consisting of invalid parameters. |
|
86 */ |
|
87 string validateParameters(); |
|
88 |
|
89 /** |
|
90 * Check if the given parameter value is valid |
|
91 * @param parm parameter to check |
|
92 * @param val parameter value to check |
|
93 */ |
|
94 void validParamValue(string parm, string val); |
|
95 |
|
96 // private methods |
|
97 private: |
|
98 /** |
|
99 * Inserts a parameter into parameter validation lists. |
|
100 * This is *NOT* the same as storeParameter, which stores |
|
101 * the actual parameter value of a command line parm. |
|
102 * |
|
103 * Parameter to be inserted can require a specifier/no |
|
104 * specifier, and can be optional/mandatory |
|
105 * |
|
106 * @param parmname name of the paramter to insert |
|
107 * @param specifierRequired if required, this true |
|
108 * @param optional true if parameter is optional |
|
109 */ |
|
110 void insertParameter(string parmname, bool specifierRequired, bool optional = true); |
|
111 |
|
112 /** |
|
113 * Initialize acceptable parameters list |
|
114 * |
|
115 * Define which parameters are valid and which additionally require a string |
|
116 * after them (dir etc), and which are mandatory (ie. modal, ie. non-optional) |
|
117 * Also assign the initial values for epocroot directories. |
|
118 * |
|
119 * See documentation for exact meaning of each parameter. |
|
120 */ |
|
121 void initializeAcceptableParametersList(); |
|
122 |
|
123 /** |
|
124 * Stores a parameter. Actual functionality depends on parameter type |
|
125 * (was it a command-line parameter [overrides environmental variables and |
|
126 * command file variables], a command-file parameter [overrides environmental |
|
127 * variables] or an environmental variable) |
|
128 * In other words, it's not possible for a command-file parameter or |
|
129 * an environment variable to replace any parameter that has been |
|
130 * specified on command-line; respectively, command-line argument |
|
131 * will always override any other type of argument, no matter where it |
|
132 * was earlier defined. |
|
133 * |
|
134 * Throws an exception if no specifier is given to a parameter that |
|
135 * requires one (e.g. -baselinedir requires a specifier [directory], |
|
136 * whereas -recursive doesn't) |
|
137 * |
|
138 * @param parm parameter to store |
|
139 * @param val parameter value to store |
|
140 * @param parmtype commandline or commandfile |
|
141 * @exception an exeption is thrown if no specifier is given to a parameter |
|
142 * that requires one (e.g. -baselinedir requires a specifier [directory], |
|
143 * whereas -recursive doesn't) |
|
144 */ |
|
145 void storeParameter(string parm, string val, int parmtype = EParmCommandLine); |
|
146 |
|
147 /** |
|
148 * Parses the command line parameters and stores them to public instance |
|
149 * variables. |
|
150 * @param params list of params |
|
151 * @param count count params |
|
152 * @param parmtype defaulttype=EParmCommandLine |
|
153 */ |
|
154 void parse(char** params, size_t count, int parmtype = EParmCommandLine); |
|
155 |
|
156 /** |
|
157 * Show commandline options and exit programe |
|
158 * Not enough options are given, so options list is shown and |
|
159 * the program is exited |
|
160 */ |
|
161 void showCommandLineOptionsAndExit(); |
|
162 |
|
163 // Private instance variables |
|
164 private: |
|
165 //! All the parameters that the program accepts |
|
166 map <string, string> iAcceptableParameterMap; |
|
167 |
|
168 //! Map of parameters |
|
169 map <string, string> iParameterMap; |
|
170 |
|
171 //! These command line parameters require option |
|
172 set <string> parameterSpecifierSet; |
|
173 |
|
174 //! These command line parameters are required |
|
175 set <string> requiredParametersSet; |
|
176 |
|
177 //! True if the parameters are valid |
|
178 int iParametersValid; |
|
179 |
|
180 //! Command line arguments |
|
181 char** iArgList; |
|
182 |
|
183 //! Count of command line arguments |
|
184 int iArgCount; |
|
185 }; |
|
186 |
|
187 #endif |