diff options
-rw-r--r-- | base/command_line.cc | 38 | ||||
-rw-r--r-- | base/command_line.h | 8 | ||||
-rw-r--r-- | base/run_all_unittests.cc | 2 |
3 files changed, 26 insertions, 22 deletions
diff --git a/base/command_line.cc b/base/command_line.cc index 57d1b12..c5b24d8 100644 --- a/base/command_line.cc +++ b/base/command_line.cc @@ -41,16 +41,6 @@ #include "base/string_util.h" #include "base/sys_string_conversions.h" -extern "C" { -#if defined(OS_MACOSX) -extern const char** NXArgv; -extern int NXArgc; -#elif defined(OS_LINUX) -extern const char** __libc_argv; -extern int __libc_argc; -#endif -} // extern "C" - using namespace std; // Since we use a lazy match, make sure that longer versions (like L"--") @@ -58,7 +48,7 @@ using namespace std; #if defined(OS_WIN) const wchar_t* const CommandLine::kSwitchPrefixes[] = {L"--", L"-", L"/"}; #elif defined(OS_POSIX) -// POSIX shells don't use slash as a switch since they mark absolute paths +// Unixes don't use slash as a switch. const wchar_t* const CommandLine::kSwitchPrefixes[] = {L"--", L"-"}; #endif @@ -90,13 +80,9 @@ class CommandLine::Data { Data() { Init(GetCommandLineW()); } -#elif defined(OS_MACOSX) - Data() { - Init(NXArgc, NXArgv); - } -#elif defined(OS_LINUX) +#elif defined(OS_POSIX) Data() { - Init(__gnuc_argc, __gnuc_argv); + // Owner must call Init(). } #endif @@ -105,7 +91,7 @@ class CommandLine::Data { Init(command_line); } #elif defined(OS_POSIX) - Data(const int argc, const char* argv[]) { + Data(const int argc, char** argv) { Init(argc, argv); } #endif @@ -142,8 +128,10 @@ class CommandLine::Data { if (args) LocalFree(args); } -#elif defined(OS_POSIX) // Does the actual parsing of the command line. - void Init(int argc, const char* argv[]) { + +#elif defined(OS_POSIX) + // Does the actual parsing of the command line. + void Init(int argc, char** argv) { if (argc < 1) return; program_ = base::SysNativeMBToWide(argv[0]); @@ -226,6 +214,9 @@ class CommandLine::Data { CommandLine::CommandLine() : we_own_data_(false), // The Singleton class will manage it for us. data_(Singleton<Data>::get()) { + DCHECK(!data_->command_line_string().empty()) << + "You must call CommandLine::SetArgcArgv before making any CommandLine " + "calls."; } #if defined(OS_WIN) @@ -234,7 +225,7 @@ CommandLine::CommandLine(const wstring& command_line) data_(new Data(command_line)) { } #elif defined(OS_POSIX) -CommandLine::CommandLine(const int argc, const char* argv[]) +CommandLine::CommandLine(const int argc, char** argv) : we_own_data_(true), data_(new Data(argc, argv)) { } @@ -245,6 +236,11 @@ CommandLine::~CommandLine() { delete data_; } +// static +void CommandLine::SetArgcArgv(int argc, char** argv) { + Singleton<Data>::get()->Init(argc, argv); +} + bool CommandLine::HasSwitch(const wstring& switch_string) const { wstring lowercased_switch(switch_string); Lowercase(&lowercased_switch); diff --git a/base/command_line.h b/base/command_line.h index 4f24f26..72cf8c9 100644 --- a/base/command_line.h +++ b/base/command_line.h @@ -55,11 +55,17 @@ class CommandLine { // The program name is assumed to be the first item in the string. CommandLine(const std::wstring& command_line); #elif defined(OS_POSIX) - CommandLine(int argc, const char** argv); + CommandLine(int argc, char** argv); #endif ~CommandLine(); + // On non-Windows platforms, main() must call SetArgcArgv() before accessing + // any members of this class. + // On Windows, this call is a no-op (we instead parse GetCommandLineW() + // directly) because we don't trust the CRT's parsing of the command line. + static void SetArgcArgv(int argc, char** argv); + // Returns true if this command line contains the given switch. // (Switch names are case-insensitive.) bool HasSwitch(const std::wstring& switch_string) const; diff --git a/base/run_all_unittests.cc b/base/run_all_unittests.cc index 299b1c1..62d22a5 100644 --- a/base/run_all_unittests.cc +++ b/base/run_all_unittests.cc @@ -35,5 +35,7 @@ int main(int argc, char** argv) { // the AtExitManager or else we will leak objects. base::AtExitManager at_exit_manager; + CommandLine::SetArgcArgv(argc, argv); + return TestSuite(argc, argv).Run(); } |