diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 8bd374a..1c1b277 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -18,22 +18,15 @@ jobs: CC: gcc-11 CXX: g++-11 run: | - cd test cmake -B build -DCMAKE_CXX_COMPILER=g++-11 -DCMAKE_C_COMPILER=gcc-11 continue-on-error: false - name: Build test executable - shell: bash - env: - CC: gcc-11 - CXX: g++-11 run: | - cd test/build - make + cd build && make continue-on-error: false - name: Run tests run: | - cd test/build - ./test + ./build/test/run_test continue-on-error: false diff --git a/.gitignore b/.gitignore index a006ac9..5a9f2b4 100644 --- a/.gitignore +++ b/.gitignore @@ -3,9 +3,7 @@ # build files *.exe -test/test -test/build/ -example/build/ +build/ # documentation files /documentation diff --git a/CMakeLists.txt b/CMakeLists.txt index d60ddd2..a6c4b81 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,14 +7,15 @@ else() set(CPP_AP_IS_TOP_LEVEL_PROJECT OFF) endif() -project( - cpp-ap +project(cpp-ap VERSION 1.0 DESCRIPTION "Command-line argument parser for C++20" HOMEPAGE_URL "https://github.com/SpectraL519/cpp-ap" LANGUAGES CXX ) +option(BUILD_TESTS "Build project tests" ON) + # Define the library target add_library(cpp-ap INTERFACE) target_include_directories(cpp-ap INTERFACE @@ -47,7 +48,7 @@ configure_package_config_file( write_basic_package_version_file( ${CMAKE_CURRENT_BINARY_DIR}/cpp-ap-config-version.cmake VERSION ${PROJECT_VERSION} - COMPATIBILITY SameMajorVersion + COMPATIBILITY ExactVersion ) install(FILES @@ -63,11 +64,8 @@ install(EXPORT cpp-ap-targets ) # For FetchContent usage -if (CPP_AP_IS_TOP_LEVEL_PROJECT) - include(CTest) - if (BUILD_TESTING) - add_subdirectory(test) - endif() +if (CPP_AP_IS_TOP_LEVEL_PROJECT AND BUILD_TESTS) + add_subdirectory(test) endif() # Exporting from the build tree diff --git a/README.md b/README.md index c354633..d785393 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,8 @@ The `CPP-AP` library does not require installing any additional tools or heavy l ## Table of contents * [Tutorial](#tutorial) + * [Including CPP-AP into a project](#including-cpp-ap-into-a-project) + * [CMake integration](#cmake-integration) * [The parser class](#the-parser-class) * [Adding arguments](#adding-arguments) * [Argument parameters](#argument-parameters) @@ -48,15 +50,56 @@ The `CPP-AP` library does not require installing any additional tools or heavy l ## Tutorial - +### Including CPP-AP into a project -To use the `CPP-AP` library in your project, copy the [argument_parser.hpp](include/ap/argument_parser.hpp) file into your include directory, e.g. `include/ap`. No other setup is necessary - you only need to include this header in your code: +There are 3 main ways to include the CPP-AP library into a C++ project: -```c++ -#include +#### CMake integration: + +For CMake projects you can simply fetch the library in your `CMakeLists.txt` file: + +```cmake +cmake_minimum_required(VERSION 3.12) + +project(my_project LANGUAGES CXX) + +# Include FetchContent module +include(FetchContent) + +# Fetch CPP-AP library +FetchContent_Declare( + cpp-ap + GIT_REPOSITORY https://github.com/SpectraL519/cpp-ap.git + GIT_TAG master # here you can specify the desired tag or branch +) + +FetchContent_MakeAvailable(cpp-ap) + +# Define the executable for the project +add_executable(my_project main.cpp) + +set_target_properties(my_project PROPERTIES + CXX_STANDARD 20 + CXX_STANDARD_REQUIRED YES +) + +# Link against the cpp-ap library +target_link_libraries(my_project PRIVATE cpp-ap) ``` -If you wish to use the library across multiple projects without copying the header into each one, you can copy it into a common directory and add the `-I ` option when compiling your project. +#### Downloading the library + +If you do not use CMake you can dowload the desired [library release](https://github.com/SpectraL519/cpp-ap/releases), extract it in a desired directory and simply add the `/include` to the include paths of your project. + +#### Downloading the single header + +The core of the library is a [single header file](https://github.com/SpectraL519/cpp-ap/blob/master/include/ap/argument_parser.hpp) so to be able to use the library you can simply download the `argument_parser.hpp` header and paste it into the include directory of your project. + +> [!IMPORTANT] +> To actually use the library in your project simply include the single header in you `main.cpp` file: +> ```c++ +> #include +> ``` ### The parser class @@ -460,39 +503,35 @@ The compiled binaries will appear in the `/example/build/bin` dire First build the testing executable: ```shell -cd /test/ cmake -B build -cd build -make +cd build && make ``` or alternatively: ```shell -cd /test/ mkdir build && cd build cmake .. make ``` +This will build the test executable `run_tests` in the `/build/test` directory. + > [!TIP] > Building on Windows - use the `-G "Unix Makefiles"` option when running CMake to build a GNU Make project instead of a default Visual Studio project. Run the tests: -> [!NOTE] -> The test executable is generated in the `/test/build` directory. - * All tests: ```shell - ./test + ./run_tests ``` * A single test suite: ```shell - ./test -ts="" + ./run_tests -ts="" ``` > [!NOTE] diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8c9af97..461b8d4 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -6,9 +6,8 @@ project(cpp-ap-test) # Structure set(SOURCE_DIRS "source" "app") -set(INCLUDE_DIRS "include" "../include") -set(BINARY_DIR ".") -set(EXECUTABLE_DIR ".") +set(INCLUDE_DIRS "include") +set(EXECUTABLE_DIR "${CMAKE_CURRENT_BINARY_DIR}") # Source files file(GLOB_RECURSE SOURCES "") @@ -17,33 +16,28 @@ foreach(SOURCE_DIR ${SOURCE_DIRS}) list(APPEND SOURCES ${CURRENT_SOURCES}) endforeach() -# Include dirs -include_directories(${INCLUDE_DIRS}) - -# Default compile options -set(DEFAULT_CXX_FLAGS "-Wall -Wextra -Wcast-align -Wconversion -Wunreachable-code -Wuninitialized -pedantic -g -O3") - # Set compile options if(NOT DEFINED CMAKE_CXX_FLAGS) - set(CMAKE_CXX_FLAGS ${DEFAULT_CXX_FLAGS} CACHE STRING "Default C++ compile flags" FORCE) + set(CMAKE_CXX_FLAGS "-Wall -Wextra -Wcast-align -Wconversion -Wunreachable-code -Wuninitialized -pedantic -g -O3" + CACHE STRING "Default C++ compile flags" FORCE) endif() # Executable -add_executable(test ${SOURCES}) +add_executable(run_tests ${SOURCES}) -# Target properties -set_target_properties(test PROPERTIES - RUNTIME_OUTPUT_DIRECTORY "${BINARY_DIR}" +set_target_properties(run_tests PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_DIR}" CXX_STANDARD 20 CXX_STANDARD_REQUIRED YES CXX_EXTENSIONS NO ) -target_compile_options(test PRIVATE ${CMAKE_CXX_FLAGS}) -# Executable path -set(EXECUTABLE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/${EXECUTABLE_DIR}") +target_include_directories(run_tests PRIVATE ${INCLUDE_DIRS}) +target_compile_options(run_tests PRIVATE ${CMAKE_CXX_FLAGS}) +target_link_libraries(run_tests PRIVATE cpp-ap) -# Symbolic links +# Create symbolic links in the build_ut directory +set(EXECUTABLE_DIR "${CMAKE_CURRENT_BINARY_DIR}") foreach(config ${CMAKE_CONFIGURATION_TYPES}) - file(CREATE_LINK "${BINARY_DIR}/test" "${EXECUTABLE_PATH}/test_${config}" SYMBOLIC) + file(CREATE_LINK "${EXECUTABLE_DIR}/run_tests" "${EXECUTABLE_DIR}/test_${config}" SYMBOLIC) endforeach(config)