blob: ca4af9754cb07a22268992bee0a79f79ad313fc4 [file] [log] [blame]
erikwright@chromium.org506c72b2012-02-28 03:38:12 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
evanm@google.com91cdff82008-08-08 05:07:32 +09005#include "base/command_line.h"
6
initial.commit3f4a7322008-07-27 06:49:38 +09007#include <algorithm>
jhawkins@chromium.org8e73d062011-04-05 03:04:37 +09008#include <ostream>
initial.commit3f4a7322008-07-27 06:49:38 +09009
jhawkins@chromium.org8e73d062011-04-05 03:04:37 +090010#include "base/basictypes.h"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090011#include "base/files/file_path.h"
initial.commit3f4a7322008-07-27 06:49:38 +090012#include "base/logging.h"
tfarina@chromium.org63aaf3f2013-02-08 08:01:39 +090013#include "base/strings/string_split.h"
avi@chromium.org94bd5732013-06-11 22:36:37 +090014#include "base/strings/string_util.h"
avi@chromium.org17f60622013-06-08 03:37:07 +090015#include "base/strings/utf_string_conversions.h"
brettw@chromium.org6a3802c2010-12-30 06:06:43 +090016#include "build/build_config.h"
initial.commit3f4a7322008-07-27 06:49:38 +090017
brettw@chromium.org6a3802c2010-12-30 06:06:43 +090018#if defined(OS_WIN)
19#include <windows.h>
20#include <shellapi.h>
mdm@chromium.org18175a02009-09-11 03:02:17 +090021#endif
22
brettw@chromium.org37273892014-03-18 08:07:15 +090023namespace base {
brettw@chromium.orgc02b6032013-02-16 13:12:26 +090024
evan@chromium.org4bbc6132009-01-21 10:00:22 +090025CommandLine* CommandLine::current_process_commandline_ = NULL;
initial.commit3f4a7322008-07-27 06:49:38 +090026
msw@chromium.org7b2af612011-03-01 11:28:42 +090027namespace {
brettw@chromium.org37273892014-03-18 08:07:15 +090028
msw@chromium.org53fbac12011-05-14 10:10:24 +090029const CommandLine::CharType kSwitchTerminator[] = FILE_PATH_LITERAL("--");
30const CommandLine::CharType kSwitchValueSeparator[] = FILE_PATH_LITERAL("=");
brettw@chromium.org40b2d582013-09-26 08:36:00 +090031
msw@chromium.org7b2af612011-03-01 11:28:42 +090032// Since we use a lazy match, make sure that longer versions (like "--") are
33// listed before shorter versions (like "-") of similar prefixes.
pinkerton@google.com0c0e01c2008-08-09 05:46:21 +090034#if defined(OS_WIN)
brettw@chromium.org40b2d582013-09-26 08:36:00 +090035// 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.
msw@chromium.org53fbac12011-05-14 10:10:24 +090038const CommandLine::CharType* const kSwitchPrefixes[] = {L"--", L"-", L"/"};
pinkerton@google.com0c0e01c2008-08-09 05:46:21 +090039#elif defined(OS_POSIX)
evanm@google.com638e9fb2008-08-12 10:14:37 +090040// Unixes don't use slash as a switch.
msw@chromium.org53fbac12011-05-14 10:10:24 +090041const CommandLine::CharType* const kSwitchPrefixes[] = {"--", "-"};
pinkerton@google.com0c0e01c2008-08-09 05:46:21 +090042#endif
brettw@chromium.org40b2d582013-09-26 08:36:00 +090043size_t switch_prefix_count = arraysize(kSwitchPrefixes);
initial.commit3f4a7322008-07-27 06:49:38 +090044
msw@chromium.org53fbac12011-05-14 10:10:24 +090045size_t GetSwitchPrefixLength(const CommandLine::StringType& string) {
brettw@chromium.org40b2d582013-09-26 08:36:00 +090046 for (size_t i = 0; i < switch_prefix_count; ++i) {
msw@chromium.org53fbac12011-05-14 10:10:24 +090047 CommandLine::StringType prefix(kSwitchPrefixes[i]);
joth@chromium.orgb23d1842011-09-14 00:45:34 +090048 if (string.compare(0, prefix.length(), prefix) == 0)
msw@chromium.org53fbac12011-05-14 10:10:24 +090049 return prefix.length();
50 }
51 return 0;
initial.commit3f4a7322008-07-27 06:49:38 +090052}
msw@chromium.orgda7a5482011-02-20 15:33:04 +090053
msw@chromium.org53fbac12011-05-14 10:10:24 +090054// 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();
jochen@chromium.org1e9efb12012-10-19 15:19:59 +090061 size_t prefix_length = GetSwitchPrefixLength(string);
62 if (prefix_length == 0 || prefix_length == string.length())
msw@chromium.org53fbac12011-05-14 10:10:24 +090063 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.
73void AppendSwitchesAndArguments(CommandLine& command_line,
74 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];
brettw@chromium.org37273892014-03-18 08:07:15 +090078 TrimWhitespace(arg, TRIM_ALL, &arg);
msw@chromium.org53fbac12011-05-14 10:10:24 +090079
80 CommandLine::StringType switch_string;
81 CommandLine::StringType switch_value;
82 parse_switches &= (arg != kSwitchTerminator);
83 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) {
84#if defined(OS_WIN)
brettw@chromium.org37273892014-03-18 08:07:15 +090085 command_line.AppendSwitchNative(UTF16ToASCII(switch_string),
brettw@chromium.org18c07022014-03-15 06:11:46 +090086 switch_value);
msw@chromium.org53fbac12011-05-14 10:10:24 +090087#elif defined(OS_POSIX)
88 command_line.AppendSwitchNative(switch_string, switch_value);
89#endif
90 } else {
91 command_line.AppendArgNative(arg);
92 }
93 }
94}
95
96// Lowercase switches for backwards compatiblity *on Windows*.
97std::string LowerASCIIOnWindows(const std::string& string) {
98#if defined(OS_WIN)
brettw@chromium.orge6d07792014-08-08 01:55:42 +090099 return base::StringToLowerASCII(string);
msw@chromium.org53fbac12011-05-14 10:10:24 +0900100#elif defined(OS_POSIX)
101 return string;
102#endif
103}
104
105
106#if defined(OS_WIN)
107// Quote a string as necessary for CommandLineToArgvW compatiblity *on Windows*.
108std::wstring QuoteForCommandLineToArgvW(const std::wstring& arg) {
msw@chromium.orgda7a5482011-02-20 15:33:04 +0900109 // We follow the quoting rules of CommandLineToArgvW.
110 // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
111 if (arg.find_first_of(L" \\\"") == std::wstring::npos) {
112 // No quoting necessary.
113 return arg;
114 }
115
116 std::wstring out;
117 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;
122 for (; end < arg.size() && arg[end] == '\\'; ++end)
123 /* empty */;
124 size_t backslash_count = end - start;
125
126 // Backslashes are escapes only if the run is followed by a double quote.
127 // Since we also will end the string with a double quote, we escape for
128 // either a double quote or the end of the string.
129 if (end == arg.size() || arg[end] == '"') {
130 // To quote, we need to output 2x as many backslashes.
131 backslash_count *= 2;
132 }
133 for (size_t j = 0; j < backslash_count; ++j)
134 out.push_back('\\');
135
136 // Advance i to one before the end to balance i++ in loop.
137 i = end - 1;
138 } else if (arg[i] == '"') {
139 out.push_back('\\');
140 out.push_back('"');
141 } else {
142 out.push_back(arg[i]);
143 }
144 }
145 out.push_back('"');
146
147 return out;
148}
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900149#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900150
msw@chromium.org7b2af612011-03-01 11:28:42 +0900151} // namespace
152
msw@chromium.org53fbac12011-05-14 10:10:24 +0900153CommandLine::CommandLine(NoProgram no_program)
154 : argv_(1),
155 begin_args_(1) {
msw@chromium.org7b2af612011-03-01 11:28:42 +0900156}
157
msw@chromium.org53fbac12011-05-14 10:10:24 +0900158CommandLine::CommandLine(const FilePath& program)
159 : argv_(1),
160 begin_args_(1) {
161 SetProgram(program);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900162}
163
msw@chromium.org53fbac12011-05-14 10:10:24 +0900164CommandLine::CommandLine(int argc, const CommandLine::CharType* const* argv)
165 : argv_(1),
166 begin_args_(1) {
msw@chromium.org7b2af612011-03-01 11:28:42 +0900167 InitFromArgv(argc, argv);
168}
169
msw@chromium.org53fbac12011-05-14 10:10:24 +0900170CommandLine::CommandLine(const StringVector& argv)
171 : argv_(1),
172 begin_args_(1) {
msw@chromium.org7b2af612011-03-01 11:28:42 +0900173 InitFromArgv(argv);
174}
msw@chromium.org7b2af612011-03-01 11:28:42 +0900175
msw@chromium.org96b695b2011-03-02 05:47:58 +0900176CommandLine::~CommandLine() {
177}
178
brettw@chromium.org40b2d582013-09-26 08:36:00 +0900179#if defined(OS_WIN)
180// static
181void CommandLine::set_slash_is_not_a_switch() {
182 // The last switch prefix should be slash, so adjust the size to skip it.
183 DCHECK(wcscmp(kSwitchPrefixes[arraysize(kSwitchPrefixes) - 1], L"/") == 0);
184 switch_prefix_count = arraysize(kSwitchPrefixes) - 1;
185}
186#endif
187
msw@chromium.org7b2af612011-03-01 11:28:42 +0900188// static
erikwright@chromium.org506c72b2012-02-28 03:38:12 +0900189bool CommandLine::Init(int argc, const char* const* argv) {
rvargas@google.com58a11842011-07-14 03:03:34 +0900190 if (current_process_commandline_) {
191 // If this is intentional, Reset() must be called first. If we are using
192 // the shared build mode, we have to share a single object across multiple
193 // shared libraries.
erikwright@chromium.org506c72b2012-02-28 03:38:12 +0900194 return false;
rvargas@google.com58a11842011-07-14 03:03:34 +0900195 }
196
msw@chromium.org53fbac12011-05-14 10:10:24 +0900197 current_process_commandline_ = new CommandLine(NO_PROGRAM);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900198#if defined(OS_WIN)
199 current_process_commandline_->ParseFromString(::GetCommandLineW());
200#elif defined(OS_POSIX)
201 current_process_commandline_->InitFromArgv(argc, argv);
202#endif
erikwright@chromium.org506c72b2012-02-28 03:38:12 +0900203
204 return true;
msw@chromium.org7b2af612011-03-01 11:28:42 +0900205}
206
207// static
208void CommandLine::Reset() {
209 DCHECK(current_process_commandline_);
210 delete current_process_commandline_;
211 current_process_commandline_ = NULL;
212}
213
214// static
215CommandLine* CommandLine::ForCurrentProcess() {
216 DCHECK(current_process_commandline_);
217 return current_process_commandline_;
erg@chromium.org493f5f62010-07-16 06:03:54 +0900218}
219
vollick@chromium.org4ff9adc2013-07-12 08:10:40 +0900220// static
221bool CommandLine::InitializedForCurrentProcess() {
222 return !!current_process_commandline_;
223}
224
evanm@google.com91cdff82008-08-08 05:07:32 +0900225#if defined(OS_WIN)
msw@chromium.org7b2af612011-03-01 11:28:42 +0900226// static
227CommandLine CommandLine::FromString(const std::wstring& command_line) {
msw@chromium.org53fbac12011-05-14 10:10:24 +0900228 CommandLine cmd(NO_PROGRAM);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900229 cmd.ParseFromString(command_line);
230 return cmd;
231}
msw@chromium.org53fbac12011-05-14 10:10:24 +0900232#endif
msw@chromium.org7b2af612011-03-01 11:28:42 +0900233
msw@chromium.org53fbac12011-05-14 10:10:24 +0900234void CommandLine::InitFromArgv(int argc,
235 const CommandLine::CharType* const* argv) {
236 StringVector new_argv;
msw@chromium.org7b2af612011-03-01 11:28:42 +0900237 for (int i = 0; i < argc; ++i)
msw@chromium.org53fbac12011-05-14 10:10:24 +0900238 new_argv.push_back(argv[i]);
239 InitFromArgv(new_argv);
evan@chromium.orgc4fee2c2009-10-27 07:39:33 +0900240}
241
msw@chromium.org7b2af612011-03-01 11:28:42 +0900242void CommandLine::InitFromArgv(const StringVector& argv) {
msw@chromium.org53fbac12011-05-14 10:10:24 +0900243 argv_ = StringVector(1);
bartfab@chromium.orgad4cf182013-06-18 17:19:18 +0900244 switches_.clear();
msw@chromium.org53fbac12011-05-14 10:10:24 +0900245 begin_args_ = 1;
246 SetProgram(argv.empty() ? FilePath() : FilePath(argv[0]));
247 AppendSwitchesAndArguments(*this, argv);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900248}
msw@chromium.org7b2af612011-03-01 11:28:42 +0900249
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900250CommandLine::StringType CommandLine::GetCommandLineString() const {
msw@chromium.org53fbac12011-05-14 10:10:24 +0900251 StringType string(argv_[0]);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900252#if defined(OS_WIN)
msw@chromium.org53fbac12011-05-14 10:10:24 +0900253 string = QuoteForCommandLineToArgvW(string);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900254#endif
gab@chromium.orge399ff42012-10-30 06:31:31 +0900255 StringType params(GetArgumentsString());
256 if (!params.empty()) {
257 string.append(StringType(FILE_PATH_LITERAL(" ")));
258 string.append(params);
259 }
260 return string;
261}
262
263CommandLine::StringType CommandLine::GetArgumentsString() const {
264 StringType params;
msw@chromium.org53fbac12011-05-14 10:10:24 +0900265 // Append switches and arguments.
266 bool parse_switches = true;
267 for (size_t i = 1; i < argv_.size(); ++i) {
gab@chromium.orge399ff42012-10-30 06:31:31 +0900268 StringType arg = argv_[i];
269 StringType switch_string;
270 StringType switch_value;
msw@chromium.org53fbac12011-05-14 10:10:24 +0900271 parse_switches &= arg != kSwitchTerminator;
gab@chromium.orge399ff42012-10-30 06:31:31 +0900272 if (i > 1)
273 params.append(StringType(FILE_PATH_LITERAL(" ")));
msw@chromium.org53fbac12011-05-14 10:10:24 +0900274 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) {
gab@chromium.orge399ff42012-10-30 06:31:31 +0900275 params.append(switch_string);
msw@chromium.org53fbac12011-05-14 10:10:24 +0900276 if (!switch_value.empty()) {
277#if defined(OS_WIN)
278 switch_value = QuoteForCommandLineToArgvW(switch_value);
279#endif
gab@chromium.orge399ff42012-10-30 06:31:31 +0900280 params.append(kSwitchValueSeparator + switch_value);
msw@chromium.org53fbac12011-05-14 10:10:24 +0900281 }
282 }
283 else {
284#if defined(OS_WIN)
285 arg = QuoteForCommandLineToArgvW(arg);
286#endif
gab@chromium.orge399ff42012-10-30 06:31:31 +0900287 params.append(arg);
msw@chromium.org53fbac12011-05-14 10:10:24 +0900288 }
289 }
gab@chromium.orge399ff42012-10-30 06:31:31 +0900290 return params;
msw@chromium.org7b2af612011-03-01 11:28:42 +0900291}
292
293FilePath CommandLine::GetProgram() const {
msw@chromium.org7b2af612011-03-01 11:28:42 +0900294 return FilePath(argv_[0]);
msw@chromium.org53fbac12011-05-14 10:10:24 +0900295}
296
297void CommandLine::SetProgram(const FilePath& program) {
brettw@chromium.org37273892014-03-18 08:07:15 +0900298 TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900299}
300
301bool CommandLine::HasSwitch(const std::string& switch_string) const {
msw@chromium.org53fbac12011-05-14 10:10:24 +0900302 return switches_.find(LowerASCIIOnWindows(switch_string)) != switches_.end();
msw@chromium.org7b2af612011-03-01 11:28:42 +0900303}
304
305std::string CommandLine::GetSwitchValueASCII(
306 const std::string& switch_string) const {
msw@chromium.org53fbac12011-05-14 10:10:24 +0900307 StringType value = GetSwitchValueNative(switch_string);
brettw@chromium.org93397402014-03-18 08:55:43 +0900308 if (!IsStringASCII(value)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900309 DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII.";
310 return std::string();
msw@chromium.org7b2af612011-03-01 11:28:42 +0900311 }
312#if defined(OS_WIN)
brettw@chromium.org37273892014-03-18 08:07:15 +0900313 return UTF16ToASCII(value);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900314#else
315 return value;
316#endif
317}
318
319FilePath CommandLine::GetSwitchValuePath(
320 const std::string& switch_string) const {
321 return FilePath(GetSwitchValueNative(switch_string));
322}
323
324CommandLine::StringType CommandLine::GetSwitchValueNative(
325 const std::string& switch_string) const {
vivek.vg@samsung.comeb3dc332013-11-21 21:05:28 +0900326 SwitchMap::const_iterator result =
327 switches_.find(LowerASCIIOnWindows(switch_string));
msw@chromium.org53fbac12011-05-14 10:10:24 +0900328 return result == switches_.end() ? StringType() : result->second;
msw@chromium.org7b2af612011-03-01 11:28:42 +0900329}
330
msw@chromium.org7b2af612011-03-01 11:28:42 +0900331void CommandLine::AppendSwitch(const std::string& switch_string) {
msw@chromium.org53fbac12011-05-14 10:10:24 +0900332 AppendSwitchNative(switch_string, StringType());
msw@chromium.org7b2af612011-03-01 11:28:42 +0900333}
334
335void CommandLine::AppendSwitchPath(const std::string& switch_string,
336 const FilePath& path) {
337 AppendSwitchNative(switch_string, path.value());
338}
339
340void CommandLine::AppendSwitchNative(const std::string& switch_string,
341 const CommandLine::StringType& value) {
msw@chromium.org53fbac12011-05-14 10:10:24 +0900342 std::string switch_key(LowerASCIIOnWindows(switch_string));
msw@chromium.org7b2af612011-03-01 11:28:42 +0900343#if defined(OS_WIN)
brettw@chromium.org37273892014-03-18 08:07:15 +0900344 StringType combined_switch_string(ASCIIToWide(switch_key));
msw@chromium.org7b2af612011-03-01 11:28:42 +0900345#elif defined(OS_POSIX)
msw@chromium.org53fbac12011-05-14 10:10:24 +0900346 StringType combined_switch_string(switch_string);
347#endif
348 size_t prefix_length = GetSwitchPrefixLength(combined_switch_string);
349 switches_[switch_key.substr(prefix_length)] = value;
350 // Preserve existing switch prefixes in |argv_|; only append one if necessary.
351 if (prefix_length == 0)
352 combined_switch_string = kSwitchPrefixes[0] + combined_switch_string;
msw@chromium.org7b2af612011-03-01 11:28:42 +0900353 if (!value.empty())
354 combined_switch_string += kSwitchValueSeparator + value;
msw@chromium.org53fbac12011-05-14 10:10:24 +0900355 // Append the switch and update the switches/arguments divider |begin_args_|.
356 argv_.insert(argv_.begin() + begin_args_++, combined_switch_string);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900357}
358
359void CommandLine::AppendSwitchASCII(const std::string& switch_string,
360 const std::string& value_string) {
361#if defined(OS_WIN)
brettw@chromium.org37273892014-03-18 08:07:15 +0900362 AppendSwitchNative(switch_string, ASCIIToWide(value_string));
msw@chromium.org7b2af612011-03-01 11:28:42 +0900363#elif defined(OS_POSIX)
364 AppendSwitchNative(switch_string, value_string);
365#endif
366}
367
msw@chromium.org7b2af612011-03-01 11:28:42 +0900368void CommandLine::CopySwitchesFrom(const CommandLine& source,
369 const char* const switches[],
370 size_t count) {
371 for (size_t i = 0; i < count; ++i) {
msw@chromium.org53fbac12011-05-14 10:10:24 +0900372 if (source.HasSwitch(switches[i]))
373 AppendSwitchNative(switches[i], source.GetSwitchValueNative(switches[i]));
msw@chromium.org7b2af612011-03-01 11:28:42 +0900374 }
375}
376
msw@chromium.orga25adf42011-07-14 08:41:22 +0900377CommandLine::StringVector CommandLine::GetArgs() const {
msw@chromium.org53fbac12011-05-14 10:10:24 +0900378 // Gather all arguments after the last switch (may include kSwitchTerminator).
379 StringVector args(argv_.begin() + begin_args_, argv_.end());
380 // Erase only the first kSwitchTerminator (maybe "--" is a legitimate page?)
381 StringVector::iterator switch_terminator =
382 std::find(args.begin(), args.end(), kSwitchTerminator);
383 if (switch_terminator != args.end())
384 args.erase(switch_terminator);
385 return args;
386}
387
msw@chromium.org7b2af612011-03-01 11:28:42 +0900388void CommandLine::AppendArg(const std::string& value) {
389#if defined(OS_WIN)
brettw@chromium.org93397402014-03-18 08:55:43 +0900390 DCHECK(IsStringUTF8(value));
391 AppendArgNative(UTF8ToWide(value));
msw@chromium.org7b2af612011-03-01 11:28:42 +0900392#elif defined(OS_POSIX)
393 AppendArgNative(value);
394#endif
395}
396
397void CommandLine::AppendArgPath(const FilePath& path) {
398 AppendArgNative(path.value());
399}
400
401void CommandLine::AppendArgNative(const CommandLine::StringType& value) {
msw@chromium.org7b2af612011-03-01 11:28:42 +0900402 argv_.push_back(value);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900403}
404
405void CommandLine::AppendArguments(const CommandLine& other,
406 bool include_program) {
msw@chromium.org7b2af612011-03-01 11:28:42 +0900407 if (include_program)
msw@chromium.org53fbac12011-05-14 10:10:24 +0900408 SetProgram(other.GetProgram());
409 AppendSwitchesAndArguments(*this, other.argv());
msw@chromium.org7b2af612011-03-01 11:28:42 +0900410}
411
412void CommandLine::PrependWrapper(const CommandLine::StringType& wrapper) {
msw@chromium.org7b2af612011-03-01 11:28:42 +0900413 if (wrapper.empty())
414 return;
msw@chromium.org53fbac12011-05-14 10:10:24 +0900415 // The wrapper may have embedded arguments (like "gdb --args"). In this case,
416 // we don't pretend to do anything fancy, we just split on spaces.
417 StringVector wrapper_argv;
brettw@chromium.org37273892014-03-18 08:07:15 +0900418 SplitString(wrapper, FILE_PATH_LITERAL(' '), &wrapper_argv);
msw@chromium.org53fbac12011-05-14 10:10:24 +0900419 // 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();
msw@chromium.org7b2af612011-03-01 11:28:42 +0900422}
423
424#if defined(OS_WIN)
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900425void CommandLine::ParseFromString(const std::wstring& command_line) {
msw@chromium.org53fbac12011-05-14 10:10:24 +0900426 std::wstring command_line_string;
brettw@chromium.org37273892014-03-18 08:07:15 +0900427 TrimWhitespace(command_line, TRIM_ALL, &command_line_string);
msw@chromium.org53fbac12011-05-14 10:10:24 +0900428 if (command_line_string.empty())
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900429 return;
430
431 int num_args = 0;
432 wchar_t** args = NULL;
msw@chromium.org53fbac12011-05-14 10:10:24 +0900433 args = ::CommandLineToArgvW(command_line_string.c_str(), &num_args);
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900434
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900435 DPLOG_IF(FATAL, !args) << "CommandLineToArgvW failed on command line: "
brettw@chromium.org37273892014-03-18 08:07:15 +0900436 << UTF16ToUTF8(command_line);
msw@chromium.org53fbac12011-05-14 10:10:24 +0900437 InitFromArgv(num_args, args);
438 LocalFree(args);
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900439}
evanm@google.com91cdff82008-08-08 05:07:32 +0900440#endif
brettw@chromium.org37273892014-03-18 08:07:15 +0900441
442} // namespace base