Commit f9cb7bdb by Maarten L. Hekkelman

clean up

parent 20c95733
# Copyright Maarten L. Hekkelman, 2022
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
cmake_minimum_required(VERSION 3.16)
# set the project name
project(libconfig VERSION 1.2.0 LANGUAGES CXX)
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
include(Dart)
set(CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD 17 CACHE STRING "The minimum version of C++ required for this library")
set(CMAKE_CXX_STANDARD_REQUIRED ON)
option(ENABLE_TESTING "Build the unit test applications" OFF)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers")
elseif(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
endif()
if(MSVC)
# make msvc standards compliant...
add_compile_options(/permissive-)
macro(get_WIN32_WINNT version)
if(WIN32 AND CMAKE_SYSTEM_VERSION)
set(ver ${CMAKE_SYSTEM_VERSION})
string(REPLACE "." "" ver ${ver})
string(REGEX REPLACE "([0-9])" "0\\1" ver ${ver})
set(${version} "0x${ver}")
endif()
endmacro()
get_WIN32_WINNT(ver)
add_definitions(-D_WIN32_WINNT=${ver})
endif()
add_library(libconfig INTERFACE)
add_library(libconfig::libconfig ALIAS libconfig)
target_include_directories(libconfig INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
# adding header sources just helps IDEs
target_sources(libconfig INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>$<INSTALL_INTERFACE:include>/cfg.hpp
)
set_target_properties(libconfig PROPERTIES PUBLIC_HEADER include/cfg.hpp)
# installation
set(version_config "${CMAKE_CURRENT_BINARY_DIR}/libconfigConfigVersion.cmake")
set(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR})
write_basic_package_version_file("${version_config}"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion)
install(TARGETS libconfig
EXPORT libconfigConfig
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(
DIRECTORY include/cfg
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
COMPONENT Devel
)
export(TARGETS libconfig NAMESPACE libconfig:: FILE libconfigTargets.cmake)
if(WIN32 AND NOT CYGWIN)
set(CONFIG_LOC CMake)
else()
set(CONFIG_LOC "${CMAKE_INSTALL_LIBDIR}/cmake/libconfig")
endif()
configure_package_config_file(
${PROJECT_SOURCE_DIR}/cmake/libconfigConfig.cmake.in
${PROJECT_SOURCE_DIR}/cmake/libconfigConfig.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/libconfig
PATH_VARS INCLUDE_INSTALL_DIR
)
install(EXPORT libconfigConfig
FILE libconfigTargets.cmake
NAMESPACE libconfig::
DESTINATION ${CONFIG_LOC})
install(
FILES cmake/libconfigConfig.cmake "${version_config}"
DESTINATION ${CONFIG_LOC})
# if(ENABLE_TESTING)
# enable_testing()
# add_executable(libconfig-unit-test ${PROJECT_SOURCE_DIR}/test/unit-test.cpp)
# target_link_libraries(libconfig-unit-test libconfig::libconfig)
# if(MSVC)
# # Specify unwind semantics so that MSVC knowns how to handle exceptions
# target_compile_options(libconfig-unit-test PRIVATE /EHsc)
# endif()
# add_test(NAME libconfig-unit-test
# COMMAND $<TARGET_FILE:libconfig-unit-test> -- ${PROJECT_SOURCE_DIR}/test)
# endif()
# libconfig
A library for parsing command line arguments and making them available throughout a program.
## Synopsis
```c++
#include <cfg.hpp>
int VERBOSE = 0;
int main(int argc, char * const argv[])
{
// config is a singleton class
auto &config = cfg::config::instance();
// Tell config what options to accept
// This can be done more than once, replacing
// the current set of options.
config.init("usage: test [options] input output",
cfg::make_option("verbose,v", "Use verbose output"),
cfg::make_option("help,h", "Show this help"),
cfg::make_option<std::string>("opt1", "foo", "First option"),
cfg::make_option<std::string>("config", "Name of a config file to use"),
cfg::make_option<float>("float-value,f", "Another option")
);
// Do the parsing of argv
config.parse(argc, argv);
// Set verbose based on number of times the
// -v or --verbose flag was passed
VERBOSE = config.count("verbose");
// Check for the help flag
if (config.has("help"))
{
// print out the options to the screen
std::cout << config << std::endl;
exit(0);
}
// Optionally read a config file
// Config file is by default called myapp.conf
// but can be overridden with the --config parameter
// Files will be located in current dictory and /etc
if (config.has("config"))
config.parse_config_file("config", "myapp.conf",
{ fs::current_path().string(), "/etc/" }
);
// Operands are parameters that are not options
// E.g. filenames to process
if (config.operands().size() != 2)
{
std::cerr << config << std::endl;
exit(1);
}
std::filesystem::path input = config.operands().front();
std::filesystem::path output = config.operands().back();
// Get values from options with a value
std::option1 = config.get<std::string>("opt1");
float option2 = 3.14;
if (config.has("float-value"))
option2 = config.get<float>("float-value");
... etc
}
```
Version 1.1.1
- Better support for older GCC compilers
Version 1.2.0
- New, tuples base implementation
- Add 'usage' parameter to init
Version 1.1.0
- Parse config files
Version 1.0.2
- Fix get_terminal_width. Should be inline of course.
Version 1.0.1
- printing options, word-wrapped
Version 1.0.0
- initial release
\ No newline at end of file
####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() #######
####### Any changes to this file will be overwritten by the next CMake run ####
####### The input file was libconfigConfig.cmake.in ########
get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)
macro(set_and_check _var _file)
set(${_var} "${_file}")
if(NOT EXISTS "${_file}")
message(FATAL_ERROR "File or directory ${_file} referenced by variable ${_var} does not exist !")
endif()
endmacro()
macro(check_required_components _NAME)
foreach(comp ${${_NAME}_FIND_COMPONENTS})
if(NOT ${_NAME}_${comp}_FOUND)
if(${_NAME}_FIND_REQUIRED_${comp})
set(${_NAME}_FOUND FALSE)
endif()
endif()
endforeach()
endmacro()
####################################################################################
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/libconfigTargets.cmake")
check_required_components(libconfig)
@PACKAGE_INIT@
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/libconfigTargets.cmake")
check_required_components(libconfig)
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2022 Maarten L. Hekkelman
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <climits>
#include <cstdint>
#if __has_include(<sys/ioctl.h>)
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#elif defined(_MSC_VER)
#include <windows.h>
#endif
namespace cfg
{
#if defined(_MSC_VER)
/// @brief Get the width in columns of the current terminal
/// @return number of columns of the terminal
inline uint32_t get_terminal_width()
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
::GetConsoleScreenBufferInfo(::GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
return csbi.srWindow.Right - csbi.srWindow.Left + 1;
}
#elif __has_include(<sys/ioctl.h>)
/// @brief Get the width in columns of the current terminal
/// @return number of columns of the terminal
inline uint32_t get_terminal_width()
{
uint32_t result = 80;
if (::isatty(STDOUT_FILENO))
{
struct winsize w;
::ioctl(0, TIOCGWINSZ, &w);
result = w.ws_col;
}
return result;
}
#else
#warning "Could not find the terminal width, falling back to default"
inline uint32_t get_terminal_width()
{
return 80;
}
#endif
} // namespace cfg
\ No newline at end of file
# A simple test config file
aap = 2
noot = 3
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment