1 # Copied from http://www.itk.org/Wiki/CMakeMacroParseArguments under
2 # http://creativecommons.org/licenses/by/2.5/.
4 # The PARSE_ARGUMENTS macro will take the arguments of another macro and define
5 # several variables. The first argument to PARSE_ARGUMENTS is a prefix to put on
6 # all variables it creates. The second argument is a list of names, and the
7 # third argument is a list of options. Both of these lists should be quoted. The
8 # rest of PARSE_ARGUMENTS are arguments from another macro to be parsed.
10 # PARSE_ARGUMENTS(prefix arg_names options arg1 arg2...)
12 # For each item in options, PARSE_ARGUMENTS will create a variable with that
13 # name, prefixed with prefix_. So, for example, if prefix is MY_MACRO and
14 # options is OPTION1;OPTION2, then PARSE_ARGUMENTS will create the variables
15 # MY_MACRO_OPTION1 and MY_MACRO_OPTION2. These variables will be set to true if
16 # the option exists in the command line or false otherwise.
18 #For each item in arg_names, PARSE_ARGUMENTS will create a variable with that
19 #name, prefixed with prefix_. Each variable will be filled with the arguments
20 #that occur after the given arg_name is encountered up to the next arg_name or
21 #the end of the arguments. All options are removed from these
22 #lists. PARSE_ARGUMENTS also creates a prefix_DEFAULT_ARGS variable containing
23 #the list of all arguments up to the first arg_name encountered.
25 #Here is a simple, albeit impractical, example of using PARSE_ARGUMENTS that
26 #demonstrates its behavior.
35 # PARSE_ARGUMENTS(ARG "LIST1;LIST2;LIST3" "OPTION1;OPTION2;OPTION3" ${arguments})
37 # PARSE_ARGUMENTS creates 7 variables and sets them as follows:
38 # ARG_DEFAULT_ARGS: hello;world
46 # If you don't have any options, use an empty string in its place.
47 # PARSE_ARGUMENTS(ARG "LIST1;LIST2;LIST3" "" ${arguments})
48 # Likewise if you have no lists.
49 # PARSE_ARGUMENTS(ARG "" "OPTION1;OPTION2;OPTION3" ${arguments})
51 MACRO(PARSE_ARGUMENTS prefix arg_names option_names)
53 FOREACH(arg_name ${arg_names})
54 SET(${prefix}_${arg_name})
56 FOREACH(option ${option_names})
57 SET(${prefix}_${option} FALSE)
60 SET(current_arg_name DEFAULT_ARGS)
63 SET(larg_names ${arg_names})
64 LIST(FIND larg_names "${arg}" is_arg_name)
65 IF (is_arg_name GREATER -1)
66 SET(${prefix}_${current_arg_name} ${current_arg_list})
67 SET(current_arg_name ${arg})
69 ELSE (is_arg_name GREATER -1)
70 SET(loption_names ${option_names})
71 LIST(FIND loption_names "${arg}" is_option)
72 IF (is_option GREATER -1)
73 SET(${prefix}_${arg} TRUE)
74 ELSE (is_option GREATER -1)
75 SET(current_arg_list ${current_arg_list} ${arg})
76 ENDIF (is_option GREATER -1)
77 ENDIF (is_arg_name GREATER -1)
79 SET(${prefix}_${current_arg_name} ${current_arg_list})
80 ENDMACRO(PARSE_ARGUMENTS)