separate_arguments.rst 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. separate_arguments
  2. ------------------
  3. Parse command-line arguments into a semicolon-separated list.
  4. .. code-block:: cmake
  5. separate_arguments(<variable> <mode> [PROGRAM [SEPARATE_ARGS]] <args>)
  6. Parses a space-separated string ``<args>`` into a list of items,
  7. and stores this list in semicolon-separated standard form in ``<variable>``.
  8. This function is intended for parsing command-line arguments.
  9. The entire command line must be passed as one string in the
  10. argument ``<args>``.
  11. The exact parsing rules depend on the operating system.
  12. They are specified by the ``<mode>`` argument which must
  13. be one of the following keywords:
  14. ``UNIX_COMMAND``
  15. Arguments are separated by unquoted whitespace.
  16. Both single-quote and double-quote pairs are respected.
  17. A backslash escapes the next literal character (``\"`` is ``"``);
  18. there are no special escapes (``\n`` is just ``n``).
  19. ``WINDOWS_COMMAND``
  20. A Windows command-line is parsed using the same
  21. syntax the runtime library uses to construct argv at startup. It
  22. separates arguments by whitespace that is not double-quoted.
  23. Backslashes are literal unless they precede double-quotes. See the
  24. MSDN article `Parsing C Command-Line Arguments`_ for details.
  25. ``NATIVE_COMMAND``
  26. .. versionadded:: 3.9
  27. Proceeds as in ``WINDOWS_COMMAND`` mode if the host system is Windows.
  28. Otherwise proceeds as in ``UNIX_COMMAND`` mode.
  29. ``PROGRAM``
  30. .. versionadded:: 3.19
  31. The first item in ``<args>`` is assumed to be an executable and will be
  32. searched in the system search path or left as a full path. If not found,
  33. ``<variable>`` will be empty. Otherwise, ``<variable>`` is a list of 2
  34. elements:
  35. 0. Absolute path of the program
  36. 1. Any command-line arguments present in ``<args>`` as a string
  37. For example:
  38. .. code-block:: cmake
  39. separate_arguments (out UNIX_COMMAND PROGRAM "cc -c main.c")
  40. * First element of the list: ``/path/to/cc``
  41. * Second element of the list: ``" -c main.c"``
  42. ``SEPARATE_ARGS``
  43. When this sub-option of ``PROGRAM`` option is specified, command-line
  44. arguments will be split as well and stored in ``<variable>``.
  45. For example:
  46. .. code-block:: cmake
  47. separate_arguments (out UNIX_COMMAND PROGRAM SEPARATE_ARGS "cc -c main.c")
  48. The contents of ``out`` will be: ``/path/to/cc;-c;main.c``
  49. .. _`Parsing C Command-Line Arguments`: https://learn.microsoft.com/en-us/cpp/c-language/parsing-c-command-line-arguments
  50. .. code-block:: cmake
  51. separate_arguments(<var>)
  52. Convert the value of ``<var>`` to a semi-colon separated list. All
  53. spaces are replaced with ';'. This helps with generating command
  54. lines.