# How to check variables specified with -D command line option?

**URL:** <https://discourse.cmake.org/t/how-to-check-variables-specified-with-d-command-line-option/2270>\
**Category:** Code\
**Created:** [December 1, 2020, 3:01am UTC](https://discourse.cmake.org/t/how-to-check-variables-specified-with-d-command-line-option/2270 "2020-12-01T03:01:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomoreilly](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/439d5e/32.png) [@tomoreilly](https://discourse.cmake.org/u/tomoreilly)\
**Post date:** [December 1, 2020, 3:01am UTC](https://discourse.cmake.org/t/how-to-check-variables-specified-with-d-command-line-option/2270/1 "2020-12-01T03:01:42Z")

</div>

I’d like my users to be able to set various variable values on the command line. So the cmake ‘-D’ option does this. But how can I insure that the user has spelled the variable correctly on the command line? E.g. suppose the variable is GMT\_INCLUDE\_DIR; then the user could type:

`cmake -D GMT_INCLUDE_DIR=/usr/local/include/gmt ..`

But what if the user accidentally types:

`cmake -D GMG_INCLUDE_DIRECTORY=/usr/local/include/gmt ..`

How can CMakeLists.txt detect the misspelled variable name? E.g. can CMakeLists.txt get the names of all variables specified with the -D option?

---

<div class="post-metadata">

**Author:** ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)\
**Post date:** [December 1, 2020, 9:53am UTC](https://discourse.cmake.org/t/how-to-check-variables-specified-with-d-command-line-option/2270/2 "2020-12-01T09:53:27Z")

</div>

First, you can check in the CMake code whether the expected variable is defined

```cmake
if(DEFINED GMT_INCLUDE_DIR)
  # do something with that variable
else()
  message(FATAL_ERROR "GMT_INCLUDE_DIR must be defined")
endif()

```

Then, you can query the `VARIABLES` CMake property to get all defined variables:

```auto
get_cmake_property(all_variables VARIABLES)

```

but that doesn’t tell you whether they were specified with the `-D` option.

---

<div class="post-metadata">

**Author:** ![snikulov](https://discourse.cmake.org/user_avatar/discourse.cmake.org/snikulov/32/194_2.png) [@snikulov](https://discourse.cmake.org/u/snikulov)\
**Post date:** [December 1, 2020, 9:55am UTC](https://discourse.cmake.org/t/how-to-check-variables-specified-with-d-command-line-option/2270/3 "2020-12-01T09:55:02Z")

</div>

Usually, you’ll see a warning when run CMake with an unused variable

```
cmake -S. -Bbuild -DNOT_USED_OPTION=ON

```

Will give you

```
CMake Warning:
  Manually-specified variables were not used by the project:

    NOT_USED_OPTION

```

Also, you’ll see same warn for misspelled variables.

HTH.
