diff --git a/CMakeLists.txt b/CMakeLists.txt index 3f61050..3381acd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,8 @@ project(argparse LANGUAGES CXX VERSION 0.1.0.0) set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_STANDARD 11) +add_subdirectory(examples) + # "this command should be in the source directory root for CTest to find the test file" enable_testing() add_subdirectory(test) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..f87ea37 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,31 @@ +macro(add_args tgt) +target_compile_options( + ${tgt} + PUBLIC + -Wall; + -Wextra; + -Wpedantic; + -Wcast-align; + -Wdisabled-optimization; + -Winit-self; + -Wlogical-op; + -Wmissing-include-dirs; + -Woverloaded-virtual; + -Wpointer-arith; + -Wshadow; + -Wstrict-aliasing; + -Wswitch-enum; + -Wundef; + -Wvla; + -Wformat=2; +) +endmacro() + +macro(add_example EXE SRCS) + add_executable(${EXE} ${SRCS}) + add_args(${EXE}) + target_include_directories(${EXE} SYSTEM PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../include) +endmacro() + +add_example(example1 example1.cpp) + diff --git a/examples/example1.cpp b/examples/example1.cpp new file mode 100644 index 0000000..2d2fc0d --- /dev/null +++ b/examples/example1.cpp @@ -0,0 +1,45 @@ +#include "argparse/argparse.hpp" + +int main(int argc, char **argv) { + + // A parser object + Parser p; + + // Program data corresponding to flags, options, and positional arguments + bool verbose = false; + std::string toPrint; + std::string maybePrint; + int repeats = 1; + + // Inform the parser of the program data. + // It will do type-specific conversion + p.add_option(repeats, "--repeat"); + p.add_flag(verbose, "--verbose", "-v"); + p.add_positional(toPrint)->required(); + auto psnl = p.add_positional(maybePrint); + + // If there was an error during parsing, report it. + if (!p.parse(argc, argv)) { + std::cerr << p.help(); + exit(EXIT_FAILURE); + } + + // Execute the program logic + if (verbose) { + std::cerr << "about to print '" << toPrint << "' " << repeats << " times"; + if (psnl->found()) { + std::cerr << ", then '" << maybePrint << "'"; + } + std::cerr << std::endl; + } + + for (int i = 0; i < repeats; ++i) { + std::cout << toPrint; + } + if (psnl->found()) { + std::cout << maybePrint; + } + std::cout << std::endl; + + return 0; +} \ No newline at end of file