Order of arguments in commands

Should the arguments be given to commands in the same order as in command documentation? For example, there’s install command in tutorial:

install(EXPORT MathFunctionsTargets
FILE MathFunctionsTargets.cmake
DESTINATION lib/cmake/MathFunctions
)

Here the order of options is EXPORT, FILE and DESTINATION but in the documentation the order is EXPORT, DESTINATION and FILE.

It seems that in many commands any order will work but is that by design?

In most commands the only important thing is to match ARG_NAME and ARG_VALUE correctly and the order of those doesn’t matter. It, of course, only stands if there are named arguments. Positional arguments have fixed order.

Does that also apply to arguments without value? For example, can EXCLUDE_FROM_ALL in install command be placed anywhere in the command invocation?

What about the commands where the order of arguments do matter? Is it always mentioned in the documentation or how can one know in which commands the order matters and in which it doesn’t?

The only positional arguments can appear in the beginning of the argument list. Anything without a dedicated KEYWORD-like argument signifier can be assumed to be positional (e.g., in add_library(tgt), tgt is positional and must be first).

1 Like