Skip to content

Commit

Permalink
adds cmaize_option_list
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmrichard committed Dec 11, 2023
1 parent 98b1320 commit 54c5ca1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
51 changes: 51 additions & 0 deletions cmake/cmaize/user_api/cmaize_option.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,54 @@ function(cmaize_option _co_name _co_default_value)
set("${_co_name}" "${_co_value}" PARENT_SCOPE)

endfunction()

#[[[
# Convenience function for registering a number of options with CMaize.
#
# Many projects have multiple configuration options. Rather than having to call
# ``cmaize_option`` for each option, ``cmaize_option_list`` allows you to
# provide a list of key/value pairs. Under the hood this function simply loops
# over the key/value pairs and forwards them to ``cmaize_option``, thus all
# documentation relating to ``cmaize_option`` applies here as well.
#
# :param *args: A variadiac list of key/value pairs. The ``2i``-th argument will
# be interpretted as the key for the ``i``-th pair and the ``2i + 1``-th
# argument will be interpretted as the value for the ``i``-th pair.
# :type *args: str
#
# :raises input_error: If the number of arguments is not divisible by 2.
#]]
function(cmaize_option_list)
set(_col_nargs "${ARGC}")

# Early out if no arguments were provided (otherwise math below won't work)
if("${ARGC}" EQUAL "0")
return()
endif()


# Verify we got an even number of arguments
math(EXPR _col_is_even "${_col_nargs} % 2")
if(NOT "${_col_is_even}" EQUAL "0")
cpp_raise(
input_error
"Syntax: cmaize_option_list(key0 value0 key1 value1 ...)"
)
endif()

# Loops are inclusive on the end point, so subtract 1
math(EXPR _col_nargs "${_col_nargs} - 1")

# ARGN isn't actually a variable, so make a list from its contents
set(_col_argn "${ARGN}")

# Loop over pairs forwarding them to cmaize_option
foreach(_col_i RANGE 0 "${_col_nargs}" 2)
set(_col_key_i "${_col_i}")
math(EXPR _col_value_i "${_col_i} + 1")
list(GET _col_argn ${_col_key_i} _col_key)
list(GET _col_argn ${_col_value_i} _col_value)
cmaize_option("${_col_key}" "${_col_value}")
endforeach()

endfunction()
1 change: 1 addition & 0 deletions cmake/cmaize/user_api/user_api.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ include(cmaize/user_api/add_executable)
include(cmaize/user_api/add_library)
include(cmaize/user_api/add_package)
include(cmaize/user_api/add_tests)
include(cmaize/user_api/cmaize_option)
include(cmaize/user_api/cmaize_project)
include(cmaize/user_api/find_or_build_dependency)
42 changes: 42 additions & 0 deletions tests/cmaize/user_api/test_cmaize_option.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,45 @@ function("${test_cmaize_option}")

endfunction()
endfunction()

ct_add_test(NAME "test_cmaize_option_list")
function("${test_cmaize_option_list}")
include(cmaize/project/projects)
include(cmaize/user_api/cmaize_option)
include(cmaize/user_api/cmaize_project)

project("ATestCMaizeProject")
cmaize_project("ATestCMaizeProject")
cpp_get_global(proj_obj CMAIZE_TOP_PROJECT)

ct_add_section(NAME "zero_arguments")
function("${zero_arguments}")
cmaize_option_list()
endfunction()

ct_add_section(NAME "one_argument" EXPECTFAIL)
function("${one_argument}")
cmaize_option_list("foo")
endfunction()

ct_add_section(NAME "two_arguments")
function("${two_arguments}")
cmaize_option_list("foo" 42)
ct_assert_equal(foo 42)
endfunction()

ct_add_section(NAME "three_arguments" EXPECTFAIL)
function("${three_arguments}")
cmaize_option_list("foo" 42 "bar")
endfunction()

ct_add_section(NAME "four_arguments")
function("${four_arguments}")
cmaize_option_list(
"foo" 42
"bar" "hello"
)
ct_assert_equal(foo 42)
ct_assert_equal(bar "hello")
endfunction()
endfunction()

0 comments on commit 54c5ca1

Please sign in to comment.