blob: 873da813483651bc9e74b0510e17fa925225b5bd [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"
skyostild851aa12017-03-29 17:38:3514#include "base/strings/string_tokenizer.h"
[email protected]251cd6e52013-06-11 13:36:3715#include "base/strings/string_util.h"
[email protected]a4ea1f12013-06-07 18:37:0716#include "base/strings/utf_string_conversions.h"
[email protected]74e9fa22010-12-29 21:06:4317#include "build/build_config.h"
initial.commitd7cae122008-07-26 21:49:3818
[email protected]74e9fa22010-12-29 21:06:4319#if defined(OS_WIN)
20#include <windows.h>
21#include <shellapi.h>
[email protected]7f113f32009-09-10 18:02:1722#endif
23
[email protected]2f3b1cc2014-03-17 23:07:1524namespace base {
[email protected]04af979a2013-02-16 04:12:2625
[email protected]bb975362009-01-21 01:00:2226CommandLine* CommandLine::current_process_commandline_ = NULL;
initial.commitd7cae122008-07-26 21:49:3827
[email protected]06cc083a2011-03-01 02:28:4228namespace {
[email protected]2f3b1cc2014-03-17 23:07:1529
[email protected]a40ca4302011-05-14 01:10:2430const CommandLine::CharType kSwitchTerminator[] = FILE_PATH_LITERAL("--");
31const CommandLine::CharType kSwitchValueSeparator[] = FILE_PATH_LITERAL("=");
[email protected]bf98a0e12013-09-25 23:36:0032
[email protected]06cc083a2011-03-01 02:28:4233// Since we use a lazy match, make sure that longer versions (like "--") are
34// listed before shorter versions (like "-") of similar prefixes.
[email protected]5d426332008-08-08 20:46:2135#if defined(OS_WIN)
[email protected]bf98a0e12013-09-25 23:36:0036// By putting slash last, we can control whether it is treaded as a switch
37// value by changing the value of switch_prefix_count to be one less than
38// the array size.
[email protected]a40ca4302011-05-14 01:10:2439const CommandLine::CharType* const kSwitchPrefixes[] = {L"--", L"-", L"/"};
[email protected]5d426332008-08-08 20:46:2140#elif defined(OS_POSIX)
[email protected]1a48f312008-08-12 01:14:3741// Unixes don't use slash as a switch.
[email protected]a40ca4302011-05-14 01:10:2442const CommandLine::CharType* const kSwitchPrefixes[] = {"--", "-"};
[email protected]5d426332008-08-08 20:46:2143#endif
[email protected]bf98a0e12013-09-25 23:36:0044size_t switch_prefix_count = arraysize(kSwitchPrefixes);
initial.commitd7cae122008-07-26 21:49:3845
[email protected]a40ca4302011-05-14 01:10:2446size_t GetSwitchPrefixLength(const CommandLine::StringType& string) {
[email protected]bf98a0e12013-09-25 23:36:0047 for (size_t i = 0; i < switch_prefix_count; ++i) {
[email protected]a40ca4302011-05-14 01:10:2448 CommandLine::StringType prefix(kSwitchPrefixes[i]);
[email protected]1fa39f02011-09-13 15:45:3449 if (string.compare(0, prefix.length(), prefix) == 0)
[email protected]a40ca4302011-05-14 01:10:2450 return prefix.length();
51 }
52 return 0;
initial.commitd7cae122008-07-26 21:49:3853}
[email protected]0fd23af2011-02-20 06:33:0454
[email protected]a40ca4302011-05-14 01:10:2455// Fills in |switch_string| and |switch_value| if |string| is a switch.
56// This will preserve the input switch prefix in the output |switch_string|.
57bool IsSwitch(const CommandLine::StringType& string,
58 CommandLine::StringType* switch_string,
59 CommandLine::StringType* switch_value) {
60 switch_string->clear();
61 switch_value->clear();
[email protected]21e342f2012-10-19 06:19:5962 size_t prefix_length = GetSwitchPrefixLength(string);
63 if (prefix_length == 0 || prefix_length == string.length())
[email protected]a40ca4302011-05-14 01:10:2464 return false;
65
66 const size_t equals_position = string.find(kSwitchValueSeparator);
67 *switch_string = string.substr(0, equals_position);
68 if (equals_position != CommandLine::StringType::npos)
69 *switch_value = string.substr(equals_position + 1);
70 return true;
71}
72
73// Append switches and arguments, keeping switches before arguments.
thestig8badc792014-12-04 22:14:2274void AppendSwitchesAndArguments(CommandLine* command_line,
[email protected]a40ca4302011-05-14 01:10:2475 const CommandLine::StringVector& argv) {
76 bool parse_switches = true;
77 for (size_t i = 1; i < argv.size(); ++i) {
78 CommandLine::StringType arg = argv[i];
tfarina023b1dcc2015-12-06 13:25:4179#if defined(OS_WIN)
[email protected]2f3b1cc2014-03-17 23:07:1580 TrimWhitespace(arg, TRIM_ALL, &arg);
tfarina023b1dcc2015-12-06 13:25:4181#else
82 TrimWhitespaceASCII(arg, TRIM_ALL, &arg);
83#endif
[email protected]a40ca4302011-05-14 01:10:2484
85 CommandLine::StringType switch_string;
86 CommandLine::StringType switch_value;
87 parse_switches &= (arg != kSwitchTerminator);
88 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) {
89#if defined(OS_WIN)
thestig8badc792014-12-04 22:14:2290 command_line->AppendSwitchNative(UTF16ToASCII(switch_string),
91 switch_value);
[email protected]a40ca4302011-05-14 01:10:2492#elif defined(OS_POSIX)
thestig8badc792014-12-04 22:14:2293 command_line->AppendSwitchNative(switch_string, switch_value);
[email protected]a40ca4302011-05-14 01:10:2494#endif
95 } else {
thestig8badc792014-12-04 22:14:2296 command_line->AppendArgNative(arg);
[email protected]a40ca4302011-05-14 01:10:2497 }
98 }
99}
100
[email protected]a40ca4302011-05-14 01:10:24101#if defined(OS_WIN)
102// Quote a string as necessary for CommandLineToArgvW compatiblity *on Windows*.
thestig8badc792014-12-04 22:14:22103string16 QuoteForCommandLineToArgvW(const string16& arg,
104 bool quote_placeholders) {
[email protected]0fd23af2011-02-20 06:33:04105 // We follow the quoting rules of CommandLineToArgvW.
106 // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
thestig8badc792014-12-04 22:14:22107 string16 quotable_chars(L" \\\"");
mgiucac974d5102014-10-01 09:24:51108 // We may also be required to quote '%', which is commonly used in a command
109 // line as a placeholder. (It may be substituted for a string with spaces.)
110 if (quote_placeholders)
111 quotable_chars.push_back(L'%');
thestig8badc792014-12-04 22:14:22112 if (arg.find_first_of(quotable_chars) == string16::npos) {
[email protected]0fd23af2011-02-20 06:33:04113 // No quoting necessary.
114 return arg;
115 }
116
thestig8badc792014-12-04 22:14:22117 string16 out;
[email protected]0fd23af2011-02-20 06:33:04118 out.push_back(L'"');
119 for (size_t i = 0; i < arg.size(); ++i) {
120 if (arg[i] == '\\') {
121 // Find the extent of this run of backslashes.
122 size_t start = i, end = start + 1;
thestig8badc792014-12-04 22:14:22123 for (; end < arg.size() && arg[end] == '\\'; ++end) {}
[email protected]0fd23af2011-02-20 06:33:04124 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}
[email protected]bb975362009-01-21 01:00:22149#endif
initial.commitd7cae122008-07-26 21:49:38150
[email protected]06cc083a2011-03-01 02:28:42151} // namespace
152
[email protected]a40ca4302011-05-14 01:10:24153CommandLine::CommandLine(NoProgram no_program)
154 : argv_(1),
155 begin_args_(1) {
[email protected]06cc083a2011-03-01 02:28:42156}
157
[email protected]a40ca4302011-05-14 01:10:24158CommandLine::CommandLine(const FilePath& program)
159 : argv_(1),
160 begin_args_(1) {
161 SetProgram(program);
[email protected]06cc083a2011-03-01 02:28:42162}
163
[email protected]a40ca4302011-05-14 01:10:24164CommandLine::CommandLine(int argc, const CommandLine::CharType* const* argv)
165 : argv_(1),
166 begin_args_(1) {
[email protected]06cc083a2011-03-01 02:28:42167 InitFromArgv(argc, argv);
168}
169
[email protected]a40ca4302011-05-14 01:10:24170CommandLine::CommandLine(const StringVector& argv)
171 : argv_(1),
172 begin_args_(1) {
[email protected]06cc083a2011-03-01 02:28:42173 InitFromArgv(argv);
174}
[email protected]06cc083a2011-03-01 02:28:42175
jackhou1bd9da92015-05-21 04:48:00176CommandLine::CommandLine(const CommandLine& other)
177 : argv_(other.argv_),
178 switches_(other.switches_),
179 begin_args_(other.begin_args_) {
180 ResetStringPieces();
181}
182
183CommandLine& CommandLine::operator=(const CommandLine& other) {
184 argv_ = other.argv_;
185 switches_ = other.switches_;
186 begin_args_ = other.begin_args_;
187 ResetStringPieces();
188 return *this;
189}
190
[email protected]acbeb3d2011-03-01 20:47:58191CommandLine::~CommandLine() {
192}
193
[email protected]bf98a0e12013-09-25 23:36:00194#if defined(OS_WIN)
195// static
196void CommandLine::set_slash_is_not_a_switch() {
197 // The last switch prefix should be slash, so adjust the size to skip it.
danakj94219a212015-03-09 22:27:25198 DCHECK_EQ(wcscmp(kSwitchPrefixes[arraysize(kSwitchPrefixes) - 1], L"/"), 0);
[email protected]bf98a0e12013-09-25 23:36:00199 switch_prefix_count = arraysize(kSwitchPrefixes) - 1;
200}
anantad936bc12016-06-22 21:40:31201
202// static
203void CommandLine::InitUsingArgvForTesting(int argc, const char* const* argv) {
204 DCHECK(!current_process_commandline_);
205 current_process_commandline_ = new CommandLine(NO_PROGRAM);
206 // On Windows we need to convert the command line arguments to string16.
207 base::CommandLine::StringVector argv_vector;
208 for (int i = 0; i < argc; ++i)
209 argv_vector.push_back(UTF8ToUTF16(argv[i]));
210 current_process_commandline_->InitFromArgv(argv_vector);
211}
[email protected]bf98a0e12013-09-25 23:36:00212#endif
213
[email protected]06cc083a2011-03-01 02:28:42214// static
[email protected]72e2e2422012-02-27 18:38:12215bool CommandLine::Init(int argc, const char* const* argv) {
[email protected]f96fe2c42011-07-13 18:03:34216 if (current_process_commandline_) {
217 // If this is intentional, Reset() must be called first. If we are using
218 // the shared build mode, we have to share a single object across multiple
219 // shared libraries.
[email protected]72e2e2422012-02-27 18:38:12220 return false;
[email protected]f96fe2c42011-07-13 18:03:34221 }
222
[email protected]a40ca4302011-05-14 01:10:24223 current_process_commandline_ = new CommandLine(NO_PROGRAM);
[email protected]06cc083a2011-03-01 02:28:42224#if defined(OS_WIN)
225 current_process_commandline_->ParseFromString(::GetCommandLineW());
226#elif defined(OS_POSIX)
227 current_process_commandline_->InitFromArgv(argc, argv);
228#endif
[email protected]72e2e2422012-02-27 18:38:12229
230 return true;
[email protected]06cc083a2011-03-01 02:28:42231}
232
233// static
234void CommandLine::Reset() {
235 DCHECK(current_process_commandline_);
236 delete current_process_commandline_;
237 current_process_commandline_ = NULL;
238}
239
240// static
241CommandLine* CommandLine::ForCurrentProcess() {
242 DCHECK(current_process_commandline_);
243 return current_process_commandline_;
[email protected]3a3d47472010-07-15 21:03:54244}
245
[email protected]2bf64a92013-07-11 23:10:40246// static
247bool CommandLine::InitializedForCurrentProcess() {
248 return !!current_process_commandline_;
249}
250
[email protected]f3adb5c2008-08-07 20:07:32251#if defined(OS_WIN)
[email protected]06cc083a2011-03-01 02:28:42252// static
thestig8badc792014-12-04 22:14:22253CommandLine CommandLine::FromString(const string16& command_line) {
[email protected]a40ca4302011-05-14 01:10:24254 CommandLine cmd(NO_PROGRAM);
[email protected]06cc083a2011-03-01 02:28:42255 cmd.ParseFromString(command_line);
256 return cmd;
257}
[email protected]a40ca4302011-05-14 01:10:24258#endif
[email protected]06cc083a2011-03-01 02:28:42259
[email protected]a40ca4302011-05-14 01:10:24260void CommandLine::InitFromArgv(int argc,
261 const CommandLine::CharType* const* argv) {
262 StringVector new_argv;
[email protected]06cc083a2011-03-01 02:28:42263 for (int i = 0; i < argc; ++i)
[email protected]a40ca4302011-05-14 01:10:24264 new_argv.push_back(argv[i]);
265 InitFromArgv(new_argv);
[email protected]51343d5a2009-10-26 22:39:33266}
267
[email protected]06cc083a2011-03-01 02:28:42268void CommandLine::InitFromArgv(const StringVector& argv) {
[email protected]a40ca4302011-05-14 01:10:24269 argv_ = StringVector(1);
[email protected]93660ab32013-06-18 08:19:18270 switches_.clear();
jackhou1bd9da92015-05-21 04:48:00271 switches_by_stringpiece_.clear();
[email protected]a40ca4302011-05-14 01:10:24272 begin_args_ = 1;
273 SetProgram(argv.empty() ? FilePath() : FilePath(argv[0]));
thestig8badc792014-12-04 22:14:22274 AppendSwitchesAndArguments(this, argv);
[email protected]06cc083a2011-03-01 02:28:42275}
[email protected]06cc083a2011-03-01 02:28:42276
[email protected]06cc083a2011-03-01 02:28:42277FilePath CommandLine::GetProgram() const {
[email protected]06cc083a2011-03-01 02:28:42278 return FilePath(argv_[0]);
[email protected]a40ca4302011-05-14 01:10:24279}
280
281void CommandLine::SetProgram(const FilePath& program) {
tfarina023b1dcc2015-12-06 13:25:41282#if defined(OS_WIN)
[email protected]2f3b1cc2014-03-17 23:07:15283 TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]);
tfarina023b1dcc2015-12-06 13:25:41284#else
285 TrimWhitespaceASCII(program.value(), TRIM_ALL, &argv_[0]);
286#endif
[email protected]06cc083a2011-03-01 02:28:42287}
288
jackhou1bd9da92015-05-21 04:48:00289bool CommandLine::HasSwitch(const base::StringPiece& switch_string) const {
brettwfce8d192015-08-10 19:07:51290 DCHECK_EQ(ToLowerASCII(switch_string), switch_string);
jackhou1bd9da92015-05-21 04:48:00291 return switches_by_stringpiece_.find(switch_string) !=
292 switches_by_stringpiece_.end();
[email protected]06cc083a2011-03-01 02:28:42293}
294
jackhou1bd9da92015-05-21 04:48:00295bool CommandLine::HasSwitch(const char switch_constant[]) const {
296 return HasSwitch(base::StringPiece(switch_constant));
tapted009a1dc82015-03-30 03:57:10297}
298
[email protected]06cc083a2011-03-01 02:28:42299std::string CommandLine::GetSwitchValueASCII(
jackhou1bd9da92015-05-21 04:48:00300 const base::StringPiece& switch_string) const {
[email protected]a40ca4302011-05-14 01:10:24301 StringType value = GetSwitchValueNative(switch_string);
[email protected]bd6fc2f2014-03-17 23:55:43302 if (!IsStringASCII(value)) {
[email protected]a42d4632011-10-26 21:48:00303 DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII.";
304 return std::string();
[email protected]06cc083a2011-03-01 02:28:42305 }
306#if defined(OS_WIN)
[email protected]2f3b1cc2014-03-17 23:07:15307 return UTF16ToASCII(value);
[email protected]06cc083a2011-03-01 02:28:42308#else
309 return value;
310#endif
311}
312
313FilePath CommandLine::GetSwitchValuePath(
jackhou1bd9da92015-05-21 04:48:00314 const base::StringPiece& switch_string) const {
[email protected]06cc083a2011-03-01 02:28:42315 return FilePath(GetSwitchValueNative(switch_string));
316}
317
318CommandLine::StringType CommandLine::GetSwitchValueNative(
jackhou1bd9da92015-05-21 04:48:00319 const base::StringPiece& switch_string) const {
brettwfce8d192015-08-10 19:07:51320 DCHECK_EQ(ToLowerASCII(switch_string), switch_string);
jackhou1bd9da92015-05-21 04:48:00321 auto result = switches_by_stringpiece_.find(switch_string);
322 return result == switches_by_stringpiece_.end() ? StringType()
323 : *(result->second);
[email protected]06cc083a2011-03-01 02:28:42324}
325
[email protected]06cc083a2011-03-01 02:28:42326void CommandLine::AppendSwitch(const std::string& switch_string) {
[email protected]a40ca4302011-05-14 01:10:24327 AppendSwitchNative(switch_string, StringType());
[email protected]06cc083a2011-03-01 02:28:42328}
329
330void CommandLine::AppendSwitchPath(const std::string& switch_string,
331 const FilePath& path) {
332 AppendSwitchNative(switch_string, path.value());
333}
334
335void CommandLine::AppendSwitchNative(const std::string& switch_string,
336 const CommandLine::StringType& value) {
337#if defined(OS_WIN)
brettwfce8d192015-08-10 19:07:51338 const std::string switch_key = ToLowerASCII(switch_string);
thestig8badc792014-12-04 22:14:22339 StringType combined_switch_string(ASCIIToUTF16(switch_key));
[email protected]06cc083a2011-03-01 02:28:42340#elif defined(OS_POSIX)
jackhou1bd9da92015-05-21 04:48:00341 const std::string& switch_key = switch_string;
342 StringType combined_switch_string(switch_key);
[email protected]a40ca4302011-05-14 01:10:24343#endif
344 size_t prefix_length = GetSwitchPrefixLength(combined_switch_string);
jackhou1bd9da92015-05-21 04:48:00345 auto insertion =
346 switches_.insert(make_pair(switch_key.substr(prefix_length), value));
347 if (!insertion.second)
348 insertion.first->second = value;
349 switches_by_stringpiece_[insertion.first->first] = &(insertion.first->second);
[email protected]a40ca4302011-05-14 01:10:24350 // 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;
[email protected]06cc083a2011-03-01 02:28:42353 if (!value.empty())
354 combined_switch_string += kSwitchValueSeparator + value;
[email protected]a40ca4302011-05-14 01:10:24355 // Append the switch and update the switches/arguments divider |begin_args_|.
356 argv_.insert(argv_.begin() + begin_args_++, combined_switch_string);
[email protected]06cc083a2011-03-01 02:28:42357}
358
359void CommandLine::AppendSwitchASCII(const std::string& switch_string,
360 const std::string& value_string) {
361#if defined(OS_WIN)
thestig8badc792014-12-04 22:14:22362 AppendSwitchNative(switch_string, ASCIIToUTF16(value_string));
[email protected]06cc083a2011-03-01 02:28:42363#elif defined(OS_POSIX)
364 AppendSwitchNative(switch_string, value_string);
365#endif
366}
367
[email protected]06cc083a2011-03-01 02:28:42368void CommandLine::CopySwitchesFrom(const CommandLine& source,
369 const char* const switches[],
370 size_t count) {
371 for (size_t i = 0; i < count; ++i) {
[email protected]a40ca4302011-05-14 01:10:24372 if (source.HasSwitch(switches[i]))
373 AppendSwitchNative(switches[i], source.GetSwitchValueNative(switches[i]));
[email protected]06cc083a2011-03-01 02:28:42374 }
375}
376
[email protected]75f1c782011-07-13 23:41:22377CommandLine::StringVector CommandLine::GetArgs() const {
[email protected]a40ca4302011-05-14 01:10:24378 // 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
[email protected]06cc083a2011-03-01 02:28:42388void CommandLine::AppendArg(const std::string& value) {
389#if defined(OS_WIN)
[email protected]bd6fc2f2014-03-17 23:55:43390 DCHECK(IsStringUTF8(value));
391 AppendArgNative(UTF8ToWide(value));
[email protected]06cc083a2011-03-01 02:28:42392#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) {
[email protected]06cc083a2011-03-01 02:28:42402 argv_.push_back(value);
[email protected]06cc083a2011-03-01 02:28:42403}
404
405void CommandLine::AppendArguments(const CommandLine& other,
406 bool include_program) {
[email protected]06cc083a2011-03-01 02:28:42407 if (include_program)
[email protected]a40ca4302011-05-14 01:10:24408 SetProgram(other.GetProgram());
thestig8badc792014-12-04 22:14:22409 AppendSwitchesAndArguments(this, other.argv());
[email protected]06cc083a2011-03-01 02:28:42410}
411
412void CommandLine::PrependWrapper(const CommandLine::StringType& wrapper) {
[email protected]06cc083a2011-03-01 02:28:42413 if (wrapper.empty())
414 return;
skyostild851aa12017-03-29 17:38:35415 // Split the wrapper command based on whitespace (with quoting).
416 using CommandLineTokenizer =
417 StringTokenizerT<StringType, StringType::const_iterator>;
418 CommandLineTokenizer tokenizer(wrapper, FILE_PATH_LITERAL(" "));
419 tokenizer.set_quote_chars(FILE_PATH_LITERAL("'\""));
420 std::vector<StringType> wrapper_argv;
421 while (tokenizer.GetNext())
422 wrapper_argv.emplace_back(tokenizer.token());
423
[email protected]a40ca4302011-05-14 01:10:24424 // Prepend the wrapper and update the switches/arguments |begin_args_|.
425 argv_.insert(argv_.begin(), wrapper_argv.begin(), wrapper_argv.end());
426 begin_args_ += wrapper_argv.size();
[email protected]06cc083a2011-03-01 02:28:42427}
428
429#if defined(OS_WIN)
thestig8badc792014-12-04 22:14:22430void CommandLine::ParseFromString(const string16& command_line) {
431 string16 command_line_string;
[email protected]2f3b1cc2014-03-17 23:07:15432 TrimWhitespace(command_line, TRIM_ALL, &command_line_string);
[email protected]a40ca4302011-05-14 01:10:24433 if (command_line_string.empty())
[email protected]bb975362009-01-21 01:00:22434 return;
435
436 int num_args = 0;
437 wchar_t** args = NULL;
[email protected]a40ca4302011-05-14 01:10:24438 args = ::CommandLineToArgvW(command_line_string.c_str(), &num_args);
[email protected]bb975362009-01-21 01:00:22439
[email protected]a42d4632011-10-26 21:48:00440 DPLOG_IF(FATAL, !args) << "CommandLineToArgvW failed on command line: "
[email protected]2f3b1cc2014-03-17 23:07:15441 << UTF16ToUTF8(command_line);
[email protected]a40ca4302011-05-14 01:10:24442 InitFromArgv(num_args, args);
443 LocalFree(args);
[email protected]bb975362009-01-21 01:00:22444}
[email protected]f3adb5c2008-08-07 20:07:32445#endif
[email protected]2f3b1cc2014-03-17 23:07:15446
mgiucac974d5102014-10-01 09:24:51447CommandLine::StringType CommandLine::GetCommandLineStringInternal(
448 bool quote_placeholders) const {
449 StringType string(argv_[0]);
450#if defined(OS_WIN)
451 string = QuoteForCommandLineToArgvW(string, quote_placeholders);
452#endif
453 StringType params(GetArgumentsStringInternal(quote_placeholders));
454 if (!params.empty()) {
455 string.append(StringType(FILE_PATH_LITERAL(" ")));
456 string.append(params);
457 }
458 return string;
459}
460
461CommandLine::StringType CommandLine::GetArgumentsStringInternal(
462 bool quote_placeholders) const {
463 StringType params;
464 // Append switches and arguments.
465 bool parse_switches = true;
466 for (size_t i = 1; i < argv_.size(); ++i) {
467 StringType arg = argv_[i];
468 StringType switch_string;
469 StringType switch_value;
470 parse_switches &= arg != kSwitchTerminator;
471 if (i > 1)
472 params.append(StringType(FILE_PATH_LITERAL(" ")));
473 if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) {
474 params.append(switch_string);
475 if (!switch_value.empty()) {
476#if defined(OS_WIN)
477 switch_value =
478 QuoteForCommandLineToArgvW(switch_value, quote_placeholders);
479#endif
480 params.append(kSwitchValueSeparator + switch_value);
481 }
thestig8badc792014-12-04 22:14:22482 } else {
mgiucac974d5102014-10-01 09:24:51483#if defined(OS_WIN)
484 arg = QuoteForCommandLineToArgvW(arg, quote_placeholders);
485#endif
486 params.append(arg);
487 }
488 }
489 return params;
490}
491
jackhou1bd9da92015-05-21 04:48:00492void CommandLine::ResetStringPieces() {
493 switches_by_stringpiece_.clear();
494 for (const auto& entry : switches_)
495 switches_by_stringpiece_[entry.first] = &(entry.second);
496}
497
[email protected]2f3b1cc2014-03-17 23:07:15498} // namespace base