blob: 3a5d089bb2fd1c9441fbf524e9cf5011fe3b1206 [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"
Jeremy Roman863386d2017-10-31 19:25:3813#include "base/stl_util.h"
[email protected]5ae0b763e2013-02-07 23:01:3914#include "base/strings/string_split.h"
skyostild851aa12017-03-29 17:38:3515#include "base/strings/string_tokenizer.h"
[email protected]251cd6e52013-06-11 13:36:3716#include "base/strings/string_util.h"
[email protected]a4ea1f12013-06-07 18:37:0717#include "base/strings/utf_string_conversions.h"
[email protected]74e9fa22010-12-29 21:06:4318#include "build/build_config.h"
initial.commitd7cae122008-07-26 21:49:3819
[email protected]74e9fa22010-12-29 21:06:4320#if defined(OS_WIN)
21#include <windows.h>
22#include <shellapi.h>
[email protected]7f113f32009-09-10 18:02:1723#endif
24
[email protected]2f3b1cc2014-03-17 23:07:1525namespace base {
[email protected]04af979a2013-02-16 04:12:2626
Ivan Kotenkova16212a52017-11-08 12:37:3327CommandLine* CommandLine::current_process_commandline_ = nullptr;
initial.commitd7cae122008-07-26 21:49:3828
[email protected]06cc083a2011-03-01 02:28:4229namespace {
[email protected]2f3b1cc2014-03-17 23:07:1530
[email protected]a40ca4302011-05-14 01:10:2431const CommandLine::CharType kSwitchTerminator[] = FILE_PATH_LITERAL("--");
32const CommandLine::CharType kSwitchValueSeparator[] = FILE_PATH_LITERAL("=");
[email protected]bf98a0e12013-09-25 23:36:0033
[email protected]06cc083a2011-03-01 02:28:4234// Since we use a lazy match, make sure that longer versions (like "--") are
35// listed before shorter versions (like "-") of similar prefixes.
[email protected]5d426332008-08-08 20:46:2136#if defined(OS_WIN)
[email protected]bf98a0e12013-09-25 23:36:0037// By putting slash last, we can control whether it is treaded as a switch
38// value by changing the value of switch_prefix_count to be one less than
39// the array size.
[email protected]a40ca4302011-05-14 01:10:2440const CommandLine::CharType* const kSwitchPrefixes[] = {L"--", L"-", L"/"};
[email protected]5d426332008-08-08 20:46:2141#elif defined(OS_POSIX)
[email protected]1a48f312008-08-12 01:14:3742// Unixes don't use slash as a switch.
[email protected]a40ca4302011-05-14 01:10:2443const CommandLine::CharType* const kSwitchPrefixes[] = {"--", "-"};
[email protected]5d426332008-08-08 20:46:2144#endif
[email protected]bf98a0e12013-09-25 23:36:0045size_t switch_prefix_count = arraysize(kSwitchPrefixes);
initial.commitd7cae122008-07-26 21:49:3846
[email protected]a40ca4302011-05-14 01:10:2447size_t GetSwitchPrefixLength(const CommandLine::StringType& string) {
[email protected]bf98a0e12013-09-25 23:36:0048 for (size_t i = 0; i < switch_prefix_count; ++i) {
[email protected]a40ca4302011-05-14 01:10:2449 CommandLine::StringType prefix(kSwitchPrefixes[i]);
[email protected]1fa39f02011-09-13 15:45:3450 if (string.compare(0, prefix.length(), prefix) == 0)
[email protected]a40ca4302011-05-14 01:10:2451 return prefix.length();
52 }
53 return 0;
initial.commitd7cae122008-07-26 21:49:3854}
[email protected]0fd23af2011-02-20 06:33:0455
[email protected]a40ca4302011-05-14 01:10:2456// Fills in |switch_string| and |switch_value| if |string| is a switch.
57// This will preserve the input switch prefix in the output |switch_string|.
58bool IsSwitch(const CommandLine::StringType& string,
59 CommandLine::StringType* switch_string,
60 CommandLine::StringType* switch_value) {
61 switch_string->clear();
62 switch_value->clear();
[email protected]21e342f2012-10-19 06:19:5963 size_t prefix_length = GetSwitchPrefixLength(string);
64 if (prefix_length == 0 || prefix_length == string.length())
[email protected]a40ca4302011-05-14 01:10:2465 return false;
66
67 const size_t equals_position = string.find(kSwitchValueSeparator);
68 *switch_string = string.substr(0, equals_position);
69 if (equals_position != CommandLine::StringType::npos)
70 *switch_value = string.substr(equals_position + 1);
71 return true;
72}
73
74// Append switches and arguments, keeping switches before arguments.
thestig8badc792014-12-04 22:14:2275void AppendSwitchesAndArguments(CommandLine* command_line,
[email protected]a40ca4302011-05-14 01:10:2476 const CommandLine::StringVector& argv) {
77 bool parse_switches = true;
78 for (size_t i = 1; i < argv.size(); ++i) {
79 CommandLine::StringType arg = argv[i];
tfarina023b1dcc2015-12-06 13:25:4180#if defined(OS_WIN)
[email protected]2f3b1cc2014-03-17 23:07:1581 TrimWhitespace(arg, TRIM_ALL, &arg);
tfarina023b1dcc2015-12-06 13:25:4182#else
83 TrimWhitespaceASCII(arg, TRIM_ALL, &arg);
84#endif
[email protected]a40ca4302011-05-14 01:10:2485
86 CommandLine::StringType switch_string;
87 CommandLine::StringType switch_value;
88 parse_switches &= (arg != kSwitchTerminator);
89 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) {
90#if defined(OS_WIN)
thestig8badc792014-12-04 22:14:2291 command_line->AppendSwitchNative(UTF16ToASCII(switch_string),
92 switch_value);
[email protected]a40ca4302011-05-14 01:10:2493#elif defined(OS_POSIX)
thestig8badc792014-12-04 22:14:2294 command_line->AppendSwitchNative(switch_string, switch_value);
[email protected]a40ca4302011-05-14 01:10:2495#endif
96 } else {
thestig8badc792014-12-04 22:14:2297 command_line->AppendArgNative(arg);
[email protected]a40ca4302011-05-14 01:10:2498 }
99 }
100}
101
[email protected]a40ca4302011-05-14 01:10:24102#if defined(OS_WIN)
103// Quote a string as necessary for CommandLineToArgvW compatiblity *on Windows*.
thestig8badc792014-12-04 22:14:22104string16 QuoteForCommandLineToArgvW(const string16& arg,
105 bool quote_placeholders) {
[email protected]0fd23af2011-02-20 06:33:04106 // We follow the quoting rules of CommandLineToArgvW.
107 // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
thestig8badc792014-12-04 22:14:22108 string16 quotable_chars(L" \\\"");
mgiucac974d5102014-10-01 09:24:51109 // We may also be required to quote '%', which is commonly used in a command
110 // line as a placeholder. (It may be substituted for a string with spaces.)
111 if (quote_placeholders)
112 quotable_chars.push_back(L'%');
thestig8badc792014-12-04 22:14:22113 if (arg.find_first_of(quotable_chars) == string16::npos) {
[email protected]0fd23af2011-02-20 06:33:04114 // No quoting necessary.
115 return arg;
116 }
117
thestig8badc792014-12-04 22:14:22118 string16 out;
[email protected]0fd23af2011-02-20 06:33:04119 out.push_back(L'"');
120 for (size_t i = 0; i < arg.size(); ++i) {
121 if (arg[i] == '\\') {
122 // Find the extent of this run of backslashes.
123 size_t start = i, end = start + 1;
thestig8badc792014-12-04 22:14:22124 for (; end < arg.size() && arg[end] == '\\'; ++end) {}
[email protected]0fd23af2011-02-20 06:33:04125 size_t backslash_count = end - start;
126
127 // Backslashes are escapes only if the run is followed by a double quote.
128 // Since we also will end the string with a double quote, we escape for
129 // either a double quote or the end of the string.
130 if (end == arg.size() || arg[end] == '"') {
131 // To quote, we need to output 2x as many backslashes.
132 backslash_count *= 2;
133 }
134 for (size_t j = 0; j < backslash_count; ++j)
135 out.push_back('\\');
136
137 // Advance i to one before the end to balance i++ in loop.
138 i = end - 1;
139 } else if (arg[i] == '"') {
140 out.push_back('\\');
141 out.push_back('"');
142 } else {
143 out.push_back(arg[i]);
144 }
145 }
146 out.push_back('"');
147
148 return out;
149}
[email protected]bb975362009-01-21 01:00:22150#endif
initial.commitd7cae122008-07-26 21:49:38151
[email protected]06cc083a2011-03-01 02:28:42152} // namespace
153
[email protected]a40ca4302011-05-14 01:10:24154CommandLine::CommandLine(NoProgram no_program)
155 : argv_(1),
156 begin_args_(1) {
[email protected]06cc083a2011-03-01 02:28:42157}
158
[email protected]a40ca4302011-05-14 01:10:24159CommandLine::CommandLine(const FilePath& program)
160 : argv_(1),
161 begin_args_(1) {
162 SetProgram(program);
[email protected]06cc083a2011-03-01 02:28:42163}
164
[email protected]a40ca4302011-05-14 01:10:24165CommandLine::CommandLine(int argc, const CommandLine::CharType* const* argv)
166 : argv_(1),
167 begin_args_(1) {
[email protected]06cc083a2011-03-01 02:28:42168 InitFromArgv(argc, argv);
169}
170
[email protected]a40ca4302011-05-14 01:10:24171CommandLine::CommandLine(const StringVector& argv)
172 : argv_(1),
173 begin_args_(1) {
[email protected]06cc083a2011-03-01 02:28:42174 InitFromArgv(argv);
175}
[email protected]06cc083a2011-03-01 02:28:42176
Chris Watkinsbb7211c2017-11-29 07:16:38177CommandLine::CommandLine(const CommandLine& other) = default;
jackhou1bd9da92015-05-21 04:48:00178
Chris Watkinsbb7211c2017-11-29 07:16:38179CommandLine& CommandLine::operator=(const CommandLine& other) = default;
jackhou1bd9da92015-05-21 04:48:00180
Chris Watkinsbb7211c2017-11-29 07:16:38181CommandLine::~CommandLine() = default;
[email protected]acbeb3d2011-03-01 20:47:58182
[email protected]bf98a0e12013-09-25 23:36:00183#if defined(OS_WIN)
184// static
185void CommandLine::set_slash_is_not_a_switch() {
186 // The last switch prefix should be slash, so adjust the size to skip it.
danakj94219a212015-03-09 22:27:25187 DCHECK_EQ(wcscmp(kSwitchPrefixes[arraysize(kSwitchPrefixes) - 1], L"/"), 0);
[email protected]bf98a0e12013-09-25 23:36:00188 switch_prefix_count = arraysize(kSwitchPrefixes) - 1;
189}
anantad936bc12016-06-22 21:40:31190
191// static
192void CommandLine::InitUsingArgvForTesting(int argc, const char* const* argv) {
193 DCHECK(!current_process_commandline_);
194 current_process_commandline_ = new CommandLine(NO_PROGRAM);
195 // On Windows we need to convert the command line arguments to string16.
196 base::CommandLine::StringVector argv_vector;
197 for (int i = 0; i < argc; ++i)
198 argv_vector.push_back(UTF8ToUTF16(argv[i]));
199 current_process_commandline_->InitFromArgv(argv_vector);
200}
[email protected]bf98a0e12013-09-25 23:36:00201#endif
202
[email protected]06cc083a2011-03-01 02:28:42203// static
[email protected]72e2e2422012-02-27 18:38:12204bool CommandLine::Init(int argc, const char* const* argv) {
[email protected]f96fe2c42011-07-13 18:03:34205 if (current_process_commandline_) {
206 // If this is intentional, Reset() must be called first. If we are using
207 // the shared build mode, we have to share a single object across multiple
208 // shared libraries.
[email protected]72e2e2422012-02-27 18:38:12209 return false;
[email protected]f96fe2c42011-07-13 18:03:34210 }
211
[email protected]a40ca4302011-05-14 01:10:24212 current_process_commandline_ = new CommandLine(NO_PROGRAM);
[email protected]06cc083a2011-03-01 02:28:42213#if defined(OS_WIN)
214 current_process_commandline_->ParseFromString(::GetCommandLineW());
215#elif defined(OS_POSIX)
216 current_process_commandline_->InitFromArgv(argc, argv);
217#endif
[email protected]72e2e2422012-02-27 18:38:12218
219 return true;
[email protected]06cc083a2011-03-01 02:28:42220}
221
222// static
223void CommandLine::Reset() {
224 DCHECK(current_process_commandline_);
225 delete current_process_commandline_;
Ivan Kotenkova16212a52017-11-08 12:37:33226 current_process_commandline_ = nullptr;
[email protected]06cc083a2011-03-01 02:28:42227}
228
229// static
230CommandLine* CommandLine::ForCurrentProcess() {
231 DCHECK(current_process_commandline_);
232 return current_process_commandline_;
[email protected]3a3d47472010-07-15 21:03:54233}
234
[email protected]2bf64a92013-07-11 23:10:40235// static
236bool CommandLine::InitializedForCurrentProcess() {
237 return !!current_process_commandline_;
238}
239
[email protected]f3adb5c2008-08-07 20:07:32240#if defined(OS_WIN)
[email protected]06cc083a2011-03-01 02:28:42241// static
thestig8badc792014-12-04 22:14:22242CommandLine CommandLine::FromString(const string16& command_line) {
[email protected]a40ca4302011-05-14 01:10:24243 CommandLine cmd(NO_PROGRAM);
[email protected]06cc083a2011-03-01 02:28:42244 cmd.ParseFromString(command_line);
245 return cmd;
246}
[email protected]a40ca4302011-05-14 01:10:24247#endif
[email protected]06cc083a2011-03-01 02:28:42248
[email protected]a40ca4302011-05-14 01:10:24249void CommandLine::InitFromArgv(int argc,
250 const CommandLine::CharType* const* argv) {
251 StringVector new_argv;
[email protected]06cc083a2011-03-01 02:28:42252 for (int i = 0; i < argc; ++i)
[email protected]a40ca4302011-05-14 01:10:24253 new_argv.push_back(argv[i]);
254 InitFromArgv(new_argv);
[email protected]51343d5a2009-10-26 22:39:33255}
256
[email protected]06cc083a2011-03-01 02:28:42257void CommandLine::InitFromArgv(const StringVector& argv) {
[email protected]a40ca4302011-05-14 01:10:24258 argv_ = StringVector(1);
[email protected]93660ab32013-06-18 08:19:18259 switches_.clear();
[email protected]a40ca4302011-05-14 01:10:24260 begin_args_ = 1;
261 SetProgram(argv.empty() ? FilePath() : FilePath(argv[0]));
thestig8badc792014-12-04 22:14:22262 AppendSwitchesAndArguments(this, argv);
[email protected]06cc083a2011-03-01 02:28:42263}
[email protected]06cc083a2011-03-01 02:28:42264
[email protected]06cc083a2011-03-01 02:28:42265FilePath CommandLine::GetProgram() const {
[email protected]06cc083a2011-03-01 02:28:42266 return FilePath(argv_[0]);
[email protected]a40ca4302011-05-14 01:10:24267}
268
269void CommandLine::SetProgram(const FilePath& program) {
tfarina023b1dcc2015-12-06 13:25:41270#if defined(OS_WIN)
[email protected]2f3b1cc2014-03-17 23:07:15271 TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]);
tfarina023b1dcc2015-12-06 13:25:41272#else
273 TrimWhitespaceASCII(program.value(), TRIM_ALL, &argv_[0]);
274#endif
[email protected]06cc083a2011-03-01 02:28:42275}
276
jackhou1bd9da92015-05-21 04:48:00277bool CommandLine::HasSwitch(const base::StringPiece& switch_string) const {
brettwfce8d192015-08-10 19:07:51278 DCHECK_EQ(ToLowerASCII(switch_string), switch_string);
Jeremy Roman863386d2017-10-31 19:25:38279 return ContainsKey(switches_, switch_string);
[email protected]06cc083a2011-03-01 02:28:42280}
281
jackhou1bd9da92015-05-21 04:48:00282bool CommandLine::HasSwitch(const char switch_constant[]) const {
283 return HasSwitch(base::StringPiece(switch_constant));
tapted009a1dc82015-03-30 03:57:10284}
285
[email protected]06cc083a2011-03-01 02:28:42286std::string CommandLine::GetSwitchValueASCII(
jackhou1bd9da92015-05-21 04:48:00287 const base::StringPiece& switch_string) const {
[email protected]a40ca4302011-05-14 01:10:24288 StringType value = GetSwitchValueNative(switch_string);
[email protected]bd6fc2f2014-03-17 23:55:43289 if (!IsStringASCII(value)) {
[email protected]a42d4632011-10-26 21:48:00290 DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII.";
291 return std::string();
[email protected]06cc083a2011-03-01 02:28:42292 }
293#if defined(OS_WIN)
[email protected]2f3b1cc2014-03-17 23:07:15294 return UTF16ToASCII(value);
[email protected]06cc083a2011-03-01 02:28:42295#else
296 return value;
297#endif
298}
299
300FilePath CommandLine::GetSwitchValuePath(
jackhou1bd9da92015-05-21 04:48:00301 const base::StringPiece& switch_string) const {
[email protected]06cc083a2011-03-01 02:28:42302 return FilePath(GetSwitchValueNative(switch_string));
303}
304
305CommandLine::StringType CommandLine::GetSwitchValueNative(
jackhou1bd9da92015-05-21 04:48:00306 const base::StringPiece& switch_string) const {
brettwfce8d192015-08-10 19:07:51307 DCHECK_EQ(ToLowerASCII(switch_string), switch_string);
Jeremy Roman863386d2017-10-31 19:25:38308 auto result = switches_.find(switch_string);
309 return result == switches_.end() ? StringType() : result->second;
[email protected]06cc083a2011-03-01 02:28:42310}
311
[email protected]06cc083a2011-03-01 02:28:42312void CommandLine::AppendSwitch(const std::string& switch_string) {
[email protected]a40ca4302011-05-14 01:10:24313 AppendSwitchNative(switch_string, StringType());
[email protected]06cc083a2011-03-01 02:28:42314}
315
316void CommandLine::AppendSwitchPath(const std::string& switch_string,
317 const FilePath& path) {
318 AppendSwitchNative(switch_string, path.value());
319}
320
321void CommandLine::AppendSwitchNative(const std::string& switch_string,
322 const CommandLine::StringType& value) {
323#if defined(OS_WIN)
brettwfce8d192015-08-10 19:07:51324 const std::string switch_key = ToLowerASCII(switch_string);
thestig8badc792014-12-04 22:14:22325 StringType combined_switch_string(ASCIIToUTF16(switch_key));
[email protected]06cc083a2011-03-01 02:28:42326#elif defined(OS_POSIX)
jackhou1bd9da92015-05-21 04:48:00327 const std::string& switch_key = switch_string;
328 StringType combined_switch_string(switch_key);
[email protected]a40ca4302011-05-14 01:10:24329#endif
330 size_t prefix_length = GetSwitchPrefixLength(combined_switch_string);
jackhou1bd9da92015-05-21 04:48:00331 auto insertion =
332 switches_.insert(make_pair(switch_key.substr(prefix_length), value));
333 if (!insertion.second)
334 insertion.first->second = value;
[email protected]a40ca4302011-05-14 01:10:24335 // Preserve existing switch prefixes in |argv_|; only append one if necessary.
336 if (prefix_length == 0)
337 combined_switch_string = kSwitchPrefixes[0] + combined_switch_string;
[email protected]06cc083a2011-03-01 02:28:42338 if (!value.empty())
339 combined_switch_string += kSwitchValueSeparator + value;
[email protected]a40ca4302011-05-14 01:10:24340 // Append the switch and update the switches/arguments divider |begin_args_|.
341 argv_.insert(argv_.begin() + begin_args_++, combined_switch_string);
[email protected]06cc083a2011-03-01 02:28:42342}
343
344void CommandLine::AppendSwitchASCII(const std::string& switch_string,
345 const std::string& value_string) {
346#if defined(OS_WIN)
thestig8badc792014-12-04 22:14:22347 AppendSwitchNative(switch_string, ASCIIToUTF16(value_string));
[email protected]06cc083a2011-03-01 02:28:42348#elif defined(OS_POSIX)
349 AppendSwitchNative(switch_string, value_string);
350#endif
351}
352
[email protected]06cc083a2011-03-01 02:28:42353void CommandLine::CopySwitchesFrom(const CommandLine& source,
354 const char* const switches[],
355 size_t count) {
356 for (size_t i = 0; i < count; ++i) {
[email protected]a40ca4302011-05-14 01:10:24357 if (source.HasSwitch(switches[i]))
358 AppendSwitchNative(switches[i], source.GetSwitchValueNative(switches[i]));
[email protected]06cc083a2011-03-01 02:28:42359 }
360}
361
[email protected]75f1c782011-07-13 23:41:22362CommandLine::StringVector CommandLine::GetArgs() const {
[email protected]a40ca4302011-05-14 01:10:24363 // Gather all arguments after the last switch (may include kSwitchTerminator).
364 StringVector args(argv_.begin() + begin_args_, argv_.end());
365 // Erase only the first kSwitchTerminator (maybe "--" is a legitimate page?)
366 StringVector::iterator switch_terminator =
367 std::find(args.begin(), args.end(), kSwitchTerminator);
368 if (switch_terminator != args.end())
369 args.erase(switch_terminator);
370 return args;
371}
372
[email protected]06cc083a2011-03-01 02:28:42373void CommandLine::AppendArg(const std::string& value) {
374#if defined(OS_WIN)
[email protected]bd6fc2f2014-03-17 23:55:43375 DCHECK(IsStringUTF8(value));
376 AppendArgNative(UTF8ToWide(value));
[email protected]06cc083a2011-03-01 02:28:42377#elif defined(OS_POSIX)
378 AppendArgNative(value);
379#endif
380}
381
382void CommandLine::AppendArgPath(const FilePath& path) {
383 AppendArgNative(path.value());
384}
385
386void CommandLine::AppendArgNative(const CommandLine::StringType& value) {
[email protected]06cc083a2011-03-01 02:28:42387 argv_.push_back(value);
[email protected]06cc083a2011-03-01 02:28:42388}
389
390void CommandLine::AppendArguments(const CommandLine& other,
391 bool include_program) {
[email protected]06cc083a2011-03-01 02:28:42392 if (include_program)
[email protected]a40ca4302011-05-14 01:10:24393 SetProgram(other.GetProgram());
thestig8badc792014-12-04 22:14:22394 AppendSwitchesAndArguments(this, other.argv());
[email protected]06cc083a2011-03-01 02:28:42395}
396
397void CommandLine::PrependWrapper(const CommandLine::StringType& wrapper) {
[email protected]06cc083a2011-03-01 02:28:42398 if (wrapper.empty())
399 return;
skyostild851aa12017-03-29 17:38:35400 // Split the wrapper command based on whitespace (with quoting).
401 using CommandLineTokenizer =
402 StringTokenizerT<StringType, StringType::const_iterator>;
403 CommandLineTokenizer tokenizer(wrapper, FILE_PATH_LITERAL(" "));
404 tokenizer.set_quote_chars(FILE_PATH_LITERAL("'\""));
405 std::vector<StringType> wrapper_argv;
406 while (tokenizer.GetNext())
407 wrapper_argv.emplace_back(tokenizer.token());
408
[email protected]a40ca4302011-05-14 01:10:24409 // Prepend the wrapper and update the switches/arguments |begin_args_|.
410 argv_.insert(argv_.begin(), wrapper_argv.begin(), wrapper_argv.end());
411 begin_args_ += wrapper_argv.size();
[email protected]06cc083a2011-03-01 02:28:42412}
413
414#if defined(OS_WIN)
thestig8badc792014-12-04 22:14:22415void CommandLine::ParseFromString(const string16& command_line) {
416 string16 command_line_string;
[email protected]2f3b1cc2014-03-17 23:07:15417 TrimWhitespace(command_line, TRIM_ALL, &command_line_string);
[email protected]a40ca4302011-05-14 01:10:24418 if (command_line_string.empty())
[email protected]bb975362009-01-21 01:00:22419 return;
420
421 int num_args = 0;
422 wchar_t** args = NULL;
[email protected]a40ca4302011-05-14 01:10:24423 args = ::CommandLineToArgvW(command_line_string.c_str(), &num_args);
[email protected]bb975362009-01-21 01:00:22424
[email protected]a42d4632011-10-26 21:48:00425 DPLOG_IF(FATAL, !args) << "CommandLineToArgvW failed on command line: "
[email protected]2f3b1cc2014-03-17 23:07:15426 << UTF16ToUTF8(command_line);
[email protected]a40ca4302011-05-14 01:10:24427 InitFromArgv(num_args, args);
428 LocalFree(args);
[email protected]bb975362009-01-21 01:00:22429}
[email protected]f3adb5c2008-08-07 20:07:32430#endif
[email protected]2f3b1cc2014-03-17 23:07:15431
mgiucac974d5102014-10-01 09:24:51432CommandLine::StringType CommandLine::GetCommandLineStringInternal(
433 bool quote_placeholders) const {
434 StringType string(argv_[0]);
435#if defined(OS_WIN)
436 string = QuoteForCommandLineToArgvW(string, quote_placeholders);
437#endif
438 StringType params(GetArgumentsStringInternal(quote_placeholders));
439 if (!params.empty()) {
440 string.append(StringType(FILE_PATH_LITERAL(" ")));
441 string.append(params);
442 }
443 return string;
444}
445
446CommandLine::StringType CommandLine::GetArgumentsStringInternal(
447 bool quote_placeholders) const {
448 StringType params;
449 // Append switches and arguments.
450 bool parse_switches = true;
451 for (size_t i = 1; i < argv_.size(); ++i) {
452 StringType arg = argv_[i];
453 StringType switch_string;
454 StringType switch_value;
455 parse_switches &= arg != kSwitchTerminator;
456 if (i > 1)
457 params.append(StringType(FILE_PATH_LITERAL(" ")));
458 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) {
459 params.append(switch_string);
460 if (!switch_value.empty()) {
461#if defined(OS_WIN)
462 switch_value =
463 QuoteForCommandLineToArgvW(switch_value, quote_placeholders);
464#endif
465 params.append(kSwitchValueSeparator + switch_value);
466 }
thestig8badc792014-12-04 22:14:22467 } else {
mgiucac974d5102014-10-01 09:24:51468#if defined(OS_WIN)
469 arg = QuoteForCommandLineToArgvW(arg, quote_placeholders);
470#endif
471 params.append(arg);
472 }
473 }
474 return params;
475}
476
[email protected]2f3b1cc2014-03-17 23:07:15477} // namespace base