mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-03 22:54:39 +00:00
Added nargs to help output, added test samples
This commit is contained in:
parent
05232b7487
commit
6f1e89885e
@ -668,16 +668,16 @@ public:
|
||||
}
|
||||
stream << name_stream.str() << "\t" << argument.m_help;
|
||||
|
||||
// print nargs spec
|
||||
if (!argument.m_help.empty()) {
|
||||
stream << " ";
|
||||
}
|
||||
stream << argument.m_num_args_range;
|
||||
|
||||
if (argument.m_default_value.has_value() &&
|
||||
argument.m_num_args_range != NArgsRange{0, 0}) {
|
||||
if (!argument.m_help.empty()) {
|
||||
stream << " ";
|
||||
}
|
||||
stream << "[default: " << argument.m_default_value_repr << "]";
|
||||
} else if (argument.m_is_required) {
|
||||
if (!argument.m_help.empty()) {
|
||||
stream << " ";
|
||||
}
|
||||
stream << "[required]";
|
||||
}
|
||||
stream << "\n";
|
||||
@ -730,6 +730,23 @@ private:
|
||||
|
||||
std::size_t get_max() const { return m_max; }
|
||||
|
||||
// Print help message
|
||||
friend auto operator<<(std::ostream &stream, const NArgsRange &range)
|
||||
-> std::ostream & {
|
||||
if (range.m_min == range.m_max) {
|
||||
if (range.m_min != 0 && range.m_min != 1) {
|
||||
stream << "[nargs: " << range.m_min << "] ";
|
||||
}
|
||||
} else {
|
||||
if (range.m_max == std::numeric_limits<std::size_t>::max()) {
|
||||
stream << "[nargs: " << range.m_min << " or more] ";
|
||||
} else {
|
||||
stream << "[nargs=" << range.m_min << ".." << range.m_max << "] ";
|
||||
}
|
||||
}
|
||||
return stream;
|
||||
}
|
||||
|
||||
bool operator==(const NArgsRange &rhs) const {
|
||||
return rhs.m_min == m_min && rhs.m_max == m_max;
|
||||
}
|
||||
|
@ -29,5 +29,11 @@ function(add_sample NAME)
|
||||
set_target_properties(ARGPARSE_SAMPLE_${NAME} PROPERTIES OUTPUT_NAME ${NAME})
|
||||
endfunction()
|
||||
|
||||
add_sample(git_subcommands)
|
||||
add_sample(square_a_number)
|
||||
add_sample(positional_argument)
|
||||
add_sample(optional_flag_argument)
|
||||
add_sample(required_optional_argument)
|
||||
add_sample(is_used)
|
||||
add_sample(joining_repeated_optional_arguments)
|
||||
add_sample(repeating_argument_to_increase_value)
|
||||
add_sample(negative_numbers)
|
||||
add_sample(subcommands)
|
26
samples/is_used.cpp
Normal file
26
samples/is_used.cpp
Normal file
@ -0,0 +1,26 @@
|
||||
#include <argparse/argparse.hpp>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
argparse::ArgumentParser program("test");
|
||||
|
||||
program.add_argument("--color")
|
||||
.default_value(std::string{
|
||||
"orange"}) // might otherwise be type const char* leading to an error
|
||||
// when trying program.get<std::string>
|
||||
.help("specify the cat's fur color");
|
||||
|
||||
try {
|
||||
program.parse_args(argc, argv); // Example: ./main --color orange
|
||||
} catch (const std::runtime_error &err) {
|
||||
std::cerr << err.what() << std::endl;
|
||||
std::cerr << program;
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
auto color = program.get<std::string>("--color"); // "orange"
|
||||
auto explicit_color =
|
||||
program.is_used("--color"); // true, user provided orange
|
||||
std::cout << "Color: " << color << "\n";
|
||||
std::cout << "Argument was explicitly provided by user? " << std::boolalpha
|
||||
<< explicit_color << "\n";
|
||||
}
|
28
samples/joining_repeated_optional_arguments.cpp
Normal file
28
samples/joining_repeated_optional_arguments.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
#include <argparse/argparse.hpp>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
argparse::ArgumentParser program("test");
|
||||
|
||||
program.add_argument("--color")
|
||||
.default_value<std::vector<std::string>>({"orange"})
|
||||
.append()
|
||||
.help("specify the cat's fur color");
|
||||
|
||||
try {
|
||||
program.parse_args(
|
||||
argc, argv); // Example: ./main --color red --color green --color blue
|
||||
} catch (const std::runtime_error &err) {
|
||||
std::cerr << err.what() << std::endl;
|
||||
std::cerr << program;
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
auto colors = program.get<std::vector<std::string>>(
|
||||
"--color"); // {"red", "green", "blue"}
|
||||
|
||||
std::cout << "Colors: ";
|
||||
for (const auto &c : colors) {
|
||||
std::cout << c << " ";
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
32
samples/negative_numbers.cpp
Normal file
32
samples/negative_numbers.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
#include <argparse/argparse.hpp>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
argparse::ArgumentParser program("test");
|
||||
|
||||
program.add_argument("integer").help("Input number").scan<'i', int>();
|
||||
|
||||
program.add_argument("floats")
|
||||
.help("Vector of floats")
|
||||
.nargs(4)
|
||||
.scan<'g', float>();
|
||||
|
||||
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("integer")) {
|
||||
std::cout << "Integer : " << program.get<int>("integer") << "\n";
|
||||
}
|
||||
|
||||
if (program.is_used("floats")) {
|
||||
std::cout << "Floats : ";
|
||||
for (const auto &f : program.get<std::vector<float>>("floats")) {
|
||||
std::cout << f << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
22
samples/optional_flag_argument.cpp
Normal file
22
samples/optional_flag_argument.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
#include <argparse/argparse.hpp>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
argparse::ArgumentParser program("test");
|
||||
|
||||
program.add_argument("--verbose")
|
||||
.help("increase output verbosity")
|
||||
.default_value(false)
|
||||
.implicit_value(true);
|
||||
|
||||
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["--verbose"] == true) {
|
||||
std::cout << "Verbosity enabled" << std::endl;
|
||||
}
|
||||
}
|
17
samples/repeating_argument_to_increase_value.cpp
Normal file
17
samples/repeating_argument_to_increase_value.cpp
Normal file
@ -0,0 +1,17 @@
|
||||
#include <argparse/argparse.hpp>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
argparse::ArgumentParser program("test");
|
||||
|
||||
int verbosity = 0;
|
||||
program.add_argument("-V", "--verbose")
|
||||
.action([&](const auto &) { ++verbosity; })
|
||||
.append()
|
||||
.default_value(false)
|
||||
.implicit_value(true)
|
||||
.nargs(0);
|
||||
|
||||
program.parse_args(argc, argv); // Example: ./main -VVVV
|
||||
|
||||
std::cout << "verbose level: " << verbosity << std::endl; // verbose level: 4
|
||||
}
|
19
samples/required_optional_argument.cpp
Normal file
19
samples/required_optional_argument.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include <argparse/argparse.hpp>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
argparse::ArgumentParser program("test");
|
||||
|
||||
program.add_argument("-o", "--output")
|
||||
.required()
|
||||
.help("specify the output file.");
|
||||
|
||||
try {
|
||||
program.parse_args(argc, argv);
|
||||
} catch (const std::runtime_error &err) {
|
||||
std::cerr << err.what() << std::endl;
|
||||
std::cerr << program;
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
std::cout << "Output written to " << program.get("-o") << "\n";
|
||||
}
|
Loading…
Reference in New Issue
Block a user