diff options
-rw-r--r-- | base/command_line.cc | 27 | ||||
-rw-r--r-- | base/command_line.h | 8 | ||||
-rw-r--r-- | base/command_line_unittest.cc | 58 |
3 files changed, 84 insertions, 9 deletions
diff --git a/base/command_line.cc b/base/command_line.cc index 29467e3..f4553ea 100644 --- a/base/command_line.cc +++ b/base/command_line.cc @@ -228,31 +228,42 @@ CommandLine::StringType CommandLine::GetCommandLineString() const { #if defined(OS_WIN) string = QuoteForCommandLineToArgvW(string); #endif + StringType params(GetArgumentsString()); + if (!params.empty()) { + string.append(StringType(FILE_PATH_LITERAL(" "))); + string.append(params); + } + return string; +} + +CommandLine::StringType CommandLine::GetArgumentsString() const { + StringType params; // Append switches and arguments. bool parse_switches = true; for (size_t i = 1; i < argv_.size(); ++i) { - CommandLine::StringType arg = argv_[i]; - CommandLine::StringType switch_string; - CommandLine::StringType switch_value; + StringType arg = argv_[i]; + StringType switch_string; + StringType switch_value; parse_switches &= arg != kSwitchTerminator; - string.append(StringType(FILE_PATH_LITERAL(" "))); + if (i > 1) + params.append(StringType(FILE_PATH_LITERAL(" "))); if (parse_switches && IsSwitch(arg, &switch_string, &switch_value)) { - string.append(switch_string); + params.append(switch_string); if (!switch_value.empty()) { #if defined(OS_WIN) switch_value = QuoteForCommandLineToArgvW(switch_value); #endif - string.append(kSwitchValueSeparator + switch_value); + params.append(kSwitchValueSeparator + switch_value); } } else { #if defined(OS_WIN) arg = QuoteForCommandLineToArgvW(arg); #endif - string.append(arg); + params.append(arg); } } - return string; + return params; } FilePath CommandLine::GetProgram() const { diff --git a/base/command_line.h b/base/command_line.h index 37cc71b..7d8627f 100644 --- a/base/command_line.h +++ b/base/command_line.h @@ -79,9 +79,15 @@ class BASE_EXPORT CommandLine { void InitFromArgv(const StringVector& argv); // Constructs and returns the represented command line string. - // CAUTION! This should be avoided because quoting behavior is unclear. + // CAUTION! This should be avoided on POSIX because quoting behavior is + // unclear. StringType GetCommandLineString() const; + // Constructs and returns the represented arguments string. + // CAUTION! This should be avoided on POSIX because quoting behavior is + // unclear. + StringType GetArgumentsString() const; + // Returns the original command line string as a vector of strings. const StringVector& argv() const { return argv_; } diff --git a/base/command_line_unittest.cc b/base/command_line_unittest.cc index d93ffb8..372af37 100644 --- a/base/command_line_unittest.cc +++ b/base/command_line_unittest.cc @@ -178,6 +178,64 @@ TEST(CommandLineTest, EmptyString) { EXPECT_TRUE(cl_from_argv.GetArgs().empty()); } +TEST(CommandLineTest, GetArgumentsString) { + static const FilePath::CharType kPath1[] = + FILE_PATH_LITERAL("C:\\Some File\\With Spaces.ggg"); + static const FilePath::CharType kPath2[] = + FILE_PATH_LITERAL("C:\\no\\spaces.ggg"); + + static const char kFirstArgName[] = "first-arg"; + static const char kSecondArgName[] = "arg2"; + static const char kThirdArgName[] = "arg with space"; + static const char kFourthArgName[] = "nospace"; + + CommandLine cl(CommandLine::NO_PROGRAM); + cl.AppendSwitchPath(kFirstArgName, FilePath(kPath1)); + cl.AppendSwitchPath(kSecondArgName, FilePath(kPath2)); + cl.AppendArg(kThirdArgName); + cl.AppendArg(kFourthArgName); + +#if defined(OS_WIN) + CommandLine::StringType expected_first_arg(UTF8ToUTF16(kFirstArgName)); + CommandLine::StringType expected_second_arg(UTF8ToUTF16(kSecondArgName)); + CommandLine::StringType expected_third_arg(UTF8ToUTF16(kThirdArgName)); + CommandLine::StringType expected_fourth_arg(UTF8ToUTF16(kFourthArgName)); +#elif defined(OS_POSIX) + CommandLine::StringType expected_first_arg(kFirstArgName); + CommandLine::StringType expected_second_arg(kSecondArgName); + CommandLine::StringType expected_third_arg(kThirdArgName); + CommandLine::StringType expected_fourth_arg(kFourthArgName); +#endif + +#if defined(OS_WIN) +#define QUOTE_ON_WIN FILE_PATH_LITERAL("\"") +#else +#define QUOTE_ON_WIN FILE_PATH_LITERAL("") +#endif // OS_WIN + + CommandLine::StringType expected_str; + expected_str.append(FILE_PATH_LITERAL("--")) + .append(expected_first_arg) + .append(FILE_PATH_LITERAL("=")) + .append(QUOTE_ON_WIN) + .append(kPath1) + .append(QUOTE_ON_WIN) + .append(FILE_PATH_LITERAL(" ")) + .append(FILE_PATH_LITERAL("--")) + .append(expected_second_arg) + .append(FILE_PATH_LITERAL("=")) + .append(QUOTE_ON_WIN) + .append(kPath2) + .append(QUOTE_ON_WIN) + .append(FILE_PATH_LITERAL(" ")) + .append(QUOTE_ON_WIN) + .append(expected_third_arg) + .append(QUOTE_ON_WIN) + .append(FILE_PATH_LITERAL(" ")) + .append(expected_fourth_arg); + EXPECT_EQ(expected_str, cl.GetArgumentsString()); +} + // Test methods for appending switches to a command line. TEST(CommandLineTest, AppendSwitches) { std::string switch1 = "switch1"; |