diff --git a/include/argparse/argparse.hpp b/include/argparse/argparse.hpp index e0fb880..5e826c5 100644 --- a/include/argparse/argparse.hpp +++ b/include/argparse/argparse.hpp @@ -1200,8 +1200,8 @@ public: * @throws std::runtime_error in case of any invalid argument */ // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays) - void parse_known_args(int argc, const char *const argv[]) { - parse_known_args({argv, argv + argc}); + auto parse_known_args(int argc, const char *const argv[]) { + return parse_known_args({argv, argv + argc}); } /* Getter for options with default values. diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 5404b74..b146b2d 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -36,4 +36,11 @@ add_sample(is_used) add_sample(joining_repeated_optional_arguments) add_sample(repeating_argument_to_increase_value) add_sample(negative_numbers) -add_sample(subcommands) \ No newline at end of file +add_sample(description_epilog_metavar) +add_sample(list_of_arguments) +add_sample(compound_arguments) +add_sample(gathering_remaining_arguments) +add_sample(subcommands) +add_sample(parse_known_args) +add_sample(custom_prefix_characters) +add_sample(custom_assignment_characters) \ No newline at end of file diff --git a/samples/compound_arguments.cpp b/samples/compound_arguments.cpp new file mode 100644 index 0000000..c44d02d --- /dev/null +++ b/samples/compound_arguments.cpp @@ -0,0 +1,36 @@ +#include + +int main(int argc, char *argv[]) { + argparse::ArgumentParser program("test"); + + program.add_argument("-a").default_value(false).implicit_value(true); + + program.add_argument("-b").default_value(false).implicit_value(true); + + program.add_argument("-c") + .nargs(2) + .default_value(std::vector{0.0f, 0.0f}) + .scan<'g', float>(); + + try { + program.parse_args(argc, argv); // Example: ./main -abc 1.95 2.47 + } catch (const std::runtime_error &err) { + std::cerr << err.what() << std::endl; + std::cerr << program; + std::exit(1); + } + + auto a = program.get("-a"); // true + auto b = program.get("-b"); // true + auto c = program.get>("-c"); // {1.95, 2.47} + + std::cout << "a: " << std::boolalpha << a << "\n"; + std::cout << "b: " << b << "\n"; + if (!c.empty()) { + std::cout << "c: "; + for (auto &v : c) { + std::cout << v << " "; + } + std::cout << std::endl; + } +} \ No newline at end of file diff --git a/samples/custom_assignment_characters.cpp b/samples/custom_assignment_characters.cpp new file mode 100644 index 0000000..7d9ab39 --- /dev/null +++ b/samples/custom_assignment_characters.cpp @@ -0,0 +1,27 @@ +#include +#include + +int main(int argc, char *argv[]) { + argparse::ArgumentParser program("test"); + program.set_prefix_chars("-+/"); + program.set_assign_chars("=:"); + + program.add_argument("--foo"); + program.add_argument("/B"); + + try { + program.parse_args(argc, argv); + } catch (const std::runtime_error &err) { + std::cerr << err.what() << std::endl; + std::cerr << program; + std::exit(1); + } + + if (program.is_used("--foo")) { + std::cout << "--foo : " << program.get("--foo") << "\n"; + } + + if (program.is_used("/B")) { + std::cout << "/B : " << program.get("/B") << "\n"; + } +} \ No newline at end of file diff --git a/samples/custom_prefix_characters.cpp b/samples/custom_prefix_characters.cpp new file mode 100644 index 0000000..cdce04d --- /dev/null +++ b/samples/custom_prefix_characters.cpp @@ -0,0 +1,31 @@ +#include +#include + +int main(int argc, char *argv[]) { + argparse::ArgumentParser program("test"); + program.set_prefix_chars("-+/"); + + program.add_argument("+f"); + program.add_argument("--bar"); + program.add_argument("/foo"); + + try { + program.parse_args(argc, argv); + } catch (const std::runtime_error &err) { + std::cerr << err.what() << std::endl; + std::cerr << program; + std::exit(1); + } + + if (program.is_used("+f")) { + std::cout << "+f : " << program.get("+f") << "\n"; + } + + if (program.is_used("--bar")) { + std::cout << "--bar : " << program.get("--bar") << "\n"; + } + + if (program.is_used("/foo")) { + std::cout << "/foo : " << program.get("/foo") << "\n"; + } +} \ No newline at end of file diff --git a/samples/description_epilog_metavar.cpp b/samples/description_epilog_metavar.cpp new file mode 100644 index 0000000..ef7f11e --- /dev/null +++ b/samples/description_epilog_metavar.cpp @@ -0,0 +1,17 @@ +#include + +int main(int argc, char *argv[]) { + argparse::ArgumentParser program("main"); + program.add_argument("thing").help("Thing to use.").metavar("THING"); + program.add_argument("--member") + .help("The alias for the member to pass to.") + .metavar("ALIAS"); + program.add_argument("--verbose").default_value(false).implicit_value(true); + + program.add_description("Forward a thing to the next member."); + program.add_epilog("Possible things include betingalw, chiz, and res."); + + program.parse_args(argc, argv); + + std::cout << program << std::endl; +} \ No newline at end of file diff --git a/samples/gathering_remaining_arguments.cpp b/samples/gathering_remaining_arguments.cpp new file mode 100644 index 0000000..e3fb84e --- /dev/null +++ b/samples/gathering_remaining_arguments.cpp @@ -0,0 +1,24 @@ +#include + +int main(int argc, char *argv[]) { + argparse::ArgumentParser program("compiler"); + + program.add_argument("files").remaining(); + + try { + program.parse_args(argc, argv); + } catch (const std::runtime_error &err) { + std::cerr << err.what() << std::endl; + std::cerr << program; + std::exit(1); + } + + try { + auto files = program.get>("files"); + std::cout << files.size() << " files provided" << std::endl; + for (auto &file : files) + std::cout << file << std::endl; + } catch (std::logic_error &e) { + std::cout << "No files provided" << std::endl; + } +} \ No newline at end of file diff --git a/samples/list_of_arguments.cpp b/samples/list_of_arguments.cpp new file mode 100644 index 0000000..ef4d421 --- /dev/null +++ b/samples/list_of_arguments.cpp @@ -0,0 +1,30 @@ +#include + +int main(int argc, char *argv[]) { + + argparse::ArgumentParser program("main"); + + program.add_argument("--input_files") + .help("The list of input files") + .nargs(2); + + try { + program.parse_args( + argc, argv); // Example: ./main --input_files config.yml System.xml + } catch (const std::runtime_error &err) { + std::cerr << err.what() << std::endl; + std::cerr << program; + std::exit(1); + } + + auto files = program.get>( + "--input_files"); // {"config.yml", "System.xml"} + + if (!files.empty()) { + std::cout << "Files: "; + for (auto &file : files) { + std::cout << file << " "; + } + std::cout << std::endl; + } +} \ No newline at end of file diff --git a/samples/parse_known_args.cpp b/samples/parse_known_args.cpp new file mode 100644 index 0000000..17b5f4f --- /dev/null +++ b/samples/parse_known_args.cpp @@ -0,0 +1,26 @@ +#include +#include + +int main(int argc, char *argv[]) { + argparse::ArgumentParser program("test"); + program.add_argument("--foo").implicit_value(true).default_value(false); + program.add_argument("bar"); + + auto unknown_args = program.parse_known_args(argc, argv); + + if (program.is_used("--foo")) { + std::cout << "--foo : " << program.get("--foo") << "\n"; + } + + if (program.is_used("bar")) { + std::cout << "bar : " << program.get("bar") << "\n"; + } + + if (!unknown_args.empty()) { + std::cout << "Unknown args : "; + for (const auto &u : unknown_args) { + std::cout << u << " "; + } + std::cout << std::endl; + } +} \ No newline at end of file