diff options
Diffstat (limited to 'base')
-rw-r--r-- | base/command_line.cc | 23 | ||||
-rw-r--r-- | base/command_line.h | 15 | ||||
-rw-r--r-- | base/command_line_unittest.cc | 11 |
3 files changed, 41 insertions, 8 deletions
diff --git a/base/command_line.cc b/base/command_line.cc index 848ac6b..11a475d 100644 --- a/base/command_line.cc +++ b/base/command_line.cc @@ -29,6 +29,7 @@ const wchar_t* const CommandLine::kSwitchPrefixes[] = {L"--", L"-"}; #endif const wchar_t CommandLine::kSwitchValueSeparator[] = L"="; +const wchar_t CommandLine::kSwitchTerminator[] = L"--"; // Needed to avoid a typecast on the tolower() function pointer in Lowercase(). // MSVC accepts it as-is but GCC requires the typecast. @@ -88,10 +89,21 @@ class CommandLine::Data { // Populate program_ with the trimmed version of the first arg. TrimWhitespace(args[0], TRIM_ALL, &program_); + bool parse_switches = true; for (int i = 1; i < num_args; ++i) { wstring arg; TrimWhitespace(args[i], TRIM_ALL, &arg); + if (!parse_switches) { + loose_values_.push_back(arg); + continue; + } + + if (arg == kSwitchTerminator) { + parse_switches = false; + continue; + } + wstring switch_string; wstring switch_value; if (IsSwitch(arg, &switch_string, &switch_value)) { @@ -113,11 +125,22 @@ class CommandLine::Data { program_ = base::SysNativeMBToWide(argv[0]); command_line_string_ = program_; + bool parse_switches = true; for (int i = 1; i < argc; ++i) { std::wstring arg = base::SysNativeMBToWide(argv[i]); command_line_string_.append(L" "); command_line_string_.append(arg); + if (!parse_switches) { + loose_values_.push_back(arg); + continue; + } + + if (arg == kSwitchTerminator) { + parse_switches = false; + continue; + } + wstring switch_string; wstring switch_value; if (IsSwitch(arg, &switch_string, &switch_value)) { diff --git a/base/command_line.h b/base/command_line.h index 5574790..7e54fd4 100644 --- a/base/command_line.h +++ b/base/command_line.h @@ -1,16 +1,17 @@ // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// + // This file contains a class that can be used to extract the salient // elements of a command line in a relatively lightweight manner. // Switches can optionally have a value attached using an equals sign, // as in "-switch=value". Arguments that aren't prefixed with a // switch prefix are considered "loose parameters". Switch names -// are case-insensitive. +// are case-insensitive. An argument of "--" will terminate switch parsing, +// causing everything after to be considered as loose parameters. -#ifndef BASE_COMMAND_LINE_H__ -#define BASE_COMMAND_LINE_H__ +#ifndef BASE_COMMAND_LINE_H_ +#define BASE_COMMAND_LINE_H_ #include <map> #include <string> @@ -76,6 +77,9 @@ class CommandLine { // The string that's used to separate switches from their values. static const wchar_t kSwitchValueSeparator[]; + // Treat everything after this argument as loose parameters. + static const wchar_t kSwitchTerminator[]; + // Appends the given switch string (preceded by a space and a switch // prefix) to the given string. static void AppendSwitch(std::wstring* command_line_string, @@ -101,5 +105,4 @@ class CommandLine { DISALLOW_EVIL_CONSTRUCTORS(CommandLine); }; -#endif // BASE_COMMAND_LINE_H__ - +#endif // BASE_COMMAND_LINE_H_ diff --git a/base/command_line_unittest.cc b/base/command_line_unittest.cc index f3d63cb..4319b4d 100644 --- a/base/command_line_unittest.cc +++ b/base/command_line_unittest.cc @@ -21,6 +21,7 @@ TEST(CommandLineTest, CommandLineConstructor) { L"--other-switches=\"--dog=canine --cat=feline\" " L"-spaetzle=Crepe -=loosevalue flan " L"--input-translation=\"45\"--output-rotation " + L"-- -- --not-a-switch " L"\"in the time of submarines...\""); #elif defined(OS_POSIX) const char* argv[] = {"program", "--foo=", "-bAr", @@ -28,6 +29,7 @@ TEST(CommandLineTest, CommandLineConstructor) { "--other-switches=--dog=canine --cat=feline", "-spaetzle=Crepe", "-=loosevalue", "flan", "--input-translation=45--output-rotation", + "--", "--", "--not-a-switch", "in the time of submarines..."}; CommandLine cl(arraysize(argv), argv); #endif @@ -38,6 +40,8 @@ TEST(CommandLineTest, CommandLineConstructor) { EXPECT_FALSE(cl.HasSwitch(L"dog")); EXPECT_FALSE(cl.HasSwitch(L"cat")); EXPECT_FALSE(cl.HasSwitch(L"output-rotation")); + EXPECT_FALSE(cl.HasSwitch(L"not-a-switch")); + EXPECT_FALSE(cl.HasSwitch(L"--")); EXPECT_EQ(L"program", cl.program()); @@ -56,13 +60,17 @@ TEST(CommandLineTest, CommandLineConstructor) { EXPECT_EQ(L"--dog=canine --cat=feline", cl.GetSwitchValue(L"other-switches")); EXPECT_EQ(L"45--output-rotation", cl.GetSwitchValue(L"input-translation")); - EXPECT_EQ(3U, cl.GetLooseValueCount()); + EXPECT_EQ(5U, cl.GetLooseValueCount()); CommandLine::LooseValueIterator iter = cl.GetLooseValuesBegin(); EXPECT_EQ(L"flim", *iter); ++iter; EXPECT_EQ(L"flan", *iter); ++iter; + EXPECT_EQ(L"--", *iter); + ++iter; + EXPECT_EQ(L"--not-a-switch", *iter); + ++iter; EXPECT_EQ(L"in the time of submarines...", *iter); ++iter; EXPECT_TRUE(iter == cl.GetLooseValuesEnd()); @@ -116,4 +124,3 @@ TEST(CommandLineTest, AppendSwitches) { EXPECT_EQ(value4.substr(1, value4.length() - 2), cl.GetSwitchValue(switch4)); } #endif - |