mirror of
https://github.com/Kitware/CMake.git
synced 2026-01-24 15:38:54 -06:00
Merge topic 'doc-cmake_parse_arguments-prefix'
2000abe74fHelp: Recommend good prefixes for cmake_parse_arguments, expand examplebb37d0e1a5Help: Improve grammar and wording for cmake_parse_arguments Acked-by: Kitware Robot <kwrobot@kitware.com> Merge-request: !9788
This commit is contained in:
@@ -20,48 +20,49 @@ It processes the arguments given to that macro or function,
|
||||
and defines a set of variables which hold the values of the
|
||||
respective options.
|
||||
|
||||
The first signature reads processes arguments passed in the ``<args>...``.
|
||||
The first signature reads arguments passed in the ``<args>...``.
|
||||
This may be used in either a :command:`macro` or a :command:`function`.
|
||||
|
||||
.. versionadded:: 3.7
|
||||
The ``PARSE_ARGV`` signature is only for use in a :command:`function`
|
||||
body. In this case the arguments that are parsed come from the
|
||||
body. In this case, the arguments that are parsed come from the
|
||||
``ARGV#`` variables of the calling function. The parsing starts with
|
||||
the ``<N>``-th argument, where ``<N>`` is an unsigned integer.
|
||||
This allows for the values to have special characters like ``;`` in them.
|
||||
|
||||
The ``<options>`` argument contains all options for the respective macro,
|
||||
i.e. keywords which can be used when calling the macro without any value
|
||||
following, like e.g. the ``OPTIONAL`` keyword of the :command:`install`
|
||||
command.
|
||||
The ``<options>`` argument contains all options for the respective function
|
||||
or macro. These are keywords that have no value following them, like the
|
||||
``OPTIONAL`` keyword of the :command:`install` command.
|
||||
|
||||
The ``<one_value_keywords>`` argument contains all keywords for this macro
|
||||
which are followed by one value, like e.g. ``DESTINATION`` keyword of the
|
||||
:command:`install` command.
|
||||
The ``<one_value_keywords>`` argument contains all keywords for this function
|
||||
or macro which are followed by one value, like the ``DESTINATION`` keyword of
|
||||
the :command:`install` command.
|
||||
|
||||
The ``<multi_value_keywords>`` argument contains all keywords for this
|
||||
macro which can be followed by more than one value, like e.g. the
|
||||
function or macro which can be followed by more than one value, like the
|
||||
``TARGETS`` or ``FILES`` keywords of the :command:`install` command.
|
||||
|
||||
.. versionchanged:: 3.5
|
||||
All keywords shall be unique. I.e. every keyword shall only be specified
|
||||
once in either ``<options>``, ``<one_value_keywords>`` or
|
||||
All keywords must be unique. Each keyword can only be specified
|
||||
once in any of the ``<options>``, ``<one_value_keywords>``, or
|
||||
``<multi_value_keywords>``. A warning will be emitted if uniqueness is
|
||||
violated.
|
||||
|
||||
When done, ``cmake_parse_arguments`` will consider for each of the
|
||||
keywords listed in ``<options>``, ``<one_value_keywords>`` and
|
||||
``<multi_value_keywords>`` a variable composed of the given ``<prefix>``
|
||||
followed by ``"_"`` and the name of the respective keyword. These
|
||||
variables will then hold the respective value from the argument list
|
||||
or be undefined if the associated option could not be found.
|
||||
For the ``<options>`` keywords, these will always be defined,
|
||||
to ``TRUE`` or ``FALSE``, whether the option is in the argument list or not.
|
||||
keywords listed in ``<options>``, ``<one_value_keywords>``, and
|
||||
``<multi_value_keywords>``, a variable composed of the given ``<prefix>``
|
||||
followed by ``"_"`` and the name of the respective keyword. For
|
||||
``<one_value_keywords>`` and ``<multi_value_keywords>``, these variables
|
||||
will then hold the respective value(s) from the argument list, or be undefined
|
||||
if the associated keyword was not given (policy :policy:`CMP0174` can also
|
||||
affect the behavior for ``<one_value_keywords>``). For the ``<options>``
|
||||
keywords, these variables will always be defined, and they will be set to
|
||||
``TRUE`` if the keyword is present, or ``FALSE`` if it is not.
|
||||
|
||||
All remaining arguments are collected in a variable
|
||||
``<prefix>_UNPARSED_ARGUMENTS`` that will be undefined if all arguments
|
||||
were recognized. This can be checked afterwards to see
|
||||
whether your macro was called with unrecognized parameters.
|
||||
whether your macro or function was called with unrecognized parameters.
|
||||
|
||||
.. versionadded:: 3.15
|
||||
``<one_value_keywords>`` and ``<multi_value_keywords>`` that were given no
|
||||
@@ -75,9 +76,42 @@ whether your macro was called with unrecognized parameters.
|
||||
policy :policy:`CMP0174` controls whether a corresponding
|
||||
``<prefix>_<keyword>`` variable is defined or not.
|
||||
|
||||
Choose a ``<prefix>`` carefully to avoid clashing with existing variable names.
|
||||
When used inside a function, it is usually suitable to use the prefix ``arg``.
|
||||
There is a very strong convention that all keywords are fully uppercase, so
|
||||
this prefix results in variables of the form ``arg_SOME_KEYWORD``. This makes
|
||||
the code more readable, and it minimizes the chance of clashing with cache
|
||||
variables, which also have a strong convention of being all uppercase.
|
||||
|
||||
Consider the following example macro, ``my_install()``, which takes similar
|
||||
arguments to the real :command:`install` command:
|
||||
.. code-block:: cmake
|
||||
|
||||
function(my_install)
|
||||
set(options OPTIONAL FAST)
|
||||
set(oneValueArgs DESTINATION RENAME)
|
||||
set(multiValueArgs TARGETS CONFIGURATIONS)
|
||||
cmake_parse_arguments(PARSE_ARGV 0 arg
|
||||
"${options}" "${oneValueArgs}" "${multiValueArgs}"
|
||||
)
|
||||
|
||||
# The above will set or unset variables with the following names:
|
||||
# arg_OPTIONAL
|
||||
# arg_FAST
|
||||
# arg_DESTINATION
|
||||
# arg_RENAME
|
||||
# arg_TARGETS
|
||||
# arg_CONFIGURATIONS
|
||||
#
|
||||
# The following will also be set or unset:
|
||||
# arg_UNPARSED_ARGUMENTS
|
||||
# arg_KEYWORDS_MISSING_VALUES
|
||||
|
||||
When used inside a macro, ``arg`` might not be a suitable prefix because the
|
||||
code will affect the calling scope. If another macro also called in the same
|
||||
scope were to use ``arg`` in its own call to ``cmake_parse_arguments()``,
|
||||
and if there are any common keywords between the two macros, the later call's
|
||||
variables can overwrite or remove those of the earlier macro's call.
|
||||
Therefore, it is advisable to incorporate something unique from the macro name
|
||||
in the ``<prefix>``, such as ``arg_lowercase_macro_name``.
|
||||
|
||||
.. code-block:: cmake
|
||||
|
||||
@@ -85,40 +119,63 @@ arguments to the real :command:`install` command:
|
||||
set(options OPTIONAL FAST)
|
||||
set(oneValueArgs DESTINATION RENAME)
|
||||
set(multiValueArgs TARGETS CONFIGURATIONS)
|
||||
cmake_parse_arguments(MY_INSTALL "${options}" "${oneValueArgs}"
|
||||
"${multiValueArgs}" ${ARGN} )
|
||||
|
||||
cmake_parse_arguments(arg_my_install
|
||||
"${options}" "${oneValueArgs}" "${multiValueArgs}"
|
||||
${ARGN}
|
||||
)
|
||||
# ...
|
||||
endmacro()
|
||||
|
||||
Assume ``my_install()`` has been called like this:
|
||||
macro(my_special_install)
|
||||
# NOTE: Has the same keywords as my_install()
|
||||
set(options OPTIONAL FAST)
|
||||
set(oneValueArgs DESTINATION RENAME)
|
||||
set(multiValueArgs TARGETS CONFIGURATIONS)
|
||||
cmake_parse_arguments(arg_my_special_install
|
||||
"${options}" "${oneValueArgs}" "${multiValueArgs}"
|
||||
${ARGN}
|
||||
)
|
||||
# ...
|
||||
endmacro()
|
||||
|
||||
Suppose the above macros are called one after the other, like so:
|
||||
|
||||
.. code-block:: cmake
|
||||
|
||||
my_install(TARGETS foo bar DESTINATION bin OPTIONAL blub CONFIGURATIONS)
|
||||
my_special_install(TARGETS barry DESTINATION sbin RENAME FAST)
|
||||
|
||||
After the ``cmake_parse_arguments`` call the macro will have set or undefined
|
||||
the following variables::
|
||||
After these two calls, the following describes the variables that will be
|
||||
set or unset::
|
||||
|
||||
MY_INSTALL_OPTIONAL = TRUE
|
||||
MY_INSTALL_FAST = FALSE # was not used in call to my_install
|
||||
MY_INSTALL_DESTINATION = "bin"
|
||||
MY_INSTALL_RENAME <UNDEFINED> # was not used
|
||||
MY_INSTALL_TARGETS = "foo;bar"
|
||||
MY_INSTALL_CONFIGURATIONS <UNDEFINED> # was not used
|
||||
MY_INSTALL_UNPARSED_ARGUMENTS = "blub" # nothing expected after "OPTIONAL"
|
||||
MY_INSTALL_KEYWORDS_MISSING_VALUES = "CONFIGURATIONS"
|
||||
# No value for "CONFIGURATIONS" given
|
||||
arg_my_install_OPTIONAL = TRUE
|
||||
arg_my_install_FAST = FALSE # was not present in call to my_install
|
||||
arg_my_install_DESTINATION = "bin"
|
||||
arg_my_install_RENAME <UNSET> # was not present
|
||||
arg_my_install_TARGETS = "foo;bar"
|
||||
arg_my_install_CONFIGURATIONS <UNSET> # was not present
|
||||
arg_my_install_UNPARSED_ARGUMENTS = "blub" # nothing expected after "OPTIONAL"
|
||||
arg_my_install_KEYWORDS_MISSING_VALUES = "CONFIGURATIONS" # value was missing
|
||||
|
||||
You can then continue and process these variables.
|
||||
arg_my_special_install_OPTIONAL = FALSE # was not present
|
||||
arg_my_special_install_FAST = TRUE
|
||||
arg_my_special_install_DESTINATION = "sbin"
|
||||
arg_my_special_install_RENAME <UNSET> # value was missing
|
||||
arg_my_special_install_TARGETS = "barry"
|
||||
arg_my_special_install_CONFIGURATIONS <UNSET> # was not present
|
||||
arg_my_special_install_UNPARSED_ARGUMENTS <UNSET>
|
||||
arg_my_special_install_KEYWORDS_MISSING_VALUES = "RENAME"
|
||||
|
||||
Keywords terminate lists of values, e.g. if directly after a
|
||||
``one_value_keyword`` another recognized keyword follows, this is
|
||||
interpreted as the beginning of the new option. E.g.
|
||||
``my_install(TARGETS foo DESTINATION OPTIONAL)`` would result in
|
||||
``MY_INSTALL_DESTINATION`` set to ``"OPTIONAL"``, but as ``OPTIONAL``
|
||||
is a keyword itself ``MY_INSTALL_DESTINATION`` will be empty (but added
|
||||
to ``MY_INSTALL_KEYWORDS_MISSING_VALUES``) and ``MY_INSTALL_OPTIONAL`` will
|
||||
therefore be set to ``TRUE``.
|
||||
Keywords terminate lists of values. If a keyword is given directly after a
|
||||
``<one_value_keyword>``, that preceding ``<one_value_keyword>`` receives no
|
||||
value and the keyword is added to the ``<prefix>_KEYWORDS_MISSING_VALUES``
|
||||
variable. In the above example, the call to ``my_special_install()`` contains
|
||||
the ``RENAME`` keyword immediately followed by the ``FAST`` keyword.
|
||||
In this case, ``FAST`` terminates processing of the ``RENAME`` keyword.
|
||||
``arg_my_special_install_FAST`` is set to ``TRUE``,
|
||||
``arg_my_special_install_RENAME`` is unset, and
|
||||
``arg_my_special_install_KEYWORDS_MISSING_VALUES`` contains the value
|
||||
``RENAME``.
|
||||
|
||||
See Also
|
||||
^^^^^^^^
|
||||
|
||||
Reference in New Issue
Block a user