| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <ostream> | 8 #include <ostream> |
| 9 | 9 |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 out.push_back('\\'); | 138 out.push_back('\\'); |
| 139 out.push_back('"'); | 139 out.push_back('"'); |
| 140 } else { | 140 } else { |
| 141 out.push_back(arg[i]); | 141 out.push_back(arg[i]); |
| 142 } | 142 } |
| 143 } | 143 } |
| 144 out.push_back('"'); | 144 out.push_back('"'); |
| 145 | 145 |
| 146 return out; | 146 return out; |
| 147 } | 147 } |
| 148 |
| 149 // Controls whether we honor the argc and argv parameters passed in to the main |
| 150 // function for console apps or use the shell32 API's to parse the command |
| 151 // line string and retrieve argc and argv. |
| 152 bool honor_command_line_args_for_testing = false; |
| 148 #endif | 153 #endif |
| 149 | 154 |
| 150 } // namespace | 155 } // namespace |
| 151 | 156 |
| 152 CommandLine::CommandLine(NoProgram no_program) | 157 CommandLine::CommandLine(NoProgram no_program) |
| 153 : argv_(1), | 158 : argv_(1), |
| 154 begin_args_(1) { | 159 begin_args_(1) { |
| 155 } | 160 } |
| 156 | 161 |
| 157 CommandLine::CommandLine(const FilePath& program) | 162 CommandLine::CommandLine(const FilePath& program) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 CommandLine::~CommandLine() { | 195 CommandLine::~CommandLine() { |
| 191 } | 196 } |
| 192 | 197 |
| 193 #if defined(OS_WIN) | 198 #if defined(OS_WIN) |
| 194 // static | 199 // static |
| 195 void CommandLine::set_slash_is_not_a_switch() { | 200 void CommandLine::set_slash_is_not_a_switch() { |
| 196 // The last switch prefix should be slash, so adjust the size to skip it. | 201 // The last switch prefix should be slash, so adjust the size to skip it. |
| 197 DCHECK_EQ(wcscmp(kSwitchPrefixes[arraysize(kSwitchPrefixes) - 1], L"/"), 0); | 202 DCHECK_EQ(wcscmp(kSwitchPrefixes[arraysize(kSwitchPrefixes) - 1], L"/"), 0); |
| 198 switch_prefix_count = arraysize(kSwitchPrefixes) - 1; | 203 switch_prefix_count = arraysize(kSwitchPrefixes) - 1; |
| 199 } | 204 } |
| 205 |
| 206 void CommandLine::set_honor_command_line_args_for_testing( |
| 207 bool honor_args_for_testing) { |
| 208 honor_command_line_args_for_testing = honor_args_for_testing; |
| 209 } |
| 200 #endif | 210 #endif |
| 201 | 211 |
| 202 // static | 212 // static |
| 203 bool CommandLine::Init(int argc, const char* const* argv) { | 213 bool CommandLine::Init(int argc, const char* const* argv) { |
| 204 if (current_process_commandline_) { | 214 if (current_process_commandline_) { |
| 205 // If this is intentional, Reset() must be called first. If we are using | 215 // If this is intentional, Reset() must be called first. If we are using |
| 206 // the shared build mode, we have to share a single object across multiple | 216 // the shared build mode, we have to share a single object across multiple |
| 207 // shared libraries. | 217 // shared libraries. |
| 208 return false; | 218 return false; |
| 209 } | 219 } |
| 210 | 220 |
| 211 current_process_commandline_ = new CommandLine(NO_PROGRAM); | 221 current_process_commandline_ = new CommandLine(NO_PROGRAM); |
| 222 |
| 212 #if defined(OS_WIN) | 223 #if defined(OS_WIN) |
| 213 current_process_commandline_->ParseFromString(::GetCommandLineW()); | 224 if (honor_command_line_args_for_testing) { |
| 225 // On Windows we need to convert the command line arguments to string16 |
| 226 base::CommandLine::StringVector argv_vector; |
| 227 for (int i = 0; i < argc; ++i) |
| 228 argv_vector.push_back(UTF8ToUTF16(argv[i])); |
| 229 current_process_commandline_->InitFromArgv(argv_vector); |
| 230 } else { |
| 231 current_process_commandline_->ParseFromString(::GetCommandLineW()); |
| 232 } |
| 214 #elif defined(OS_POSIX) | 233 #elif defined(OS_POSIX) |
| 215 current_process_commandline_->InitFromArgv(argc, argv); | 234 current_process_commandline_->InitFromArgv(argc, argv); |
| 216 #endif | 235 #endif |
| 217 | |
| 218 return true; | 236 return true; |
| 219 } | 237 } |
| 220 | 238 |
| 221 // static | 239 // static |
| 222 void CommandLine::Reset() { | 240 void CommandLine::Reset() { |
| 223 DCHECK(current_process_commandline_); | 241 DCHECK(current_process_commandline_); |
| 224 delete current_process_commandline_; | 242 delete current_process_commandline_; |
| 225 current_process_commandline_ = NULL; | 243 current_process_commandline_ = NULL; |
| 226 } | 244 } |
| 227 | 245 |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 473 return params; | 491 return params; |
| 474 } | 492 } |
| 475 | 493 |
| 476 void CommandLine::ResetStringPieces() { | 494 void CommandLine::ResetStringPieces() { |
| 477 switches_by_stringpiece_.clear(); | 495 switches_by_stringpiece_.clear(); |
| 478 for (const auto& entry : switches_) | 496 for (const auto& entry : switches_) |
| 479 switches_by_stringpiece_[entry.first] = &(entry.second); | 497 switches_by_stringpiece_[entry.first] = &(entry.second); |
| 480 } | 498 } |
| 481 | 499 |
| 482 } // namespace base | 500 } // namespace base |
| OLD | NEW |