mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-04 07:04:39 +00:00
Added additional samples and fixed parse_known_args API
This commit is contained in:
parent
6f1e89885e
commit
b1c7483cf5
@ -1200,8 +1200,8 @@ public:
|
|||||||
* @throws std::runtime_error in case of any invalid argument
|
* @throws std::runtime_error in case of any invalid argument
|
||||||
*/
|
*/
|
||||||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
|
||||||
void parse_known_args(int argc, const char *const argv[]) {
|
auto parse_known_args(int argc, const char *const argv[]) {
|
||||||
parse_known_args({argv, argv + argc});
|
return parse_known_args({argv, argv + argc});
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Getter for options with default values.
|
/* Getter for options with default values.
|
||||||
|
@ -36,4 +36,11 @@ add_sample(is_used)
|
|||||||
add_sample(joining_repeated_optional_arguments)
|
add_sample(joining_repeated_optional_arguments)
|
||||||
add_sample(repeating_argument_to_increase_value)
|
add_sample(repeating_argument_to_increase_value)
|
||||||
add_sample(negative_numbers)
|
add_sample(negative_numbers)
|
||||||
|
add_sample(description_epilog_metavar)
|
||||||
|
add_sample(list_of_arguments)
|
||||||
|
add_sample(compound_arguments)
|
||||||
|
add_sample(gathering_remaining_arguments)
|
||||||
add_sample(subcommands)
|
add_sample(subcommands)
|
||||||
|
add_sample(parse_known_args)
|
||||||
|
add_sample(custom_prefix_characters)
|
||||||
|
add_sample(custom_assignment_characters)
|
36
samples/compound_arguments.cpp
Normal file
36
samples/compound_arguments.cpp
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#include <argparse/argparse.hpp>
|
||||||
|
|
||||||
|
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<float>{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<bool>("-a"); // true
|
||||||
|
auto b = program.get<bool>("-b"); // true
|
||||||
|
auto c = program.get<std::vector<float>>("-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;
|
||||||
|
}
|
||||||
|
}
|
27
samples/custom_assignment_characters.cpp
Normal file
27
samples/custom_assignment_characters.cpp
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#include <argparse/argparse.hpp>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
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";
|
||||||
|
}
|
||||||
|
}
|
31
samples/custom_prefix_characters.cpp
Normal file
31
samples/custom_prefix_characters.cpp
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#include <argparse/argparse.hpp>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
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";
|
||||||
|
}
|
||||||
|
}
|
17
samples/description_epilog_metavar.cpp
Normal file
17
samples/description_epilog_metavar.cpp
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#include <argparse/argparse.hpp>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
24
samples/gathering_remaining_arguments.cpp
Normal file
24
samples/gathering_remaining_arguments.cpp
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#include <argparse/argparse.hpp>
|
||||||
|
|
||||||
|
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<std::vector<std::string>>("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;
|
||||||
|
}
|
||||||
|
}
|
30
samples/list_of_arguments.cpp
Normal file
30
samples/list_of_arguments.cpp
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#include <argparse/argparse.hpp>
|
||||||
|
|
||||||
|
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<std::vector<std::string>>(
|
||||||
|
"--input_files"); // {"config.yml", "System.xml"}
|
||||||
|
|
||||||
|
if (!files.empty()) {
|
||||||
|
std::cout << "Files: ";
|
||||||
|
for (auto &file : files) {
|
||||||
|
std::cout << file << " ";
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
}
|
26
samples/parse_known_args.cpp
Normal file
26
samples/parse_known_args.cpp
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#include <argparse/argparse.hpp>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
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<bool>("--foo") << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (program.is_used("bar")) {
|
||||||
|
std::cout << "bar : " << program.get<std::string>("bar") << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!unknown_args.empty()) {
|
||||||
|
std::cout << "Unknown args : ";
|
||||||
|
for (const auto &u : unknown_args) {
|
||||||
|
std::cout << u << " ";
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user