diff options
author | Brad King <brad.king@kitware.com> | 2016-09-29 12:09:48 (GMT) |
---|---|---|
committer | CMake Topic Stage <kwrobot@kitware.com> | 2016-09-29 12:09:48 (GMT) |
commit | 5d29506811c5b75ae48e12de6c317f6440874215 (patch) | |
tree | 5b3f133a47da91dbd8f1d74b2ae8715fb41700d6 | |
parent | 48cd0f74059f243d57d7032578a474452265b546 (diff) | |
parent | 66c70cd9f1eb69b03cefe7fbe8e238aaa4630f47 (diff) | |
download | CMake-5d29506811c5b75ae48e12de6c317f6440874215.zip CMake-5d29506811c5b75ae48e12de6c317f6440874215.tar.gz CMake-5d29506811c5b75ae48e12de6c317f6440874215.tar.bz2 |
Merge topic 'cmake_parse_arguments-PARSE_ARGV-multi-value'
66c70cd9 cmake_parse_arguments: Add additional unit tests
41291b20 cmake_parse_arguments: Fix PARSE_ARGV multi-value argument handling
-rw-r--r-- | Source/cmParseArgumentsCommand.cxx | 25 | ||||
-rw-r--r-- | Tests/RunCMake/cmake_parse_arguments/ArgvN.cmake | 12 | ||||
-rw-r--r-- | Tests/RunCMake/cmake_parse_arguments/CornerCasesArgvN.cmake | 53 | ||||
-rw-r--r-- | Tests/RunCMake/cmake_parse_arguments/RunCMakeTest.cmake | 1 | ||||
-rw-r--r-- | Tests/RunCMake/cmake_parse_arguments/Utils.cmake | 1 | ||||
-rw-r--r-- | Tests/RunCMake/cmake_parse_arguments/test_utils.cmake | 44 |
6 files changed, 115 insertions, 21 deletions
diff --git a/Source/cmParseArgumentsCommand.cxx b/Source/cmParseArgumentsCommand.cxx index e8de5b6..55d71ea 100644 --- a/Source/cmParseArgumentsCommand.cxx +++ b/Source/cmParseArgumentsCommand.cxx @@ -4,6 +4,19 @@ #include "cmAlgorithms.h" +static std::string escape_arg(const std::string& arg) +{ + // replace ";" with "\;" so output argument lists will split correctly + std::string escapedArg; + for (size_t i = 0; i < arg.size(); ++i) { + if (arg[i] == ';') { + escapedArg += '\\'; + } + escapedArg += arg[i]; + } + return escapedArg; +} + bool cmParseArgumentsCommand::InitialPass(std::vector<std::string> const& args, cmExecutionStatus&) { @@ -165,10 +178,18 @@ bool cmParseArgumentsCommand::InitialPass(std::vector<std::string> const& args, insideValues = NONE; break; case MULTI: - multi[currentArgName].push_back(*argIter); + if (parseFromArgV) { + multi[currentArgName].push_back(escape_arg(*argIter)); + } else { + multi[currentArgName].push_back(*argIter); + } break; default: - unparsed.push_back(*argIter); + if (parseFromArgV) { + unparsed.push_back(escape_arg(*argIter)); + } else { + unparsed.push_back(*argIter); + } break; } } diff --git a/Tests/RunCMake/cmake_parse_arguments/ArgvN.cmake b/Tests/RunCMake/cmake_parse_arguments/ArgvN.cmake index 61bde03..96a373d 100644 --- a/Tests/RunCMake/cmake_parse_arguments/ArgvN.cmake +++ b/Tests/RunCMake/cmake_parse_arguments/ArgvN.cmake @@ -23,8 +23,16 @@ function(test2 arg1) TEST(pref_OPT2 FALSE) TEST(pref_SINGLE1 "foo;bar") TEST(pref_SINGLE2 UNDEFINED) - TEST(pref_MULTI1 bar foo bar) + TEST(pref_MULTI1 bar "foo;bar") TEST(pref_MULTI2 UNDEFINED) TEST(pref_UNPARSED_ARGUMENTS UNDEFINED) endfunction() -test2("first named" OPT1 SINGLE1 "foo;bar" MULTI1 bar foo bar) +test2("first named" OPT1 SINGLE1 "foo;bar" MULTI1 bar "foo;bar") + +function(test3 arg1) + cmake_parse_arguments(PARSE_ARGV 0 + pref "" "" "") + + TEST(pref_UNPARSED_ARGUMENTS "foo;bar" dog cat) +endfunction() +test3("foo;bar" dog cat) diff --git a/Tests/RunCMake/cmake_parse_arguments/CornerCasesArgvN.cmake b/Tests/RunCMake/cmake_parse_arguments/CornerCasesArgvN.cmake new file mode 100644 index 0000000..807ed03 --- /dev/null +++ b/Tests/RunCMake/cmake_parse_arguments/CornerCasesArgvN.cmake @@ -0,0 +1,53 @@ +include(${CMAKE_CURRENT_LIST_DIR}/test_utils.cmake) + +function(test1) + cmake_parse_arguments(PARSE_ARGV 0 + mpref "" "" "MULTI") + + TEST(mpref_MULTI foo "foo\;bar") + + cmake_parse_arguments(PARSE_ARGV 1 + upref "" "" "MULTI") + + TEST(upref_UNPARSED_ARGUMENTS foo "foo\;bar") +endfunction() +test1(MULTI foo "foo\;bar") + +function(test2) + cmake_parse_arguments(PARSE_ARGV 0 + mpref "" "" "MULTI") + + TEST(mpref_MULTI "foo;" "bar;") + + cmake_parse_arguments(PARSE_ARGV 1 + upref "" "" "MULTI") + + TEST(upref_UNPARSED_ARGUMENTS "foo;" "bar;") +endfunction() +test2(MULTI "foo;" "bar;") + +function(test3) + cmake_parse_arguments(PARSE_ARGV 0 + mpref "" "" "MULTI") + + TEST(mpref_MULTI "[foo;]" "bar\\") + + cmake_parse_arguments(PARSE_ARGV 1 + upref "" "" "MULTI") + + TEST(upref_UNPARSED_ARGUMENTS "[foo;]" "bar\\") +endfunction() +test3(MULTI "[foo;]" "bar\\") + +function(test4) + cmake_parse_arguments(PARSE_ARGV 0 + mpref "" "" "MULTI") + + TEST(mpref_MULTI foo "bar;none") + + cmake_parse_arguments(PARSE_ARGV 1 + upref "" "" "MULTI") + + TEST(upref_UNPARSED_ARGUMENTS foo "bar;none") +endfunction() +test4(MULTI foo bar\\ none) diff --git a/Tests/RunCMake/cmake_parse_arguments/RunCMakeTest.cmake b/Tests/RunCMake/cmake_parse_arguments/RunCMakeTest.cmake index 22d7ee4..1e15b3b 100644 --- a/Tests/RunCMake/cmake_parse_arguments/RunCMakeTest.cmake +++ b/Tests/RunCMake/cmake_parse_arguments/RunCMakeTest.cmake @@ -10,3 +10,4 @@ run_cmake(BadArgvN1) run_cmake(BadArgvN2) run_cmake(BadArgvN3) run_cmake(BadArgvN4) +run_cmake(CornerCasesArgvN) diff --git a/Tests/RunCMake/cmake_parse_arguments/Utils.cmake b/Tests/RunCMake/cmake_parse_arguments/Utils.cmake index 3bbf115..f2001f2 100644 --- a/Tests/RunCMake/cmake_parse_arguments/Utils.cmake +++ b/Tests/RunCMake/cmake_parse_arguments/Utils.cmake @@ -17,4 +17,5 @@ SET (asdf "some value") TEST(asdf "some value") SET (asdf some list) +TEST(asdf some list) TEST(asdf "some;list") diff --git a/Tests/RunCMake/cmake_parse_arguments/test_utils.cmake b/Tests/RunCMake/cmake_parse_arguments/test_utils.cmake index f5425c2..9ce99b8 100644 --- a/Tests/RunCMake/cmake_parse_arguments/test_utils.cmake +++ b/Tests/RunCMake/cmake_parse_arguments/test_utils.cmake @@ -1,20 +1,30 @@ -macro(TEST variable) - SET(expected "${ARGN}") - if ( "${expected}" STREQUAL "UNDEFINED" ) - if (DEFINED ${variable}) - message(FATAL_ERROR "'${variable}' shall be undefined but has value '${${variable}}'") - endif() - elseif( "${expected}" STREQUAL "FALSE" ) - if (NOT ${variable} STREQUAL "FALSE") - message(FATAL_ERROR "'${variable}' shall be FALSE") - endif() - elseif( "${expected}" STREQUAL "TRUE" ) - if (NOT ${variable} STREQUAL "TRUE") - message(FATAL_ERROR "'${variable}' shall be TRUE") - endif() +function(TEST variable) + if(ARGC GREATER 2) + set(i 0) + foreach(value IN LISTS ${variable}) + math(EXPR j "${i} + 1") + set(${variable}[${i}] "${value}") + TEST(${variable}[${i}] "${ARGV${j}}") + set(i ${j}) + endforeach() else() - if (NOT ${variable} STREQUAL "${expected}") - message(FATAL_ERROR "'${variable}' shall be '${expected}'") + set(expected "${ARGN}") + if("${expected}" STREQUAL "UNDEFINED") + if(DEFINED ${variable}) + message(FATAL_ERROR "'${variable}' shall be undefined but has value '${${variable}}'") + endif() + elseif("${expected}" STREQUAL "FALSE") + if(NOT ${variable} STREQUAL "FALSE") + message(FATAL_ERROR "'${variable}' shall be FALSE") + endif() + elseif("${expected}" STREQUAL "TRUE") + if(NOT ${variable} STREQUAL "TRUE") + message(FATAL_ERROR "'${variable}' shall be TRUE") + endif() + else() + if(NOT ${variable} STREQUAL "${expected}") + message(FATAL_ERROR "'${variable}' shall be '${expected}'") + endif() endif() endif() -endmacro() +endfunction() |