Skip to content

Commit

Permalink
readme, gitignore, test workflow, cmake align
Browse files Browse the repository at this point in the history
  • Loading branch information
SpectraL519 committed May 15, 2024
1 parent 045ebf7 commit f181551
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 53 deletions.
11 changes: 2 additions & 9 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

# build files
*.exe
test/test
test/build/
example/build/
build/

# documentation files
/documentation
14 changes: 6 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
67 changes: 53 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) <!-- here add the link to the cmake integration bulletpoint -->
* [The parser class](#the-parser-class)
* [Adding arguments](#adding-arguments)
* [Argument parameters](#argument-parameters)
Expand All @@ -48,15 +50,56 @@ The `CPP-AP` library does not require installing any additional tools or heavy l

## Tutorial

<!-- TODO: download project release and add include directory to the project -->
### 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 <ap/argument_parser.hpp>
#### 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 <argument-parser-dir>` 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 `<cpp-ap-dir>/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 <ap/argument_parser.hpp>
> ```
### The parser class
Expand Down Expand Up @@ -460,39 +503,35 @@ The compiled binaries will appear in the `<project-root>/example/build/bin` dire
First build the testing executable:
```shell
cd <project-root>/test/
cmake -B build
cd build
make
cd build && make
```
or alternatively:
```shell
cd <project-root>/test/
mkdir build && cd build
cmake ..
make
```
This will build the test executable `run_tests` in the `<project-root>/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 `<project-root>/test/build` directory.

* All tests:
```shell
./test
./run_tests
```
* A single test suite:
```shell
./test -ts="<test-suite-name>"
./run_tests -ts="<test-suite-name>"
```
> [!NOTE]
Expand Down
32 changes: 13 additions & 19 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 "")
Expand All @@ -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)

0 comments on commit f181551

Please sign in to comment.