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 "build/build_config.h" | 25 #include "build/build_config.h" |
25 | 26 |
26 namespace base { | 27 namespace base { |
27 | 28 |
28 class FilePath; | 29 class FilePath; |
29 | 30 |
30 class BASE_EXPORT CommandLine { | 31 class BASE_EXPORT CommandLine { |
31 public: | 32 public: |
32 #if defined(OS_WIN) | 33 #if defined(OS_WIN) |
33 // The native command line string type. | 34 // The native command line string type. |
34 typedef std::wstring StringType; | 35 typedef base::string16 StringType; |
35 #elif defined(OS_POSIX) | 36 #elif defined(OS_POSIX) |
36 typedef std::string StringType; | 37 typedef std::string StringType; |
37 #endif | 38 #endif |
38 | 39 |
39 typedef StringType::value_type CharType; | 40 typedef StringType::value_type CharType; |
40 typedef std::vector<StringType> StringVector; | 41 typedef std::vector<StringType> StringVector; |
41 typedef std::map<std::string, StringType> SwitchMap; | 42 typedef std::map<std::string, StringType> SwitchMap; |
42 | 43 |
43 // A constructor for CommandLines that only carry switches and arguments. | 44 // A constructor for CommandLines that only carry switches and arguments. |
44 enum NoProgram { NO_PROGRAM }; | 45 enum NoProgram { NO_PROGRAM }; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 | 81 |
81 // Get the singleton CommandLine representing the current process's | 82 // Get the singleton CommandLine representing the current process's |
82 // command line. Note: returned value is mutable, but not thread safe; | 83 // command line. Note: returned value is mutable, but not thread safe; |
83 // only mutate if you know what you're doing! | 84 // only mutate if you know what you're doing! |
84 static CommandLine* ForCurrentProcess(); | 85 static CommandLine* ForCurrentProcess(); |
85 | 86 |
86 // Returns true if the CommandLine has been initialized for the given process. | 87 // Returns true if the CommandLine has been initialized for the given process. |
87 static bool InitializedForCurrentProcess(); | 88 static bool InitializedForCurrentProcess(); |
88 | 89 |
89 #if defined(OS_WIN) | 90 #if defined(OS_WIN) |
90 static CommandLine FromString(const std::wstring& command_line); | 91 static CommandLine FromString(const base::string16& command_line); |
91 #endif | 92 #endif |
92 | 93 |
93 // Initialize from an argv vector. | 94 // Initialize from an argv vector. |
94 void InitFromArgv(int argc, const CharType* const* argv); | 95 void InitFromArgv(int argc, const CharType* const* argv); |
95 void InitFromArgv(const StringVector& argv); | 96 void InitFromArgv(const StringVector& argv); |
96 | 97 |
97 // Constructs and returns the represented command line string. | 98 // Constructs and returns the represented command line string. |
98 // CAUTION! This should be avoided on POSIX because quoting behavior is | 99 // CAUTION! This should be avoided on POSIX because quoting behavior is |
99 // unclear. | 100 // unclear. |
100 StringType GetCommandLineString() const; | 101 StringType GetCommandLineString() const; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 // If |include_program| is true, include |other|'s program as well. | 156 // If |include_program| is true, include |other|'s program as well. |
156 void AppendArguments(const CommandLine& other, bool include_program); | 157 void AppendArguments(const CommandLine& other, bool include_program); |
157 | 158 |
158 // Insert a command before the current command. | 159 // Insert a command before the current command. |
159 // Common for debuggers, like "valgrind" or "gdb --args". | 160 // Common for debuggers, like "valgrind" or "gdb --args". |
160 void PrependWrapper(const StringType& wrapper); | 161 void PrependWrapper(const StringType& wrapper); |
161 | 162 |
162 #if defined(OS_WIN) | 163 #if defined(OS_WIN) |
163 // Initialize by parsing the given command line string. | 164 // Initialize by parsing the given command line string. |
164 // The program name is assumed to be the first item in the string. | 165 // The program name is assumed to be the first item in the string. |
165 void ParseFromString(const std::wstring& command_line); | 166 void ParseFromString(const base::string16& command_line); |
166 #endif | 167 #endif |
167 | 168 |
168 private: | 169 private: |
169 // Disallow default constructor; a program name must be explicitly specified. | 170 // Disallow default constructor; a program name must be explicitly specified. |
170 CommandLine(); | 171 CommandLine(); |
171 // Allow the copy constructor. A common pattern is to copy of the current | 172 // Allow the copy constructor. A common pattern is to copy of the current |
172 // process's command line and then add some flags to it. For example: | 173 // process's command line and then add some flags to it. For example: |
173 // CommandLine cl(*CommandLine::ForCurrentProcess()); | 174 // CommandLine cl(*CommandLine::ForCurrentProcess()); |
174 // cl.AppendSwitch(...); | 175 // cl.AppendSwitch(...); |
175 | 176 |
176 // The singleton CommandLine representing the current process's command line. | 177 // The singleton CommandLine representing the current process's command line. |
177 static CommandLine* current_process_commandline_; | 178 static CommandLine* current_process_commandline_; |
178 | 179 |
179 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* } | 180 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* } |
180 StringVector argv_; | 181 StringVector argv_; |
181 | 182 |
182 // Parsed-out switch keys and values. | 183 // Parsed-out switch keys and values. |
183 SwitchMap switches_; | 184 SwitchMap switches_; |
184 | 185 |
185 // The index after the program and switches, any arguments start here. | 186 // The index after the program and switches, any arguments start here. |
186 size_t begin_args_; | 187 size_t begin_args_; |
187 }; | 188 }; |
188 | 189 |
189 } // namespace base | 190 } // namespace base |
190 | 191 |
191 // TODO(brettw) remove once all callers specify the namespace properly. | 192 // TODO(brettw) remove once all callers specify the namespace properly. |
192 using base::CommandLine; | 193 using base::CommandLine; |
193 | 194 |
194 #endif // BASE_COMMAND_LINE_H_ | 195 #endif // BASE_COMMAND_LINE_H_ |
OLD | NEW |