cmake/Help/command/cmake_parse_arguments.rst

122 lines
4.8 KiB
ReStructuredText
Raw Normal View History

2016-03-13 13:35:51 +01:00
cmake_parse_arguments
---------------------
2019-11-11 23:01:05 +01:00
Parse function or macro arguments.
2016-03-13 13:35:51 +01:00
2019-11-11 23:01:05 +01:00
.. code-block:: cmake
2016-03-13 13:35:51 +01:00
cmake_parse_arguments(<prefix> <options> <one_value_keywords>
2019-11-11 23:01:05 +01:00
<multi_value_keywords> <args>...)
2016-03-13 13:35:51 +01:00
2019-11-11 23:01:05 +01:00
cmake_parse_arguments(PARSE_ARGV <N> <prefix> <options>
<one_value_keywords> <multi_value_keywords>)
2016-10-30 18:24:19 +01:00
2021-09-14 00:13:48 +02:00
.. versionadded:: 3.5
This command is implemented natively. Previously, it has been defined in the
module :module:`CMakeParseArguments`.
2019-11-11 23:01:05 +01:00
This command is for use in macros or functions.
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>...``.
2016-10-30 18:24:19 +01:00
This may be used in either a :command:`macro` or a :command:`function`.
2021-09-14 00:13:48 +02:00
.. 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
``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.
2016-03-13 13:35:51 +01:00
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 ``<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 ``<multi_value_keywords>`` argument contains all keywords for this
macro which can be followed by more than one value, like e.g. the
``TARGETS`` or ``FILES`` keywords of the :command:`install` command.
2021-09-14 00:13:48 +02:00
.. versionchanged:: 3.5
All keywords shall be unique. I.e. every keyword shall only be specified
once in either ``<options>``, ``<one_value_keywords>`` or
``<multi_value_keywords>``. A warning will be emitted if uniqueness is
violated.
2016-03-13 13:35:51 +01:00
2018-01-26 17:06:56 +01:00
When done, ``cmake_parse_arguments`` will consider for each of the
2016-03-13 13:35:51 +01:00
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
2018-01-26 17:06:56 +01:00
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.
2016-03-13 13:35:51 +01:00
All remaining arguments are collected in a variable
2019-11-11 23:01:05 +01:00
``<prefix>_UNPARSED_ARGUMENTS`` that will be undefined if all arguments
were recognized. This can be checked afterwards to see
2016-03-13 13:35:51 +01:00
whether your macro was called with unrecognized parameters.
2021-09-14 00:13:48 +02:00
.. versionadded:: 3.15
``<one_value_keywords>`` and ``<multi_value_keywords>`` that were given no
values at all are collected in a variable
``<prefix>_KEYWORDS_MISSING_VALUES`` that will be undefined if all keywords
received values. This can be checked to see if there were keywords without
any values given.
2019-11-11 23:01:05 +01:00
Consider the following example macro, ``my_install()``, which takes similar
arguments to the real :command:`install` command:
2016-03-13 13:35:51 +01:00
.. code-block:: cmake
2018-04-23 21:13:27 +02:00
macro(my_install)
2016-03-13 13:35:51 +01:00
set(options OPTIONAL FAST)
set(oneValueArgs DESTINATION RENAME)
set(multiValueArgs TARGETS CONFIGURATIONS)
cmake_parse_arguments(MY_INSTALL "${options}" "${oneValueArgs}"
"${multiValueArgs}" ${ARGN} )
# ...
Assume ``my_install()`` has been called like this:
.. code-block:: cmake
2019-11-11 23:01:05 +01:00
my_install(TARGETS foo bar DESTINATION bin OPTIONAL blub CONFIGURATIONS)
2016-03-13 13:35:51 +01:00
2018-01-26 17:06:56 +01:00
After the ``cmake_parse_arguments`` call the macro will have set or undefined
the following variables::
2016-03-13 13:35:51 +01:00
MY_INSTALL_OPTIONAL = TRUE
2018-01-26 17:06:56 +01:00
MY_INSTALL_FAST = FALSE # was not used in call to my_install
2016-03-13 13:35:51 +01:00
MY_INSTALL_DESTINATION = "bin"
2018-01-26 17:06:56 +01:00
MY_INSTALL_RENAME <UNDEFINED> # was not used
2016-03-13 13:35:51 +01:00
MY_INSTALL_TARGETS = "foo;bar"
2018-01-26 17:06:56 +01:00
MY_INSTALL_CONFIGURATIONS <UNDEFINED> # was not used
MY_INSTALL_UNPARSED_ARGUMENTS = "blub" # nothing expected after "OPTIONAL"
2019-11-11 23:01:05 +01:00
MY_INSTALL_KEYWORDS_MISSING_VALUES = "CONFIGURATIONS"
# No value for "CONFIGURATIONS" given
2016-03-13 13:35:51 +01:00
You can then continue and process these variables.
2019-11-11 23:01:05 +01:00
Keywords terminate lists of values, e.g. if directly after a
``one_value_keyword`` another recognized keyword follows, this is
2016-03-13 13:35:51 +01:00
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``
2019-11-11 23:01:05 +01:00
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``.
2023-05-23 16:38:00 +02:00
See Also
^^^^^^^^
* :command:`function`
* :command:`macro`