Setting and restoring variables (or: when you wish `block()` worked differently…)

I find this idiom awkward. I use if(YES) just to get some visual indication of the scope-like behavior here. Is there a better way to do this?

# Not using block() here because FetchContent_MakeAvailable typically causes dependency-specific
# global variables to be set, and I'm not sure to what extent they may be needed
if(YES)
  set(saved_BUILD_EXAMPLES ${BUILD_EXAMPLES})
  set(saved_BUILD_EXAMPLES ${BUILD_EXAMPLES})

  set(BUILD_EXAMPLES NO)
  set(BUILD_TESTING NO)

  FetchContent_MakeAvailable(ArgumentParser SwiftSyntax)

  set(BUILD_EXAMPLES ${saved_BUILD_EXAMPLES})
  set(BUILD_EXAMPLES ${saved_BUILD_EXAMPLES})
endif()

…And my copy/paste error above shows exactly why something better is needed. It should have been:

# Not using block() here because FetchContent_MakeAvailable typically causes dependency-specific
# global variables to be set, and I'm not sure to what extent they may be needed
if(YES)
  set(saved_BUILD_EXAMPLES ${BUILD_EXAMPLES})
  set(saved_BUILD_TESTING ${BUILD_TESTING})

  set(BUILD_EXAMPLES NO)
  set(BUILD_TESTING NO)

  FetchContent_MakeAvailable(ArgumentParser SwiftSyntax)

  set(BUILD_EXAMPLES ${saved_BUILD_EXAMPLES})
  set(BUILD_TESTING ${saved_BUILD_TESTING})
endif()

No, I’m not aware of an obviously better way to do this. A macro would do, but then you have a macro.