Skip to content

Commit ebf098d

Browse files
authored
feat: add executable target (#63)
* feat: add `my_fibonacci_main` executable target * feat: use argparse to parse arguments in the main executable * ci: exclude `src/main.cpp` from test coverage check
1 parent 6501f60 commit ebf098d

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

.github/workflows/ci.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,7 @@ jobs:
4747
- name: Check coverage
4848
uses: threeal/[email protected]
4949
with:
50-
excludes: build/*
50+
excludes: |
51+
build/*
52+
src/main.cpp
5153
fail-under-line: 80

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ file(DOWNLOAD https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.38.7/C
77
include(${CMAKE_BINARY_DIR}/_deps/CPM.cmake)
88
cpmusepackagelock(package-lock)
99

10+
cpmgetpackage(argparse)
1011
cpmgetpackage(CheckWarning.cmake)
1112

1213
add_library(my_fibonacci src/sequence.cpp)
1314
target_include_directories(my_fibonacci PUBLIC include)
1415
set_property(TARGET my_fibonacci PROPERTY CXX_STANDARD 11)
1516
target_check_warning(my_fibonacci)
1617

18+
add_executable(my_fibonacci_main src/main.cpp)
19+
target_link_libraries(my_fibonacci_main PUBLIC argparse my_fibonacci)
20+
set_property(TARGET my_fibonacci_main PROPERTY CXX_STANDARD 11)
21+
target_check_warning(my_fibonacci_main)
22+
1723
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
1824
cpmgetpackage(Format.cmake)
1925

package-lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# CPM Package Lock
22
# This file should be committed to version control
33

4+
# argparse
5+
CPMDeclarePackage(argparse
6+
VERSION 3.0
7+
GITHUB_REPOSITORY p-ranav/argparse
8+
SYSTEM YES
9+
EXCLUDE_FROM_ALL YES
10+
)
411
# CheckWarning.cmake
512
CPMDeclarePackage(CheckWarning.cmake
613
VERSION 1.0.0

src/main.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <argparse/argparse.hpp>
2+
#include <cstdlib>
3+
#include <iostream>
4+
#include <my_fibonacci/sequence.hpp>
5+
6+
int main(int argc, char** argv) {
7+
argparse::ArgumentParser program("my_fibonacci_main");
8+
program.add_description(
9+
"Generate a Fibonacci sequence up to the given number of terms.");
10+
program.add_argument("n").help("The number of terms").scan<'i', int>();
11+
12+
try {
13+
program.parse_args(argc, argv);
14+
} catch (const std::exception& err) {
15+
std::cerr << err.what() << std::endl;
16+
std::cerr << program;
17+
return 1;
18+
}
19+
20+
const auto n = program.get<int>("n");
21+
const auto sequence = my_fibonacci::fibonacci_sequence(n);
22+
for (auto val : sequence) {
23+
std::cout << val << " ";
24+
}
25+
std::cout << std::endl;
26+
27+
return 0;
28+
}

0 commit comments

Comments
 (0)