From e1d72aaea77600b84bdc80074bcaf08b4d9811eb Mon Sep 17 00:00:00 2001 From: "killua.z" Date: Mon, 4 May 2026 13:52:08 +0400 Subject: [PATCH] chore: initial examples folder. --- examples/test/CMakeLists.txt | 11 ++++++ examples/test/compile_commands.json | 8 +++++ examples/test/main.cpp | 56 +++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 examples/test/CMakeLists.txt create mode 100644 examples/test/compile_commands.json create mode 100644 examples/test/main.cpp diff --git a/examples/test/CMakeLists.txt b/examples/test/CMakeLists.txt new file mode 100644 index 0000000..8ec4221 --- /dev/null +++ b/examples/test/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.10) + +project(test) + +set(CMAKE_BUILD_TYPE Debug) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +find_package(argument_parser REQUIRED) +add_executable(main main.cpp) +target_link_libraries(main argument_parser) diff --git a/examples/test/compile_commands.json b/examples/test/compile_commands.json new file mode 100644 index 0000000..f8de2ab --- /dev/null +++ b/examples/test/compile_commands.json @@ -0,0 +1,8 @@ +[ +{ + "directory": "/Users/killua/Projects/c++/argparser/argument-parser/examples/test/build", + "command": "/usr/bin/c++ -isystem /usr/local/include/argparse -isystem /usr/local/include/argparse/parser -isystem /usr/local/include/argparse/conventions -isystem /usr/local/include/argparse/conventions/implementations -isystem /usr/local/include/argparse/parser/platform_headers -isystem /usr/local/include/argparse/parser/parsing_traits -g -std=gnu++17 -arch arm64 -o CMakeFiles/main.dir/main.cpp.o -c /Users/killua/Projects/c++/argparser/argument-parser/examples/test/main.cpp", + "file": "/Users/killua/Projects/c++/argparser/argument-parser/examples/test/main.cpp", + "output": "/Users/killua/Projects/c++/argparser/argument-parser/examples/test/build/CMakeFiles/main.dir/main.cpp.o" +} +] diff --git a/examples/test/main.cpp b/examples/test/main.cpp new file mode 100644 index 0000000..bc1affc --- /dev/null +++ b/examples/test/main.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include + +using argument = argument_parser::builder::argument<>; + +auto echo(std::string const& s) -> void { + std::cout << s << '\n'; +} + +auto main() -> int { + argument_parser::v2::parser parser(false); + + argument::start() + .positional("count") + .position(0) + .help_text("How many times to repeat the action.") + .required() + .action([](int const& count) { + std::cout << "count action configured for " << count << '\n'; + }) + .build(parser); + + int captured_value = 0; + argument::start() + .long_argument("threshold") + .help_text("Store the parsed value through a reference.") + .reference(captured_value) + .build(parser); + + argument::start() + .short_argument("q") + .help_text("Store a boolean flag.") + .build(parser); + + argument::start() + .long_argument("output") + .help_text("Store a string value.") + .store() + .build(parser); + + argument::start() + .short_argument("e") + .long_argument("echo") + .help_text("Echo the parsed value.") + .action(echo) + .build(parser); + + parser.handle_arguments({ + &argument_parser::conventions::gnu_argument_convention + }); + + return 0; +}