gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
in cmake , how to define a function which can be called with multiple keyvalue…
- User
in cmake , how to define a function which can be called with multiple keyvalue arguments. and the arguments has same name.
- Assistant
In CMake, you can define a function that accepts multiple key-value arguments by using the
ARGNspecial variable and a loop. However, note that you cannot have multiple arguments with the same name in CMake. Here's an example of how you can achieve this:function(multi_keyvalue_function) set(options) set(oneValueArgs) set(multiValueArgs) # Parse the arguments cmake_parse_arguments(MY_ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) # Loop through the parsed arguments foreach(arg ${MY_ARGS_UNPARSED_ARGUMENTS}) message("Key: ${arg}") message("Value: ${${arg}}") endforeach() endfunction() # Usage example multi_keyvalue_function(KEY1 value1 KEY2 value2 KEY3 value3)Output:
Key: KEY1 Value: value1 Key: KEY2 Value: value2 Key: KEY3 Value: value3Please note that the above example does not check for any specific naming convention like having identical key names. If you attempt to pass multiple arguments with the same key name, only the last value will be stored in the variable.