blob: 99ea2b000324b87ea4ab11bae1f7c5374586331c [file] [log] [blame]
[email protected]72e2e2422012-02-27 18:38:121// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]f3adb5c2008-08-07 20:07:325#include "base/command_line.h"
6
initial.commitd7cae122008-07-26 21:49:387#include <algorithm>
[email protected]2edc2862011-04-04 18:04:378#include <ostream>
initial.commitd7cae122008-07-26 21:49:389
[email protected]57999812013-02-24 05:40:5210#include "base/files/file_path.h"
initial.commitd7cae122008-07-26 21:49:3811#include "base/logging.h"
avi9b6f42932015-12-26 22:15:1412#include "base/macros.h"
[email protected]5ae0b763e2013-02-07 23:01:3913#include "base/strings/string_split.h"
[email protected]251cd6e52013-06-11 13:36:3714#include "base/strings/string_util.h"
[email protected]a4ea1f12013-06-07 18:37:0715#include "base/strings/utf_string_conversions.h"
[email protected]74e9fa22010-12-29 21:06:4316#include "build/build_config.h"
initial.commitd7cae122008-07-26 21:49:3817
[email protected]74e9fa22010-12-29 21:06:4318#if defined(OS_WIN)
19#include <windows.h>
20#include <shellapi.h>
[email protected]7f113f32009-09-10 18:02:1721#endif
22
[email protected]2f3b1cc2014-03-17 23:07:1523namespace base {
[email protected]04af979a2013-02-16 04:12:2624
[email protected]bb975362009-01-21 01:00:2225CommandLine* CommandLine::current_process_commandline_ = NULL;
initial.commitd7cae122008-07-26 21:49:3826
[email protected]06cc083a2011-03-01 02:28:4227namespace {
[email protected]2f3b1cc2014-03-17 23:07:1528
[email protected]a40ca4302011-05-14 01:10:2429const CommandLine::CharType kSwitchTerminator[] = FILE_PATH_LITERAL("--");
30const CommandLine::CharType kSwitchValueSeparator[] = FILE_PATH_LITERAL("=");
[email protected]bf98a0e12013-09-25 23:36:0031
[email protected]06cc083a2011-03-01 02:28:4232// Since we use a lazy match, make sure that longer versions (like "--") are
33// listed before shorter versions (like "-") of similar prefixes.
[email protected]5d426332008-08-08 20:46:2134#if defined(OS_WIN)
[email protected]bf98a0e12013-09-25 23:36:0035// By putting slash last, we can control whether it is treaded as a switch
36// value by changing the value of switch_prefix_count to be one less than
37// the array size.
[email protected]a40ca4302011-05-14 01:10:2438const CommandLine::CharType* const kSwitchPrefixes[] = {L"--", L"-", L"/"};
[email protected]5d426332008-08-08 20:46:2139#elif defined(OS_POSIX)
[email protected]1a48f312008-08-12 01:14:3740// Unixes don't use slash as a switch.
[email protected]a40ca4302011-05-14 01:10:2441const CommandLine::CharType* const kSwitchPrefixes[] = {"--", "-"};
[email protected]5d426332008-08-08 20:46:2142#endif
[email protected]bf98a0e12013-09-25 23:36:0043size_t switch_prefix_count = arraysize(kSwitchPrefixes);
initial.commitd7cae122008-07-26 21:49:3844
[email protected]a40ca4302011-05-14 01:10:2445size_t GetSwitchPrefixLength(const CommandLine::StringType& string) {
[email protected]bf98a0e12013-09-25 23:36:0046 for (size_t i = 0; i < switch_prefix_count; ++i) {
[email protected]a40ca4302011-05-14 01:10:2447 CommandLine::StringType prefix(kSwitchPrefixes[i]);
[email protected]1fa39f02011-09-13 15:45:3448 if (string.compare(0, prefix.length(), prefix) == 0)
[email protected]a40ca4302011-05-14 01:10:2449 return prefix.length();
50 }
51 return 0;
initial.commitd7cae122008-07-26 21:49:3852}
[email protected]0fd23af2011-02-20 06:33:0453
[email protected]a40ca4302011-05-14 01:10:2454// Fills in |switch_string| and |switch_value| if |string| is a switch.
55// This will preserve the input switch prefix in the output |switch_string|.
56bool IsSwitch(const CommandLine::StringType& string,
57 CommandLine::StringType* switch_string,
58 CommandLine::StringType* switch_value) {
59 switch_string->clear();
60 switch_value->clear();
[email protected]21e342f2012-10-19 06:19:5961 size_t prefix_length = GetSwitchPrefixLength(string);
62 if (prefix_length == 0 || prefix_length == string.length())
[email protected]a40ca4302011-05-14 01:10:2463 return false;
64
65 const size_t equals_position = string.find(kSwitchValueSeparator);
66 *switch_string = string.substr(0, equals_position);
67 if (equals_position != CommandLine::StringType::npos)
68 *switch_value = string.substr(equals_position + 1);
69 return true;
70}
71
72// Append switches and arguments, keeping switches before arguments.
thestig8badc792014-12-04 22:14:2273void AppendSwitchesAndArguments(CommandLine* command_line,
[email protected]a40ca4302011-05-14 01:10:2474 const CommandLine::StringVector& argv) {
75 bool parse_switches = true;
76 for (size_t i = 1; i < argv.size(); ++i) {
77 CommandLine::StringType arg = argv[i];
tfarina023b1dcc2015-12-06 13:25:4178#if defined(OS_WIN)
[email protected]2f3b1cc2014-03-17 23:07:1579 TrimWhitespace(arg, TRIM_ALL, &arg);
tfarina023b1dcc2015-12-06 13:25:4180#else
81 TrimWhitespaceASCII(arg, TRIM_ALL, &arg);
82#endif
[email protected]a40ca4302011-05-14 01:10:2483
84 CommandLine::StringType switch_string;
85 CommandLine::StringType switch_value;
86 parse_switches &= (arg != kSwitchTerminator);
87 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) {
88#if defined(OS_WIN)
thestig8badc792014-12-04 22:14:2289 command_line->AppendSwitchNative(UTF16ToASCII(switch_string),
90 switch_value);
[email protected]a40ca4302011-05-14 01:10:2491#elif defined(OS_POSIX)
thestig8badc792014-12-04 22:14:2292 command_line->AppendSwitchNative(switch_string, switch_value);
[email protected]a40ca4302011-05-14 01:10:2493#endif
94 } else {
thestig8badc792014-12-04 22:14:2295 command_line->AppendArgNative(arg);
[email protected]a40ca4302011-05-14 01:10:2496 }
97 }
98}
99
[email protected]a40ca4302011-05-14 01:10:24100#if defined(OS_WIN)
101// Quote a string as necessary for CommandLineToArgvW compatiblity *on Windows*.
thestig8badc792014-12-04 22:14:22102string16 QuoteForCommandLineToArgvW(const string16& arg,
103 bool quote_placeholders) {
[email protected]0fd23af2011-02-20 06:33:04104 // We follow the quoting rules of CommandLineToArgvW.
105 // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
thestig8badc792014-12-04 22:14:22106 string16 quotable_chars(L" \\\"");
mgiucac974d5102014-10-01 09:24:51107 // We may also be required to quote '%', which is commonly used in a command
108 // line as a placeholder. (It may be substituted for a string with spaces.)
109 if (quote_placeholders)
110 quotable_chars.push_back(L'%');
thestig8badc792014-12-04 22:14:22111 if (arg.find_first_of(quotable_chars) == string16::npos) {
[email protected]0fd23af2011-02-20 06:33:04112 // No quoting necessary.
113 return arg;
114 }
115
thestig8badc792014-12-04 22:14:22116 string16 out;
[email protected]0fd23af2011-02-20 06:33:04117 out.push_back(L'"');
118 for (size_t i = 0; i < arg.size(); ++i) {
119 if (arg[i] == '\\') {
120 // Find the extent of this run of backslashes.
121 size_t start = i, end = start + 1;
thestig8badc792014-12-04 22:14:22122 for (; end < arg.size() && arg[end] == '\\'; ++end) {}
[email protected]0fd23af2011-02-20 06:33:04123 size_t backslash_count = end - start;
124
125 // Backslashes are escapes only if the run is followed by a double quote.
126 // Since we also will end the string with a double quote, we escape for
127 // either a double quote or the end of the string.
128 if (end == arg.size() || arg[end] == '"') {
129 // To quote, we need to output 2x as many backslashes.
130 backslash_count *= 2;
131 }
132 for (size_t j = 0; j < backslash_count; ++j)
133 out.push_back('\\');
134
135 // Advance i to one before the end to balance i++ in loop.
136 i = end - 1;
137 } else if (arg[i] == '"') {
138 out.push_back('\\');
139 out.push_back('"');
140 } else {
141 out.push_back(arg[i]);
142 }
143 }
144 out.push_back('"');
145
146 return out;
147}
[email protected]bb975362009-01-21 01:00:22148#endif
initial.commitd7cae122008-07-26 21:49:38149
[email protected]06cc083a2011-03-01 02:28:42150} // namespace
151
[email protected]a40ca4302011-05-14 01:10:24152CommandLine::CommandLine(NoProgram no_program)
153 : argv_(1),
154 begin_args_(1) {
[email protected]06cc083a2011-03-01 02:28:42155}
156
[email protected]a40ca4302011-05-14 01:10:24157CommandLine::CommandLine(const FilePath& program)
158 : argv_(1),
159 begin_args_(1) {
160 SetProgram(program);
[email protected]06cc083a2011-03-01 02:28:42161}
162
[email protected]a40ca4302011-05-14 01:10:24163CommandLine::CommandLine(int argc, const CommandLine::CharType* const* argv)
164 : argv_(1),
165 begin_args_(1) {
[email protected]06cc083a2011-03-01 02:28:42166 InitFromArgv(argc, argv);
167}
168
[email protected]a40ca4302011-05-14 01:10:24169CommandLine::CommandLine(const StringVector& argv)
170 : argv_(1),
171 begin_args_(1) {
[email protected]06cc083a2011-03-01 02:28:42172 InitFromArgv(argv);
173}
[email protected]06cc083a2011-03-01 02:28:42174
jackhou1bd9da92015-05-21 04:48:00175CommandLine::CommandLine(const CommandLine& other)
176 : argv_(other.argv_),
177 switches_(other.switches_),
178 begin_args_(other.begin_args_) {
179 ResetStringPieces();
180}
181
182CommandLine& CommandLine::operator=(const CommandLine& other) {
183 argv_ = other.argv_;
184 switches_ = other.switches_;
185 begin_args_ = other.begin_args_;
186 ResetStringPieces();
187 return *this;
188}
189
[email protected]acbeb3d2011-03-01 20:47:58190CommandLine::~CommandLine() {
191}
192
[email protected]bf98a0e12013-09-25 23:36:00193#if defined(OS_WIN)
194// static
195void CommandLine::set_slash_is_not_a_switch() {
196 // The last switch prefix should be slash, so adjust the size to skip it.
danakj94219a212015-03-09 22:27:25197 DCHECK_EQ(wcscmp(kSwitchPrefixes[arraysize(kSwitchPrefixes) - 1], L"/"), 0);
[email protected]bf98a0e12013-09-25 23:36:00198 switch_prefix_count = arraysize(kSwitchPrefixes) - 1;
199}
anantad936bc12016-06-22 21:40:31200
201// static
202void CommandLine::InitUsingArgvForTesting(int argc, const char* const* argv) {
203 DCHECK(!current_process_commandline_);
204 current_process_commandline_ = new CommandLine(NO_PROGRAM);
205 // On Windows we need to convert the command line arguments to string16.
206 base::CommandLine::StringVector argv_vector;
207 for (int i = 0; i < argc; ++i)
208 argv_vector.push_back(UTF8ToUTF16(argv[i]));
209 current_process_commandline_->InitFromArgv(argv_vector);
210}
[email protected]bf98a0e12013-09-25 23:36:00211#endif
212
[email protected]06cc083a2011-03-01 02:28:42213// static
[email protected]72e2e2422012-02-27 18:38:12214bool CommandLine::Init(int argc, const char* const* argv) {
[email protected]f96fe2c42011-07-13 18:03:34215 if (current_process_commandline_) {
216 // If this is intentional, Reset() must be called first. If we are using
217 // the shared build mode, we have to share a single object across multiple
218 // shared libraries.
[email protected]72e2e2422012-02-27 18:38:12219 return false;
[email protected]f96fe2c42011-07-13 18:03:34220 }
221
[email protected]a40ca4302011-05-14 01:10:24222 current_process_commandline_ = new CommandLine(NO_PROGRAM);
[email protected]06cc083a2011-03-01 02:28:42223#if defined(OS_WIN)
224 current_process_commandline_->ParseFromString(::GetCommandLineW());
225#elif defined(OS_POSIX)
226 current_process_commandline_->InitFromArgv(argc, argv);
227#endif
[email protected]72e2e2422012-02-27 18:38:12228
229 return true;
[email protected]06cc083a2011-03-01 02:28:42230}
231
232// static
233void CommandLine::Reset() {
234 DCHECK(current_process_commandline_);
235 delete current_process_commandline_;
236 current_process_commandline_ = NULL;
237}
238
239// static
240CommandLine* CommandLine::ForCurrentProcess() {
241 DCHECK(current_process_commandline_);
242 return current_process_commandline_;
[email protected]3a3d47472010-07-15 21:03:54243}
244
[email protected]2bf64a92013-07-11 23:10:40245// static
246bool CommandLine::InitializedForCurrentProcess() {
247 return !!current_process_commandline_;
248}
249
[email protected]f3adb5c2008-08-07 20:07:32250#if defined(OS_WIN)
[email protected]06cc083a2011-03-01 02:28:42251// static
thestig8badc792014-12-04 22:14:22252CommandLine CommandLine::FromString(const string16& command_line) {
[email protected]a40ca4302011-05-14 01:10:24253 CommandLine cmd(NO_PROGRAM);
[email protected]06cc083a2011-03-01 02:28:42254 cmd.ParseFromString(command_line);
255 return cmd;
256}
[email protected]a40ca4302011-05-14 01:10:24257#endif
[email protected]06cc083a2011-03-01 02:28:42258
[email protected]a40ca4302011-05-14 01:10:24259void CommandLine::InitFromArgv(int argc,
260 const CommandLine::CharType* const* argv) {
261 StringVector new_argv;
[email protected]06cc083a2011-03-01 02:28:42262 for (int i = 0; i < argc; ++i)
[email protected]a40ca4302011-05-14 01:10:24263 new_argv.push_back(argv[i]);
264 InitFromArgv(new_argv);
[email protected]51343d5a2009-10-26 22:39:33265}
266
[email protected]06cc083a2011-03-01 02:28:42267void CommandLine::InitFromArgv(const StringVector& argv) {
[email protected]a40ca4302011-05-14 01:10:24268 argv_ = StringVector(1);
[email protected]93660ab32013-06-18 08:19:18269 switches_.clear();
jackhou1bd9da92015-05-21 04:48:00270 switches_by_stringpiece_.clear();
[email protected]a40ca4302011-05-14 01:10:24271 begin_args_ = 1;
272 SetProgram(argv.empty() ? FilePath() : FilePath(argv[0]));
thestig8badc792014-12-04 22:14:22273 AppendSwitchesAndArguments(this, argv);
[email protected]06cc083a2011-03-01 02:28:42274}
[email protected]06cc083a2011-03-01 02:28:42275
[email protected]06cc083a2011-03-01 02:28:42276FilePath CommandLine::GetProgram() const {
[email protected]06cc083a2011-03-01 02:28:42277 return FilePath(argv_[0]);
[email protected]a40ca4302011-05-14 01:10:24278}
279
280void CommandLine::SetProgram(const FilePath& program) {
tfarina023b1dcc2015-12-06 13:25:41281#if defined(OS_WIN)
[email protected]2f3b1cc2014-03-17 23:07:15282 TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]);
tfarina023b1dcc2015-12-06 13:25:41283#else
284 TrimWhitespaceASCII(program.value(), TRIM_ALL, &argv_[0]);
285#endif
[email protected]06cc083a2011-03-01 02:28:42286}
287
jackhou1bd9da92015-05-21 04:48:00288bool CommandLine::HasSwitch(const base::StringPiece& switch_string) const {
brettwfce8d192015-08-10 19:07:51289 DCHECK_EQ(ToLowerASCII(switch_string), switch_string);
jackhou1bd9da92015-05-21 04:48:00290 return switches_by_stringpiece_.find(switch_string) !=
291 switches_by_stringpiece_.end();
[email protected]06cc083a2011-03-01 02:28:42292}
293
jackhou1bd9da92015-05-21 04:48:00294bool CommandLine::HasSwitch(const char switch_constant[]) const {
295 return HasSwitch(base::StringPiece(switch_constant));
tapted009a1dc82015-03-30 03:57:10296}
297
[email protected]06cc083a2011-03-01 02:28:42298std::string CommandLine::GetSwitchValueASCII(
jackhou1bd9da92015-05-21 04:48:00299 const base::StringPiece& switch_string) const {
[email protected]a40ca4302011-05-14 01:10:24300 StringType value = GetSwitchValueNative(switch_string);
[email protected]bd6fc2f2014-03-17 23:55:43301 if (!IsStringASCII(value)) {
[email protected]a42d4632011-10-26 21:48:00302 DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII.";
303 return std::string();
[email protected]06cc083a2011-03-01 02:28:42304 }
305#if defined(OS_WIN)
[email protected]2f3b1cc2014-03-17 23:07:15306 return UTF16ToASCII(value);
[email protected]06cc083a2011-03-01 02:28:42307#else
308 return value;
309#endif
310}
311
312FilePath CommandLine::GetSwitchValuePath(
jackhou1bd9da92015-05-21 04:48:00313 const base::StringPiece& switch_string) const {
[email protected]06cc083a2011-03-01 02:28:42314 return FilePath(GetSwitchValueNative(switch_string));
315}
316
317CommandLine::StringType CommandLine::GetSwitchValueNative(
jackhou1bd9da92015-05-21 04:48:00318 const base::StringPiece& switch_string) const {
brettwfce8d192015-08-10 19:07:51319 DCHECK_EQ(ToLowerASCII(switch_string), switch_string);
jackhou1bd9da92015-05-21 04:48:00320 auto result = switches_by_stringpiece_.find(switch_string);
321 return result == switches_by_stringpiece_.end() ? StringType()
322 : *(result->second);
[email protected]06cc083a2011-03-01 02:28:42323}
324
[email protected]06cc083a2011-03-01 02:28:42325void CommandLine::AppendSwitch(const std::string& switch_string) {
[email protected]a40ca4302011-05-14 01:10:24326 AppendSwitchNative(switch_string, StringType());
[email protected]06cc083a2011-03-01 02:28:42327}
328
329void CommandLine::AppendSwitchPath(const std::string& switch_string,
330 const FilePath& path) {
331 AppendSwitchNative(switch_string, path.value());
332}
333
334void CommandLine::AppendSwitchNative(const std::string& switch_string,
335 const CommandLine::StringType& value) {
336#if defined(OS_WIN)
brettwfce8d192015-08-10 19:07:51337 const std::string switch_key = ToLowerASCII(switch_string);
thestig8badc792014-12-04 22:14:22338 StringType combined_switch_string(ASCIIToUTF16(switch_key));
[email protected]06cc083a2011-03-01 02:28:42339#elif defined(OS_POSIX)
jackhou1bd9da92015-05-21 04:48:00340 const std::string& switch_key = switch_string;
341 StringType combined_switch_string(switch_key);
[email protected]a40ca4302011-05-14 01:10:24342#endif
343 size_t prefix_length = GetSwitchPrefixLength(combined_switch_string);
jackhou1bd9da92015-05-21 04:48:00344 auto insertion =
345 switches_.insert(make_pair(switch_key.substr(prefix_length), value));
346 if (!insertion.second)
347 insertion.first->second = value;
348 switches_by_stringpiece_[insertion.first->first] = &(insertion.first->second);
[email protected]a40ca4302011-05-14 01:10:24349 // Preserve existing switch prefixes in |argv_|; only append one if necessary.
350 if (prefix_length == 0)
351 combined_switch_string = kSwitchPrefixes[0] + combined_switch_string;
[email protected]06cc083a2011-03-01 02:28:42352 if (!value.empty())
353 combined_switch_string += kSwitchValueSeparator + value;
[email protected]a40ca4302011-05-14 01:10:24354 // Append the switch and update the switches/arguments divider |begin_args_|.
355 argv_.insert(argv_.begin() + begin_args_++, combined_switch_string);
[email protected]06cc083a2011-03-01 02:28:42356}
357
358void CommandLine::AppendSwitchASCII(const std::string& switch_string,
359 const std::string& value_string) {
360#if defined(OS_WIN)
thestig8badc792014-12-04 22:14:22361 AppendSwitchNative(switch_string, ASCIIToUTF16(value_string));
[email protected]06cc083a2011-03-01 02:28:42362#elif defined(OS_POSIX)
363 AppendSwitchNative(switch_string, value_string);
364#endif
365}
366
[email protected]06cc083a2011-03-01 02:28:42367void CommandLine::CopySwitchesFrom(const CommandLine& source,
368 const char* const switches[],
369 size_t count) {
370 for (size_t i = 0; i < count; ++i) {
[email protected]a40ca4302011-05-14 01:10:24371 if (source.HasSwitch(switches[i]))
372 AppendSwitchNative(switches[i], source.GetSwitchValueNative(switches[i]));
[email protected]06cc083a2011-03-01 02:28:42373 }
374}
375
[email protected]75f1c782011-07-13 23:41:22376CommandLine::StringVector CommandLine::GetArgs() const {
[email protected]a40ca4302011-05-14 01:10:24377 // Gather all arguments after the last switch (may include kSwitchTerminator).
378 StringVector args(argv_.begin() + begin_args_, argv_.end());
379 // Erase only the first kSwitchTerminator (maybe "--" is a legitimate page?)
380 StringVector::iterator switch_terminator =
381 std::find(args.begin(), args.end(), kSwitchTerminator);
382 if (switch_terminator != args.end())
383 args.erase(switch_terminator);
384 return args;
385}
386
[email protected]06cc083a2011-03-01 02:28:42387void CommandLine::AppendArg(const std::string& value) {
388#if defined(OS_WIN)
[email protected]bd6fc2f2014-03-17 23:55:43389 DCHECK(IsStringUTF8(value));
390 AppendArgNative(UTF8ToWide(value));
[email protected]06cc083a2011-03-01 02:28:42391#elif defined(OS_POSIX)
392 AppendArgNative(value);
393#endif
394}
395
396void CommandLine::AppendArgPath(const FilePath& path) {
397 AppendArgNative(path.value());
398}
399
400void CommandLine::AppendArgNative(const CommandLine::StringType& value) {
[email protected]06cc083a2011-03-01 02:28:42401 argv_.push_back(value);
[email protected]06cc083a2011-03-01 02:28:42402}
403
404void CommandLine::AppendArguments(const CommandLine& other,
405 bool include_program) {
[email protected]06cc083a2011-03-01 02:28:42406 if (include_program)
[email protected]a40ca4302011-05-14 01:10:24407 SetProgram(other.GetProgram());
thestig8badc792014-12-04 22:14:22408 AppendSwitchesAndArguments(this, other.argv());
[email protected]06cc083a2011-03-01 02:28:42409}
410
411void CommandLine::PrependWrapper(const CommandLine::StringType& wrapper) {
[email protected]06cc083a2011-03-01 02:28:42412 if (wrapper.empty())
413 return;
[email protected]a40ca4302011-05-14 01:10:24414 // The wrapper may have embedded arguments (like "gdb --args"). In this case,
415 // we don't pretend to do anything fancy, we just split on spaces.
brettw26dab8f02015-08-08 00:28:47416 StringVector wrapper_argv = SplitString(
417 wrapper, FilePath::StringType(1, ' '), base::TRIM_WHITESPACE,
418 base::SPLIT_WANT_ALL);
[email protected]a40ca4302011-05-14 01:10:24419 // Prepend the wrapper and update the switches/arguments |begin_args_|.
420 argv_.insert(argv_.begin(), wrapper_argv.begin(), wrapper_argv.end());
421 begin_args_ += wrapper_argv.size();
[email protected]06cc083a2011-03-01 02:28:42422}
423
424#if defined(OS_WIN)
thestig8badc792014-12-04 22:14:22425void CommandLine::ParseFromString(const string16& command_line) {
426 string16 command_line_string;
[email protected]2f3b1cc2014-03-17 23:07:15427 TrimWhitespace(command_line, TRIM_ALL, &command_line_string);
[email protected]a40ca4302011-05-14 01:10:24428 if (command_line_string.empty())
[email protected]bb975362009-01-21 01:00:22429 return;
430
431 int num_args = 0;
432 wchar_t** args = NULL;
[email protected]a40ca4302011-05-14 01:10:24433 args = ::CommandLineToArgvW(command_line_string.c_str(), &num_args);
[email protected]bb975362009-01-21 01:00:22434
[email protected]a42d4632011-10-26 21:48:00435 DPLOG_IF(FATAL, !args) << "CommandLineToArgvW failed on command line: "
[email protected]2f3b1cc2014-03-17 23:07:15436 << UTF16ToUTF8(command_line);
[email protected]a40ca4302011-05-14 01:10:24437 InitFromArgv(num_args, args);
438 LocalFree(args);
[email protected]bb975362009-01-21 01:00:22439}
[email protected]f3adb5c2008-08-07 20:07:32440#endif
[email protected]2f3b1cc2014-03-17 23:07:15441
mgiucac974d5102014-10-01 09:24:51442CommandLine::StringType CommandLine::GetCommandLineStringInternal(
443 bool quote_placeholders) const {
444 StringType string(argv_[0]);
445#if defined(OS_WIN)
446 string = QuoteForCommandLineToArgvW(string, quote_placeholders);
447#endif
448 StringType params(GetArgumentsStringInternal(quote_placeholders));
449 if (!params.empty()) {
450 string.append(StringType(FILE_PATH_LITERAL(" ")));
451 string.append(params);
452 }
453 return string;
454}
455
456CommandLine::StringType CommandLine::GetArgumentsStringInternal(
457 bool quote_placeholders) const {
458 StringType params;
459 // Append switches and arguments.
460 bool parse_switches = true;
461 for (size_t i = 1; i < argv_.size(); ++i) {
462 StringType arg = argv_[i];
463 StringType switch_string;
464 StringType switch_value;
465 parse_switches &= arg != kSwitchTerminator;
466 if (i > 1)
467 params.append(StringType(FILE_PATH_LITERAL(" ")));
468 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) {
469 params.append(switch_string);
470 if (!switch_value.empty()) {
471#if defined(OS_WIN)
472 switch_value =
473 QuoteForCommandLineToArgvW(switch_value, quote_placeholders);
474#endif
475 params.append(kSwitchValueSeparator + switch_value);
476 }
thestig8badc792014-12-04 22:14:22477 } else {
mgiucac974d5102014-10-01 09:24:51478#if defined(OS_WIN)
479 arg = QuoteForCommandLineToArgvW(arg, quote_placeholders);
480#endif
481 params.append(arg);
482 }
483 }
484 return params;
485}
486
jackhou1bd9da92015-05-21 04:48:00487void CommandLine::ResetStringPieces() {
488 switches_by_stringpiece_.clear();
489 for (const auto& entry : switches_)
490 switches_by_stringpiece_[entry.first] = &(entry.second);
491}
492
[email protected]2f3b1cc2014-03-17 23:07:15493} // namespace base