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 the |
147 // codegen for the string allocation. | 153 // codegen to find the length of the constant and construct a StringPiece. |
148 bool HasSwitch(const std::string& switch_string) const; | 154 bool HasSwitch(const base::StringPiece& switch_string) const; |
149 bool HasSwitch(const char switch_constant[]) const; | 155 bool HasSwitch(const char switch_constant[]) const; |
brettw
2015/04/24 19:50:50
I think this second version is no longer necessary
jackhou1
2015/05/11 13:51:42
IIUC having the second version saves an appreciabl
| |
150 | 156 |
151 // Returns the value associated with the given switch. If the switch has no | 157 // 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. | 158 // value or isn't present, this method returns the empty string. |
153 std::string GetSwitchValueASCII(const std::string& switch_string) const; | 159 // Switch names must be lowercase. |
154 FilePath GetSwitchValuePath(const std::string& switch_string) const; | 160 std::string GetSwitchValueASCII(const base::StringPiece& switch_string) const; |
155 StringType GetSwitchValueNative(const std::string& switch_string) const; | 161 FilePath GetSwitchValuePath(const base::StringPiece& switch_string) const; |
162 StringType GetSwitchValueNative(const base::StringPiece& switch_string) const; | |
156 | 163 |
157 // Get a copy of all switches, along with their values. | 164 // Get a copy of all switches, along with their values. |
158 const SwitchMap& GetSwitches() const { return switches_; } | 165 const SwitchMap& GetSwitches() const { return switches_; } |
159 | 166 |
160 // Append a switch [with optional value] to the command line. | 167 // Append a switch [with optional value] to the command line. |
161 // Note: Switches will precede arguments regardless of appending order. | 168 // Note: Switches will precede arguments regardless of appending order. |
162 void AppendSwitch(const std::string& switch_string); | 169 void AppendSwitch(const std::string& switch_string); |
163 void AppendSwitchPath(const std::string& switch_string, | 170 void AppendSwitchPath(const std::string& switch_string, |
164 const FilePath& path); | 171 const FilePath& path); |
165 void AppendSwitchNative(const std::string& switch_string, | 172 void AppendSwitchNative(const std::string& switch_string, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
216 | 223 |
217 // The singleton CommandLine representing the current process's command line. | 224 // The singleton CommandLine representing the current process's command line. |
218 static CommandLine* current_process_commandline_; | 225 static CommandLine* current_process_commandline_; |
219 | 226 |
220 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* } | 227 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* } |
221 StringVector argv_; | 228 StringVector argv_; |
222 | 229 |
223 // Parsed-out switch keys and values. | 230 // Parsed-out switch keys and values. |
224 SwitchMap switches_; | 231 SwitchMap switches_; |
225 | 232 |
233 // A mirror of |switches_| with only references to the actual strings. | |
234 // The StringPiece internally holds a pointer to a key in |switches_| while | |
235 // the mapped_type points to a value in |switches_|. | |
236 // Used for allocation-free lookups. | |
237 StringPieceSwitchMap switches_by_stringpiece_; | |
238 | |
226 // The index after the program and switches, any arguments start here. | 239 // The index after the program and switches, any arguments start here. |
227 size_t begin_args_; | 240 size_t begin_args_; |
228 }; | 241 }; |
229 | 242 |
230 } // namespace base | 243 } // namespace base |
231 | 244 |
232 #endif // BASE_COMMAND_LINE_H_ | 245 #endif // BASE_COMMAND_LINE_H_ |
OLD | NEW |