I was just reading the book Modern CMake for C++, and on page 55, the author says exactly this:
Remember that you can pass arguments to scripts through the command line after a
--token. Values will be stored in theCMAKE_ARGV<n>variable and the count of the passed arguments will be in theCMAKE_ARGCvariable.
so I tried the following script:
# file.cmake
message("CMAKE_ARGC=${CMAKE_ARGC}")
set (i 0)
while(i LESS CMAKE_ARGC)
message("CMAKE_ARGV${i}=${CMAKE_ARGV${i}}")
math(EXPR i "${i}+1")
endwhile()
from here, and I ran this command:
cmake -P file.cmake -- arg
but the output was:
CMAKE_ARGC=5
CMAKE_ARGV0=cmake
CMAKE_ARGV1=-P
CMAKE_ARGV2=file.cmake
CMAKE_ARGV3=--
CMAKE_ARGV4=arg
Does the author of Modern CMake for C++ say that the script arguments starts after -- (which he's clearly WRONG) or I'm missing something?
I don't know the exact text there, maybe the author is wrong, maybe you lost some context and misread that.
--does not mean that the args before--will not be stored inCMAKE_ARGV<n>. It means only that any options after--are not parsed by CMake.If you run you script like
cmake -P file.cmake -Dvar=val, you getCMAKE_ARGV3=-Dvar=valin the output and the variable${var}in the script.If you run you script like
cmake -P file.cmake -- -Dvar=val, you getCMAKE_ARGV4=-Dvar=valin the output and no variable${var}defined in the script.