Reject extra arguments to a function

AFAIU, any function can be invoked with extra arguments. For example:

function(Takes2Args MyArg1, MyArg2)
  #do stuff
  ...
endfunction()

can be invoked like this without error:

TakesTwoArgs("one" "two" "three")

I’d like a way to make sure that this kind of invocation causes an error or warning.
The best I could come up with right now is add a validation at the start of the function like this:

function(Takes2Args MyArg1, MyArg2)
    if(ARGN)
        message(FATAL_ERROR "Unknown argument(s) passed to function 'Takes2Args': ${ARGN}")
    endif()
   #do stuff
   ...
endfunction()

Since most (all?) of our functions do not expect extra arguments, I’m looking for a way to generalize that, but fails to find one. I tried with a macro, like:

macro(RejectExtraArgs)
    if(ARGN)
        message(FATAL_ERROR "Unknown argument(s)': ${ARGN}")
    endif()
endmacro()

used like this:

function(Takes2Args MyArg1, MyArg2)
   RejectExtraArgs()
   #do stuff
   ...
endfunction()

but this does not seem to work (presumably because the value of ARGN inside the macro has its own value instead of inheriting from the function’s).

Any idea of how a function could be easily marked as incompatible with extra arguments?

Thanks,
Éric

Your approach works with functions, but not macros because macros expand the ARG* variables before executing the code rather than setting the variables and then executing the code. For macros, you’re probably better off doing some ARGC testing or the like.

The way I’ve done this before is to use cmake_parse_arguments and bail if there’s anything unrecognized.