From 928cdb17c53f2fc5e57d658f297ec735ab8cb81d Mon Sep 17 00:00:00 2001 From: Robert Maynard Date: Wed, 12 May 2021 09:43:33 -0400 Subject: cmCommandLineArgument: Correctly record parsing failures --- Source/cmCommandLineArgument.h | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Source/cmCommandLineArgument.h b/Source/cmCommandLineArgument.h index 495dc69..f4153fc 100644 --- a/Source/cmCommandLineArgument.h +++ b/Source/cmCommandLineArgument.h @@ -98,17 +98,11 @@ struct cmCommandLineArgument // parse the string to get the value auto possible_value = cm::string_view(input).substr(this->Name.size()); if (possible_value.empty()) { - parseState = ParseMode::SyntaxError; parseState = ParseMode::ValueError; } else if (possible_value[0] == '=') { possible_value.remove_prefix(1); if (possible_value.empty()) { parseState = ParseMode::ValueError; - } else { - parseState = this->StoreCall(std::string(possible_value), - std::forward(state)...) - ? ParseMode::Valid - : ParseMode::Invalid; } } if (parseState == ParseMode::Valid) { @@ -150,6 +144,8 @@ struct cmCommandLineArgument : ParseMode::Invalid; index = (nextValueIndex - 1); } + } else { + parseState = ParseMode::SyntaxError; } } -- cgit v0.12 From 5aa0dec6b011c74225f4af895922edd10db09607 Mon Sep 17 00:00:00 2001 From: Robert Maynard Date: Wed, 12 May 2021 09:44:39 -0400 Subject: cmake: `--build` and `--install` error out when encountering bad flags Fixes #22186 --- Source/cmakemain.cxx | 26 ++++++++++++++++++++-- Tests/RunCMake/CommandLine/RunCMakeTest.cmake | 12 ++++++++++ .../build-invalid-target-syntax-result.txt | 1 + .../build-invalid-target-syntax-stderr.txt | 2 ++ .../build-unknown-command-long-result.txt | 1 + .../build-unknown-command-long-stderr.txt | 2 ++ .../build-unknown-command-partial-match-result.txt | 1 + .../build-unknown-command-partial-match-stderr.txt | 2 ++ .../build-unknown-command-short-result.txt | 1 + .../build-unknown-command-short-stderr.txt | 2 ++ .../install-unknown-command-long-result.txt | 1 + .../install-unknown-command-long-stderr.txt | 2 ++ .../install-unknown-command-short-result.txt | 1 + .../install-unknown-command-short-stderr.txt | 2 ++ 14 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 Tests/RunCMake/CommandLine/build-invalid-target-syntax-result.txt create mode 100644 Tests/RunCMake/CommandLine/build-invalid-target-syntax-stderr.txt create mode 100644 Tests/RunCMake/CommandLine/build-unknown-command-long-result.txt create mode 100644 Tests/RunCMake/CommandLine/build-unknown-command-long-stderr.txt create mode 100644 Tests/RunCMake/CommandLine/build-unknown-command-partial-match-result.txt create mode 100644 Tests/RunCMake/CommandLine/build-unknown-command-partial-match-stderr.txt create mode 100644 Tests/RunCMake/CommandLine/build-unknown-command-short-result.txt create mode 100644 Tests/RunCMake/CommandLine/build-unknown-command-short-stderr.txt create mode 100644 Tests/RunCMake/CommandLine/install-unknown-command-long-result.txt create mode 100644 Tests/RunCMake/CommandLine/install-unknown-command-long-stderr.txt create mode 100644 Tests/RunCMake/CommandLine/install-unknown-command-short-result.txt create mode 100644 Tests/RunCMake/CommandLine/install-unknown-command-short-stderr.txt diff --git a/Source/cmakemain.cxx b/Source/cmakemain.cxx index 88ba011..ad64818 100644 --- a/Source/cmakemain.cxx +++ b/Source/cmakemain.cxx @@ -532,11 +532,22 @@ int do_build(int ac, char const* const* av) for (; i < inputArgs.size() && !nativeOptionsPassed; ++i) { std::string const& arg = inputArgs[i]; + bool matched = false; + bool parsed = false; for (auto const& m : arguments) { - if (m.matches(arg) && m.parse(arg, i, inputArgs)) { + matched = m.matches(arg); + if (matched) { + parsed = m.parse(arg, i, inputArgs); break; } } + if (!(matched && parsed)) { + dir.clear(); + if (!matched) { + std::cerr << "Unknown argument " << arg << std::endl; + } + break; + } } if (nativeOptionsPassed) { @@ -806,11 +817,22 @@ int do_install(int ac, char const* const* av) for (decltype(inputArgs.size()) i = 0; i < inputArgs.size(); ++i) { std::string const& arg = inputArgs[i]; + bool matched = false; + bool parsed = false; for (auto const& m : arguments) { - if (m.matches(arg) && m.parse(arg, i, inputArgs)) { + matched = m.matches(arg); + if (matched) { + parsed = m.parse(arg, i, inputArgs); break; } } + if (!(matched && parsed)) { + dir.clear(); + if (!matched) { + std::cerr << "Unknown argument " << arg << std::endl; + } + break; + } } } diff --git a/Tests/RunCMake/CommandLine/RunCMakeTest.cmake b/Tests/RunCMake/CommandLine/RunCMakeTest.cmake index 8072a2c..caf3c88 100644 --- a/Tests/RunCMake/CommandLine/RunCMakeTest.cmake +++ b/Tests/RunCMake/CommandLine/RunCMakeTest.cmake @@ -54,6 +54,14 @@ run_cmake_command(build-no-dir ${CMAKE_COMMAND} --build) run_cmake_command(build-no-cache ${CMAKE_COMMAND} --build ${RunCMake_SOURCE_DIR}) +run_cmake_command(build-unknown-command-short + ${CMAKE_COMMAND} --build ${RunCMake_SOURCE_DIR} -invalid-command) +run_cmake_command(build-unknown-command-long + ${CMAKE_COMMAND} --build ${RunCMake_SOURCE_DIR} --invalid-command) +run_cmake_command(build-unknown-command-partial-match + ${CMAKE_COMMAND} --build ${RunCMake_SOURCE_DIR} --targetinvalid) +run_cmake_command(build-invalid-target-syntax + ${CMAKE_COMMAND} --build ${RunCMake_SOURCE_DIR} --target=invalid) run_cmake_command(build-no-generator ${CMAKE_COMMAND} --build ${RunCMake_SOURCE_DIR}/cache-no-generator) run_cmake_command(build-bad-dir @@ -65,6 +73,10 @@ run_cmake_command(install-no-dir ${CMAKE_COMMAND} --install) run_cmake_command(install-bad-dir ${CMAKE_COMMAND} --install dir-does-not-exist) +run_cmake_command(install-unknown-command-short + ${CMAKE_COMMAND} --install ${RunCMake_SOURCE_DIR} -invalid-command) +run_cmake_command(install-unknown-command-long + ${CMAKE_COMMAND} --install ${RunCMake_SOURCE_DIR} --invalid-command) run_cmake_command(install-options-to-vars ${CMAKE_COMMAND} --install ${RunCMake_SOURCE_DIR}/dir-install-options-to-vars --strip --prefix /var/test --config sample --component pack) diff --git a/Tests/RunCMake/CommandLine/build-invalid-target-syntax-result.txt b/Tests/RunCMake/CommandLine/build-invalid-target-syntax-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/CommandLine/build-invalid-target-syntax-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/CommandLine/build-invalid-target-syntax-stderr.txt b/Tests/RunCMake/CommandLine/build-invalid-target-syntax-stderr.txt new file mode 100644 index 0000000..ce1c92d --- /dev/null +++ b/Tests/RunCMake/CommandLine/build-invalid-target-syntax-stderr.txt @@ -0,0 +1,2 @@ +^CMake Error: Invalid syntax used with --target +Usage: cmake --build \[ \| --preset \] \[options\] \[-- \[native-options\]\] diff --git a/Tests/RunCMake/CommandLine/build-unknown-command-long-result.txt b/Tests/RunCMake/CommandLine/build-unknown-command-long-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/CommandLine/build-unknown-command-long-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/CommandLine/build-unknown-command-long-stderr.txt b/Tests/RunCMake/CommandLine/build-unknown-command-long-stderr.txt new file mode 100644 index 0000000..c8f1a03 --- /dev/null +++ b/Tests/RunCMake/CommandLine/build-unknown-command-long-stderr.txt @@ -0,0 +1,2 @@ +^Unknown argument --invalid-command +Usage: cmake --build \[ \| --preset \] \[options\] \[-- \[native-options\]\] diff --git a/Tests/RunCMake/CommandLine/build-unknown-command-partial-match-result.txt b/Tests/RunCMake/CommandLine/build-unknown-command-partial-match-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/CommandLine/build-unknown-command-partial-match-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/CommandLine/build-unknown-command-partial-match-stderr.txt b/Tests/RunCMake/CommandLine/build-unknown-command-partial-match-stderr.txt new file mode 100644 index 0000000..ce1c92d --- /dev/null +++ b/Tests/RunCMake/CommandLine/build-unknown-command-partial-match-stderr.txt @@ -0,0 +1,2 @@ +^CMake Error: Invalid syntax used with --target +Usage: cmake --build \[ \| --preset \] \[options\] \[-- \[native-options\]\] diff --git a/Tests/RunCMake/CommandLine/build-unknown-command-short-result.txt b/Tests/RunCMake/CommandLine/build-unknown-command-short-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/CommandLine/build-unknown-command-short-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/CommandLine/build-unknown-command-short-stderr.txt b/Tests/RunCMake/CommandLine/build-unknown-command-short-stderr.txt new file mode 100644 index 0000000..a6cfece --- /dev/null +++ b/Tests/RunCMake/CommandLine/build-unknown-command-short-stderr.txt @@ -0,0 +1,2 @@ +^Unknown argument -invalid-command +Usage: cmake --build \[ \| --preset \] \[options\] \[-- \[native-options\]\] diff --git a/Tests/RunCMake/CommandLine/install-unknown-command-long-result.txt b/Tests/RunCMake/CommandLine/install-unknown-command-long-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/CommandLine/install-unknown-command-long-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/CommandLine/install-unknown-command-long-stderr.txt b/Tests/RunCMake/CommandLine/install-unknown-command-long-stderr.txt new file mode 100644 index 0000000..e6cee6b --- /dev/null +++ b/Tests/RunCMake/CommandLine/install-unknown-command-long-stderr.txt @@ -0,0 +1,2 @@ +^Unknown argument --invalid-command +Usage: cmake --install \[options\] diff --git a/Tests/RunCMake/CommandLine/install-unknown-command-short-result.txt b/Tests/RunCMake/CommandLine/install-unknown-command-short-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/CommandLine/install-unknown-command-short-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/CommandLine/install-unknown-command-short-stderr.txt b/Tests/RunCMake/CommandLine/install-unknown-command-short-stderr.txt new file mode 100644 index 0000000..f690ec4 --- /dev/null +++ b/Tests/RunCMake/CommandLine/install-unknown-command-short-stderr.txt @@ -0,0 +1,2 @@ +^Unknown argument -invalid-command +Usage: cmake --install \[options\] -- cgit v0.12 From f78b167a2364d81fe8effab5face0ff888f6d9c2 Mon Sep 17 00:00:00 2001 From: Robert Maynard Date: Thu, 13 May 2021 09:33:30 -0400 Subject: cmCommandLineArgument: Provide more information syntax error messages --- Source/cmCommandLineArgument.h | 7 ++++--- Tests/RunCMake/CommandLine/build-invalid-target-syntax-stderr.txt | 2 +- .../CommandLine/build-unknown-command-partial-match-stderr.txt | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Source/cmCommandLineArgument.h b/Source/cmCommandLineArgument.h index f4153fc..ddfff32 100644 --- a/Source/cmCommandLineArgument.h +++ b/Source/cmCommandLineArgument.h @@ -25,7 +25,7 @@ struct cmCommandLineArgument template cmCommandLineArgument(std::string n, Values t, FunctionType&& func) - : InvalidSyntaxMessage(cmStrCat("Invalid syntax used with ", n)) + : InvalidSyntaxMessage(cmStrCat(" is invalid syntax for ", n)) , InvalidValueMessage(cmStrCat("Invalid value used with ", n)) , Name(std::move(n)) , Type(t) @@ -36,7 +36,7 @@ struct cmCommandLineArgument template cmCommandLineArgument(std::string n, std::string failedMsg, Values t, FunctionType&& func) - : InvalidSyntaxMessage(cmStrCat("Invalid syntax used with ", n)) + : InvalidSyntaxMessage(cmStrCat(" is invalid syntax for ", n)) , InvalidValueMessage(std::move(failedMsg)) , Name(std::move(n)) , Type(t) @@ -150,7 +150,8 @@ struct cmCommandLineArgument } if (parseState == ParseMode::SyntaxError) { - cmSystemTools::Error(this->InvalidSyntaxMessage); + cmSystemTools::Error( + cmStrCat("'", input, "'", this->InvalidSyntaxMessage)); } else if (parseState == ParseMode::ValueError) { cmSystemTools::Error(this->InvalidValueMessage); } diff --git a/Tests/RunCMake/CommandLine/build-invalid-target-syntax-stderr.txt b/Tests/RunCMake/CommandLine/build-invalid-target-syntax-stderr.txt index ce1c92d..5fe2539 100644 --- a/Tests/RunCMake/CommandLine/build-invalid-target-syntax-stderr.txt +++ b/Tests/RunCMake/CommandLine/build-invalid-target-syntax-stderr.txt @@ -1,2 +1,2 @@ -^CMake Error: Invalid syntax used with --target +^CMake Error: '--target=invalid' is invalid syntax for --target Usage: cmake --build \[ \| --preset \] \[options\] \[-- \[native-options\]\] diff --git a/Tests/RunCMake/CommandLine/build-unknown-command-partial-match-stderr.txt b/Tests/RunCMake/CommandLine/build-unknown-command-partial-match-stderr.txt index ce1c92d..d69338a 100644 --- a/Tests/RunCMake/CommandLine/build-unknown-command-partial-match-stderr.txt +++ b/Tests/RunCMake/CommandLine/build-unknown-command-partial-match-stderr.txt @@ -1,2 +1,2 @@ -^CMake Error: Invalid syntax used with --target +^CMake Error: '--targetinvalid' is invalid syntax for --target Usage: cmake --build \[ \| --preset \] \[options\] \[-- \[native-options\]\] -- cgit v0.12