# defaulting a cached variable if not set by the user command line

**URL:** https://discourse.cmake.org/t/defaulting-a-cached-variable-if-not-set-by-the-user-command-line/9493
**Category:** Usage
**Created:** [November 23, 2023, 11:46am UTC](https://discourse.cmake.org/t/defaulting-a-cached-variable-if-not-set-by-the-user-command-line/9493 "2023-11-23T11:46:01Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [November 23, 2023, 11:46am UTC](https://discourse.cmake.org/t/defaulting-a-cached-variable-if-not-set-by-the-user-command-line/9493/1 "2023-11-23T11:46:01Z")

</div>

My need is roughly the same as in [[CMake] Can an option enforce a default, even if cache is present?](https://cmake.cmake.narkive.com/YXXsXs6H/can-an-option-enforce-a-default-even-if-cache-is-present).

I understand, from this thread, that cmake may relaunch a configuration by itself, without using the command line arguments but looking into the cache.

Let say, for instance, that I have an option to use -Werror warning option, lets call it WAE.  
  
If the user don’t set value to WAE or write -DWAE=OFF, I would expect the WAE variable to be set to OFF (inside the cache).  
  
If the user write -DWAE=ON, I would expect the WAE variable to be set to ON (inside the cache).

Is it possible to achieve this behavior? (beyond merely documenting that WAE is mandatory?)  
  
Maybe I could unset the variable at the end of configuration/generation but how can I do that (in the scenario where cmake has to relaunch itself, the unset must not occurred until the task has successfully terminated)?

Thanks,  
A.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [November 23, 2023, 9:29pm UTC](https://discourse.cmake.org/t/defaulting-a-cached-variable-if-not-set-by-the-user-command-line/9493/2 "2023-11-23T21:29:23Z")

</div>

In your project, check if WAE is defined. If it isn’t, set a regular non-cache variable WAE to whatever value you want as the default when the user doesn’t specify anything. Don’t define any WAE cache variable yourself. This way, if there _is_ a cache variable by that name, you know the user created it.

This isn’t perfect. The main drawback is that the user has to know about WAE in order to use it. They won’t see it in the cache unless they set it themselves.

---

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [November 24, 2023, 11:42am UTC](https://discourse.cmake.org/t/defaulting-a-cached-variable-if-not-set-by-the-user-command-line/9493/3 "2023-11-24T11:42:43Z")

</div>

Hi,

Thanks for your answer. In order to be sure to understand well: when a variable `MYVAR` is set through CLI, it is equivalent to a set(`MYVAR` CACHE FORCE)?

Regards,  
A.

---

<div class="post-metadata">

### Author: ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)
#### Post date: [November 24, 2023, 12:00pm UTC](https://discourse.cmake.org/t/defaulting-a-cached-variable-if-not-set-by-the-user-command-line/9493/4 "2023-11-24T12:00:43Z")

</div>

> [@adaldev](#):
>
> when a variable `MYVAR` is set through CLI, it is equivalent to a set(`MYVAR` CACHE FORCE)?

That’s right.
