Browse Source

Merge topic 'cmake_build_and_install_command_error_when_given_bad_arguments'

f78b167a23 cmCommandLineArgument: Provide more information syntax error messages
5aa0dec6b0 cmake: `--build` and `--install` error out when encountering bad flags
928cdb17c5 cmCommandLineArgument: Correctly record parsing failures

Acked-by: Kitware Robot <[email protected]>
Merge-request: !6119
Brad King 4 years ago
parent
commit
e2c5209532

+ 6 - 9
Source/cmCommandLineArgument.h

@@ -25,7 +25,7 @@ struct cmCommandLineArgument
 
   template <typename FunctionType>
   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 <typename FunctionType>
   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)
@@ -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<CallState>(state)...)
-              ? ParseMode::Valid
-              : ParseMode::Invalid;
           }
         }
         if (parseState == ParseMode::Valid) {
@@ -154,11 +148,14 @@ struct cmCommandLineArgument
             : ParseMode::Invalid;
           index = (nextValueIndex - 1);
         }
+      } else {
+        parseState = ParseMode::SyntaxError;
       }
     }
 
     if (parseState == ParseMode::SyntaxError) {
-      cmSystemTools::Error(this->InvalidSyntaxMessage);
+      cmSystemTools::Error(
+        cmStrCat("'", input, "'", this->InvalidSyntaxMessage));
     } else if (parseState == ParseMode::ValueError) {
       cmSystemTools::Error(this->InvalidValueMessage);
     }

+ 24 - 2
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;
+      }
     }
   }
 

+ 12 - 0
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
@@ -67,6 +75,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)

+ 1 - 0
Tests/RunCMake/CommandLine/build-invalid-target-syntax-result.txt

@@ -0,0 +1 @@
+1

+ 2 - 0
Tests/RunCMake/CommandLine/build-invalid-target-syntax-stderr.txt

@@ -0,0 +1,2 @@
+^CMake Error: '--target=invalid' is invalid syntax for --target
+Usage: cmake --build \[<dir> \| --preset <preset>\] \[options\] \[-- \[native-options\]\]

+ 1 - 0
Tests/RunCMake/CommandLine/build-unknown-command-long-result.txt

@@ -0,0 +1 @@
+1

+ 2 - 0
Tests/RunCMake/CommandLine/build-unknown-command-long-stderr.txt

@@ -0,0 +1,2 @@
+^Unknown argument --invalid-command
+Usage: cmake --build \[<dir> \| --preset <preset>\] \[options\] \[-- \[native-options\]\]

+ 1 - 0
Tests/RunCMake/CommandLine/build-unknown-command-partial-match-result.txt

@@ -0,0 +1 @@
+1

+ 2 - 0
Tests/RunCMake/CommandLine/build-unknown-command-partial-match-stderr.txt

@@ -0,0 +1,2 @@
+^CMake Error: '--targetinvalid' is invalid syntax for --target
+Usage: cmake --build \[<dir> \| --preset <preset>\] \[options\] \[-- \[native-options\]\]

+ 1 - 0
Tests/RunCMake/CommandLine/build-unknown-command-short-result.txt

@@ -0,0 +1 @@
+1

+ 2 - 0
Tests/RunCMake/CommandLine/build-unknown-command-short-stderr.txt

@@ -0,0 +1,2 @@
+^Unknown argument -invalid-command
+Usage: cmake --build \[<dir> \| --preset <preset>\] \[options\] \[-- \[native-options\]\]

+ 1 - 0
Tests/RunCMake/CommandLine/install-unknown-command-long-result.txt

@@ -0,0 +1 @@
+1

+ 2 - 0
Tests/RunCMake/CommandLine/install-unknown-command-long-stderr.txt

@@ -0,0 +1,2 @@
+^Unknown argument --invalid-command
+Usage: cmake --install <dir> \[options\]

+ 1 - 0
Tests/RunCMake/CommandLine/install-unknown-command-short-result.txt

@@ -0,0 +1 @@
+1

+ 2 - 0
Tests/RunCMake/CommandLine/install-unknown-command-short-stderr.txt

@@ -0,0 +1,2 @@
+^Unknown argument -invalid-command
+Usage: cmake --install <dir> \[options\]