OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This class works with command lines: building and parsing. | 5 // This class works with command lines: building and parsing. |
6 // Arguments with prefixes ('--', '-', and on Windows, '/') are switches. | 6 // Arguments with prefixes ('--', '-', and on Windows, '/') are switches. |
7 // Switches will precede all other arguments without switch prefixes. | 7 // Switches will precede all other arguments without switch prefixes. |
8 // Switches can optionally have values, delimited by '=', e.g., "-switch=value". | 8 // Switches can optionally have values, delimited by '=', e.g., "-switch=value". |
9 // An argument of "--" will terminate switch parsing during initialization, | 9 // An argument of "--" will terminate switch parsing during initialization, |
10 // interpreting subsequent tokens as non-switch arguments, regardless of prefix. | 10 // interpreting subsequent tokens as non-switch arguments, regardless of prefix. |
11 | 11 |
12 // There is a singleton read-only CommandLine that represents the command line | 12 // There is a singleton read-only CommandLine that represents the command line |
13 // that the current process was started with. It must be initialized in main(). | 13 // that the current process was started with. It must be initialized in main(). |
14 | 14 |
15 #ifndef BASE_COMMAND_LINE_H_ | 15 #ifndef BASE_COMMAND_LINE_H_ |
16 #define BASE_COMMAND_LINE_H_ | 16 #define BASE_COMMAND_LINE_H_ |
17 | 17 |
18 #include <stddef.h> | 18 #include <stddef.h> |
19 #include <map> | 19 #include <map> |
20 #include <string> | 20 #include <string> |
21 #include <vector> | 21 #include <vector> |
22 | 22 |
23 #include "base/base_export.h" | 23 #include "base/base_export.h" |
24 #include "base/strings/string16.h" | 24 #include "base/strings/string16.h" |
| 25 #include "base/strings/string_piece.h" |
25 #include "build/build_config.h" | 26 #include "build/build_config.h" |
26 | 27 |
27 namespace base { | 28 namespace base { |
28 | 29 |
29 class FilePath; | 30 class FilePath; |
30 | 31 |
31 class BASE_EXPORT CommandLine { | 32 class BASE_EXPORT CommandLine { |
32 public: | 33 public: |
33 #if defined(OS_WIN) | 34 #if defined(OS_WIN) |
34 // The native command line string type. | 35 // The native command line string type. |
35 typedef base::string16 StringType; | 36 typedef base::string16 StringType; |
36 #elif defined(OS_POSIX) | 37 #elif defined(OS_POSIX) |
37 typedef std::string StringType; | 38 typedef std::string StringType; |
38 #endif | 39 #endif |
39 | 40 |
40 typedef StringType::value_type CharType; | 41 typedef StringType::value_type CharType; |
41 typedef std::vector<StringType> StringVector; | 42 typedef std::vector<StringType> StringVector; |
42 typedef std::map<std::string, StringType> SwitchMap; | 43 typedef std::map<std::string, StringType> SwitchMap; |
| 44 typedef std::map<base::StringPiece, const StringType*> StringPieceSwitchMap; |
43 | 45 |
44 // A constructor for CommandLines that only carry switches and arguments. | 46 // A constructor for CommandLines that only carry switches and arguments. |
45 enum NoProgram { NO_PROGRAM }; | 47 enum NoProgram { NO_PROGRAM }; |
46 explicit CommandLine(NoProgram no_program); | 48 explicit CommandLine(NoProgram no_program); |
47 | 49 |
48 // Construct a new command line with |program| as argv[0]. | 50 // Construct a new command line with |program| as argv[0]. |
49 explicit CommandLine(const FilePath& program); | 51 explicit CommandLine(const FilePath& program); |
50 | 52 |
51 // Construct a new command line from an argument list. | 53 // Construct a new command line from an argument list. |
52 CommandLine(int argc, const CharType* const* argv); | 54 CommandLine(int argc, const CharType* const* argv); |
53 explicit CommandLine(const StringVector& argv); | 55 explicit CommandLine(const StringVector& argv); |
54 | 56 |
| 57 // Override copy and assign to ensure |switches_by_stringpiece_| is valid. |
| 58 CommandLine(const CommandLine& other); |
| 59 CommandLine& operator=(const CommandLine& other); |
| 60 |
55 ~CommandLine(); | 61 ~CommandLine(); |
56 | 62 |
57 #if defined(OS_WIN) | 63 #if defined(OS_WIN) |
58 // By default this class will treat command-line arguments beginning with | 64 // By default this class will treat command-line arguments beginning with |
59 // slashes as switches on Windows, but not other platforms. | 65 // slashes as switches on Windows, but not other platforms. |
60 // | 66 // |
61 // If this behavior is inappropriate for your application, you can call this | 67 // If this behavior is inappropriate for your application, you can call this |
62 // function BEFORE initializing the current process' global command line | 68 // function BEFORE initializing the current process' global command line |
63 // object and the behavior will be the same as Posix systems (only hyphens | 69 // object and the behavior will be the same as Posix systems (only hyphens |
64 // begin switches, everything else will be an arg). | 70 // begin switches, everything else will be an arg). |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 #endif | 141 #endif |
136 | 142 |
137 // Returns the original command line string as a vector of strings. | 143 // Returns the original command line string as a vector of strings. |
138 const StringVector& argv() const { return argv_; } | 144 const StringVector& argv() const { return argv_; } |
139 | 145 |
140 // Get and Set the program part of the command line string (the first item). | 146 // Get and Set the program part of the command line string (the first item). |
141 FilePath GetProgram() const; | 147 FilePath GetProgram() const; |
142 void SetProgram(const FilePath& program); | 148 void SetProgram(const FilePath& program); |
143 | 149 |
144 // Returns true if this command line contains the given switch. | 150 // Returns true if this command line contains the given switch. |
145 // Switch names should only be lowercase. | 151 // Switch names must be lowercase. |
146 // The second override provides an optimized version to avoid inlining the | 152 // The second override provides an optimized version to avoid inlining codegen |
147 // codegen for the string allocation. | 153 // at every callsite to find the length of the constant and construct a |
148 bool HasSwitch(const std::string& switch_string) const; | 154 // StringPiece. |
| 155 bool HasSwitch(const base::StringPiece& switch_string) const; |
149 bool HasSwitch(const char switch_constant[]) const; | 156 bool HasSwitch(const char switch_constant[]) const; |
150 | 157 |
151 // Returns the value associated with the given switch. If the switch has no | 158 // Returns the value associated with the given switch. If the switch has no |
152 // value or isn't present, this method returns the empty string. | 159 // value or isn't present, this method returns the empty string. |
153 std::string GetSwitchValueASCII(const std::string& switch_string) const; | 160 // Switch names must be lowercase. |
154 FilePath GetSwitchValuePath(const std::string& switch_string) const; | 161 std::string GetSwitchValueASCII(const base::StringPiece& switch_string) const; |
155 StringType GetSwitchValueNative(const std::string& switch_string) const; | 162 FilePath GetSwitchValuePath(const base::StringPiece& switch_string) const; |
| 163 StringType GetSwitchValueNative(const base::StringPiece& switch_string) const; |
156 | 164 |
157 // Get a copy of all switches, along with their values. | 165 // Get a copy of all switches, along with their values. |
158 const SwitchMap& GetSwitches() const { return switches_; } | 166 const SwitchMap& GetSwitches() const { return switches_; } |
159 | 167 |
160 // Append a switch [with optional value] to the command line. | 168 // Append a switch [with optional value] to the command line. |
161 // Note: Switches will precede arguments regardless of appending order. | 169 // Note: Switches will precede arguments regardless of appending order. |
162 void AppendSwitch(const std::string& switch_string); | 170 void AppendSwitch(const std::string& switch_string); |
163 void AppendSwitchPath(const std::string& switch_string, | 171 void AppendSwitchPath(const std::string& switch_string, |
164 const FilePath& path); | 172 const FilePath& path); |
165 void AppendSwitchNative(const std::string& switch_string, | 173 void AppendSwitchNative(const std::string& switch_string, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 // cl.AppendSwitch(...); | 215 // cl.AppendSwitch(...); |
208 | 216 |
209 // Internal version of GetCommandLineString. If |quote_placeholders| is true, | 217 // Internal version of GetCommandLineString. If |quote_placeholders| is true, |
210 // also quotes parts with '%' in them. | 218 // also quotes parts with '%' in them. |
211 StringType GetCommandLineStringInternal(bool quote_placeholders) const; | 219 StringType GetCommandLineStringInternal(bool quote_placeholders) const; |
212 | 220 |
213 // Internal version of GetArgumentsString. If |quote_placeholders| is true, | 221 // Internal version of GetArgumentsString. If |quote_placeholders| is true, |
214 // also quotes parts with '%' in them. | 222 // also quotes parts with '%' in them. |
215 StringType GetArgumentsStringInternal(bool quote_placeholders) const; | 223 StringType GetArgumentsStringInternal(bool quote_placeholders) const; |
216 | 224 |
| 225 // Reconstruct |switches_by_stringpiece| to be a mirror of |switches|. |
| 226 // |switches_by_stringpiece| only contains pointers to objects owned by |
| 227 // |switches|. |
| 228 void ResetStringPieces(); |
| 229 |
217 // The singleton CommandLine representing the current process's command line. | 230 // The singleton CommandLine representing the current process's command line. |
218 static CommandLine* current_process_commandline_; | 231 static CommandLine* current_process_commandline_; |
219 | 232 |
220 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* } | 233 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* } |
221 StringVector argv_; | 234 StringVector argv_; |
222 | 235 |
223 // Parsed-out switch keys and values. | 236 // Parsed-out switch keys and values. |
224 SwitchMap switches_; | 237 SwitchMap switches_; |
225 | 238 |
| 239 // A mirror of |switches_| with only references to the actual strings. |
| 240 // The StringPiece internally holds a pointer to a key in |switches_| while |
| 241 // the mapped_type points to a value in |switches_|. |
| 242 // Used for allocation-free lookups. |
| 243 StringPieceSwitchMap switches_by_stringpiece_; |
| 244 |
226 // The index after the program and switches, any arguments start here. | 245 // The index after the program and switches, any arguments start here. |
227 size_t begin_args_; | 246 size_t begin_args_; |
228 }; | 247 }; |
229 | 248 |
230 } // namespace base | 249 } // namespace base |
231 | 250 |
232 #endif // BASE_COMMAND_LINE_H_ | 251 #endif // BASE_COMMAND_LINE_H_ |
OLD | NEW |