OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <windows.h> | 8 #include <windows.h> |
9 #include <shellapi.h> | 9 #include <shellapi.h> |
10 #elif defined(OS_POSIX) | 10 #elif defined(OS_POSIX) |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 switches_[switch_string] = switch_value; | 97 switches_[switch_string] = switch_value; |
98 } else { | 98 } else { |
99 args_.push_back(arg); | 99 args_.push_back(arg); |
100 } | 100 } |
101 } | 101 } |
102 | 102 |
103 if (args) | 103 if (args) |
104 LocalFree(args); | 104 LocalFree(args); |
105 } | 105 } |
106 | 106 |
| 107 // static |
| 108 CommandLine CommandLine::FromString(const std::wstring& command_line) { |
| 109 CommandLine cmd; |
| 110 cmd.ParseFromString(command_line); |
| 111 return cmd; |
| 112 } |
| 113 |
107 CommandLine::CommandLine(const FilePath& program) { | 114 CommandLine::CommandLine(const FilePath& program) { |
108 if (!program.empty()) { | 115 if (!program.empty()) { |
109 program_ = program.value(); | 116 program_ = program.value(); |
110 command_line_string_ = L'"' + program.value() + L'"'; | 117 command_line_string_ = L'"' + program.value() + L'"'; |
111 } | 118 } |
112 } | 119 } |
113 | 120 |
114 #elif defined(OS_POSIX) | 121 #elif defined(OS_POSIX) |
115 CommandLine::CommandLine(ArgumentsOnly args_only) { | 122 CommandLine::CommandLine(ArgumentsOnly args_only) { |
116 // Push an empty argument, because we always assume argv_[0] is a program. | 123 // Push an empty argument, because we always assume argv_[0] is a program. |
117 argv_.push_back(""); | 124 argv_.push_back(""); |
118 } | 125 } |
119 | 126 |
| 127 CommandLine::CommandLine(int argc, const char* const* argv) { |
| 128 InitFromArgv(argc, argv); |
| 129 } |
| 130 |
| 131 CommandLine::CommandLine(const std::vector<std::string>& argv) { |
| 132 InitFromArgv(argv); |
| 133 } |
| 134 |
120 void CommandLine::InitFromArgv(int argc, const char* const* argv) { | 135 void CommandLine::InitFromArgv(int argc, const char* const* argv) { |
121 for (int i = 0; i < argc; ++i) | 136 for (int i = 0; i < argc; ++i) |
122 argv_.push_back(argv[i]); | 137 argv_.push_back(argv[i]); |
123 InitFromArgv(argv_); | 138 InitFromArgv(argv_); |
124 } | 139 } |
125 | 140 |
126 void CommandLine::InitFromArgv(const std::vector<std::string>& argv) { | 141 void CommandLine::InitFromArgv(const std::vector<std::string>& argv) { |
127 argv_ = argv; | 142 argv_ = argv; |
128 bool parse_switches = true; | 143 bool parse_switches = true; |
129 for (size_t i = 1; i < argv_.size(); ++i) { | 144 for (size_t i = 1; i < argv_.size(); ++i) { |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 } | 273 } |
259 | 274 |
260 bool CommandLine::HasSwitch(const std::string& switch_string) const { | 275 bool CommandLine::HasSwitch(const std::string& switch_string) const { |
261 std::string lowercased_switch(switch_string); | 276 std::string lowercased_switch(switch_string); |
262 #if defined(OS_WIN) | 277 #if defined(OS_WIN) |
263 Lowercase(&lowercased_switch); | 278 Lowercase(&lowercased_switch); |
264 #endif | 279 #endif |
265 return switches_.find(lowercased_switch) != switches_.end(); | 280 return switches_.find(lowercased_switch) != switches_.end(); |
266 } | 281 } |
267 | 282 |
| 283 bool CommandLine::HasSwitch(const std::wstring& switch_string) const { |
| 284 return HasSwitch(WideToASCII(switch_string)); |
| 285 } |
| 286 |
| 287 std::string CommandLine::GetSwitchValueASCII( |
| 288 const std::string& switch_string) const { |
| 289 return WideToASCII(GetSwitchValue(switch_string)); |
| 290 } |
| 291 |
| 292 FilePath CommandLine::GetSwitchValuePath( |
| 293 const std::string& switch_string) const { |
| 294 return FilePath::FromWStringHack(GetSwitchValue(switch_string)); |
| 295 } |
| 296 |
268 std::wstring CommandLine::GetSwitchValue( | 297 std::wstring CommandLine::GetSwitchValue( |
269 const std::string& switch_string) const { | 298 const std::string& switch_string) const { |
270 std::string lowercased_switch(switch_string); | 299 std::string lowercased_switch(switch_string); |
271 #if defined(OS_WIN) | 300 #if defined(OS_WIN) |
272 Lowercase(&lowercased_switch); | 301 Lowercase(&lowercased_switch); |
273 #endif | 302 #endif |
274 | 303 |
275 std::map<std::string, StringType>::const_iterator result = | 304 std::map<std::string, StringType>::const_iterator result = |
276 switches_.find(lowercased_switch); | 305 switches_.find(lowercased_switch); |
277 | 306 |
278 if (result == switches_.end()) { | 307 if (result == switches_.end()) { |
279 return L""; | 308 return L""; |
280 } else { | 309 } else { |
281 #if defined(OS_WIN) | 310 #if defined(OS_WIN) |
282 return result->second; | 311 return result->second; |
283 #else | 312 #else |
284 return base::SysNativeMBToWide(result->second); | 313 return base::SysNativeMBToWide(result->second); |
285 #endif | 314 #endif |
286 } | 315 } |
287 } | 316 } |
288 | 317 |
| 318 std::wstring CommandLine::GetSwitchValue( |
| 319 const std::wstring& switch_string) const { |
| 320 return GetSwitchValue(WideToASCII(switch_string)); |
| 321 } |
| 322 |
| 323 FilePath CommandLine::GetProgram() const { |
| 324 return FilePath::FromWStringHack(program()); |
| 325 } |
| 326 |
289 #if defined(OS_WIN) | 327 #if defined(OS_WIN) |
290 std::wstring CommandLine::program() const { | 328 std::wstring CommandLine::program() const { |
291 return program_; | 329 return program_; |
292 } | 330 } |
293 #else | 331 #else |
294 std::wstring CommandLine::program() const { | 332 std::wstring CommandLine::program() const { |
295 DCHECK_GT(argv_.size(), 0U); | 333 DCHECK_GT(argv_.size(), 0U); |
296 return base::SysNativeMBToWide(argv_[0]); | 334 return base::SysNativeMBToWide(argv_[0]); |
297 } | 335 } |
298 #endif | 336 #endif |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 // The wrapper may have embedded arguments (like "gdb --args"). In this case, | 463 // The wrapper may have embedded arguments (like "gdb --args"). In this case, |
426 // we don't pretend to do anything fancy, we just split on spaces. | 464 // we don't pretend to do anything fancy, we just split on spaces. |
427 const std::string wrapper = base::SysWideToNativeMB(wrapper_wide); | 465 const std::string wrapper = base::SysWideToNativeMB(wrapper_wide); |
428 std::vector<std::string> wrapper_and_args; | 466 std::vector<std::string> wrapper_and_args; |
429 SplitString(wrapper, ' ', &wrapper_and_args); | 467 SplitString(wrapper, ' ', &wrapper_and_args); |
430 argv_.insert(argv_.begin(), wrapper_and_args.begin(), wrapper_and_args.end()); | 468 argv_.insert(argv_.begin(), wrapper_and_args.begin(), wrapper_and_args.end()); |
431 } | 469 } |
432 | 470 |
433 #endif | 471 #endif |
434 | 472 |
| 473 void CommandLine::AppendSwitchWithValue(const std::string& switch_string, |
| 474 const std::string& value_string) { |
| 475 AppendSwitchWithValue(switch_string, ASCIIToWide(value_string)); |
| 476 } |
| 477 |
435 // private | 478 // private |
436 CommandLine::CommandLine() { | 479 CommandLine::CommandLine() { |
437 } | 480 } |
OLD | NEW |