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. |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 static CommandLine FromString(const base::string16& command_line); | 91 static CommandLine FromString(const base::string16& command_line); |
92 #endif | 92 #endif |
93 | 93 |
94 // Initialize from an argv vector. | 94 // Initialize from an argv vector. |
95 void InitFromArgv(int argc, const CharType* const* argv); | 95 void InitFromArgv(int argc, const CharType* const* argv); |
96 void InitFromArgv(const StringVector& argv); | 96 void InitFromArgv(const StringVector& argv); |
97 | 97 |
98 // Constructs and returns the represented command line string. | 98 // Constructs and returns the represented command line string. |
99 // CAUTION! This should be avoided on POSIX because quoting behavior is | 99 // CAUTION! This should be avoided on POSIX because quoting behavior is |
100 // unclear. | 100 // unclear. |
101 StringType GetCommandLineString() const; | 101 StringType GetCommandLineString() const { |
| 102 return GetCommandLineStringInternal(false); |
| 103 } |
| 104 |
| 105 #if defined(OS_WIN) |
| 106 // Constructs and returns the represented command line string. Assumes the |
| 107 // command line contains placeholders (eg, %1) and quotes any program or |
| 108 // argument with a '%' in it. This should be avoided unless the placeholder is |
| 109 // required by an external interface (eg, the Windows registry), because it is |
| 110 // not generally safe to replace it with an arbitrary string. If possible, |
| 111 // placeholders should be replaced *before* converting the command line to a |
| 112 // string. |
| 113 StringType GetCommandLineStringWithPlaceholders() const { |
| 114 return GetCommandLineStringInternal(true); |
| 115 } |
| 116 #endif |
102 | 117 |
103 // Constructs and returns the represented arguments string. | 118 // Constructs and returns the represented arguments string. |
104 // CAUTION! This should be avoided on POSIX because quoting behavior is | 119 // CAUTION! This should be avoided on POSIX because quoting behavior is |
105 // unclear. | 120 // unclear. |
106 StringType GetArgumentsString() const; | 121 StringType GetArgumentsString() const { |
| 122 return GetArgumentsStringInternal(false); |
| 123 } |
| 124 |
| 125 #if defined(OS_WIN) |
| 126 // Constructs and returns the represented arguments string. Assumes the |
| 127 // command line contains placeholders (eg, %1) and quotes any argument with a |
| 128 // '%' in it. This should be avoided unless the placeholder is required by an |
| 129 // external interface (eg, the Windows registry), because it is not generally |
| 130 // safe to replace it with an arbitrary string. If possible, placeholders |
| 131 // should be replaced *before* converting the arguments to a string. |
| 132 StringType GetArgumentsStringWithPlaceholders() const { |
| 133 return GetArgumentsStringInternal(true); |
| 134 } |
| 135 #endif |
107 | 136 |
108 // Returns the original command line string as a vector of strings. | 137 // Returns the original command line string as a vector of strings. |
109 const StringVector& argv() const { return argv_; } | 138 const StringVector& argv() const { return argv_; } |
110 | 139 |
111 // Get and Set the program part of the command line string (the first item). | 140 // Get and Set the program part of the command line string (the first item). |
112 FilePath GetProgram() const; | 141 FilePath GetProgram() const; |
113 void SetProgram(const FilePath& program); | 142 void SetProgram(const FilePath& program); |
114 | 143 |
115 // Returns true if this command line contains the given switch. | 144 // Returns true if this command line contains the given switch. |
116 // (Switch names are case-insensitive). | 145 // (Switch names are case-insensitive). |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 #endif | 196 #endif |
168 | 197 |
169 private: | 198 private: |
170 // Disallow default constructor; a program name must be explicitly specified. | 199 // Disallow default constructor; a program name must be explicitly specified. |
171 CommandLine(); | 200 CommandLine(); |
172 // Allow the copy constructor. A common pattern is to copy of the current | 201 // Allow the copy constructor. A common pattern is to copy of the current |
173 // process's command line and then add some flags to it. For example: | 202 // process's command line and then add some flags to it. For example: |
174 // CommandLine cl(*CommandLine::ForCurrentProcess()); | 203 // CommandLine cl(*CommandLine::ForCurrentProcess()); |
175 // cl.AppendSwitch(...); | 204 // cl.AppendSwitch(...); |
176 | 205 |
| 206 // Internal version of GetCommandLineString. If |quote_placeholders| is true, |
| 207 // also quotes parts with '%' in them. |
| 208 StringType GetCommandLineStringInternal(bool quote_placeholders) const; |
| 209 |
| 210 // Internal version of GetArgumentsString. If |quote_placeholders| is true, |
| 211 // also quotes parts with '%' in them. |
| 212 StringType GetArgumentsStringInternal(bool quote_placeholders) const; |
| 213 |
177 // The singleton CommandLine representing the current process's command line. | 214 // The singleton CommandLine representing the current process's command line. |
178 static CommandLine* current_process_commandline_; | 215 static CommandLine* current_process_commandline_; |
179 | 216 |
180 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* } | 217 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* } |
181 StringVector argv_; | 218 StringVector argv_; |
182 | 219 |
183 // Parsed-out switch keys and values. | 220 // Parsed-out switch keys and values. |
184 SwitchMap switches_; | 221 SwitchMap switches_; |
185 | 222 |
186 // The index after the program and switches, any arguments start here. | 223 // The index after the program and switches, any arguments start here. |
187 size_t begin_args_; | 224 size_t begin_args_; |
188 }; | 225 }; |
189 | 226 |
190 } // namespace base | 227 } // namespace base |
191 | 228 |
192 // TODO(brettw) remove once all callers specify the namespace properly. | 229 // TODO(brettw) remove once all callers specify the namespace properly. |
193 using base::CommandLine; | 230 using base::CommandLine; |
194 | 231 |
195 #endif // BASE_COMMAND_LINE_H_ | 232 #endif // BASE_COMMAND_LINE_H_ |
OLD | NEW |