-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for optional arguments with to::lax (#19)
* Separate argv consumption from option matching in `state` and `counted_option` classes. * Implement `to::lax`. * Update unit tests against new internal API. * Add example 6 that demonstrates `to::lax`. * Add unit tests for `to::lax`. * Update documentation and add examples to explain `to::lax` and the use of modal options to implement options with a count of arguments. * Add `ex7-run` target to Makefile. * Edit examples for consistency. * Exclude CI with clang++ and c++20 on ubuntu-22.04 image in order to avoid triggering actions/runner-images#8659
- Loading branch information
Showing
15 changed files
with
436 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <iostream> | ||
#include <string> | ||
|
||
#include <tinyopt/tinyopt.h> | ||
|
||
const char* usage_str = | ||
"[OPTIONS]...\n" | ||
"\n" | ||
" -n fish | cake print a message indicating a keyword argument\n" | ||
" -n INT print a message indicating aninteger argument\n" | ||
" -n print a message indicating no argument\n" | ||
" -h, --help display usage information and exit\n"; | ||
|
||
void print_kw(const char* kw) { | ||
std::cout << "keyword argument: " << kw << "\n"; | ||
} | ||
|
||
void print_int(int n) { | ||
std::cout << "integer argument: " << n << "\n"; | ||
} | ||
|
||
void print_flag() { | ||
std::cout << "no argument\n"; | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
try { | ||
std::pair<const char*, const char*> kw_tbl[] = { | ||
{ "fish", "FISH" }, { "cake", "CAKE" } | ||
}; | ||
|
||
auto help = [argv0 = argv[0]] { to::usage(argv0, usage_str); }; | ||
|
||
to::option opts[] = { | ||
{ to::action(help), "-h", "--help" }, | ||
{ to::action(print_kw, to::keywords(kw_tbl)), "-n", to::lax }, | ||
{ to::action(print_int, to::default_parser<int>{}), "-n", to::lax }, | ||
{ to::action(print_flag), "-n", to::flag }, | ||
}; | ||
|
||
to::run(opts, argc, argv+1); | ||
if (argv[1]) throw to::option_error("unrecognized argument", argv[1]); | ||
} | ||
catch (to::option_error& e) { | ||
to::usage_error(argv[0], usage_str, e.what()); | ||
return 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <iostream> | ||
#include <iterator> | ||
#include <vector> | ||
|
||
#include <tinyopt/tinyopt.h> | ||
|
||
const char* usage_str = | ||
"[OPTION] ...\n" | ||
"\n" | ||
" -n [ INT [ INT [ INT ] ] ] collect a vector of up to 3 integers\n" | ||
" -h, --help display usage information and exit\n" | ||
"\n" | ||
"Collect and display vectors of up to 3 integers as multiple arguments to the -n option.\n"; | ||
|
||
int main(int argc, char** argv) { | ||
try { | ||
auto help = [argv0 = argv[0]] { to::usage(argv0, usage_str); }; | ||
|
||
std::vector<std::vector<int>> nss; | ||
|
||
auto new_ns = [&]() { nss.push_back({}); return true; }; | ||
auto push_ns = [&](int n) { nss.back().push_back(n); return true; }; | ||
|
||
auto gt0 = [](int m) { return m>0; }; | ||
auto decrement = [](int m) { return m-1; }; | ||
|
||
to::option opts[] = { | ||
{ to::action(help), "-h", "--help", to::flag, to::exit }, | ||
{ to::action(new_ns), to::then(3), "-n", to::flag }, | ||
{ to::action(push_ns), to::when(gt0), to::then(decrement)} | ||
}; | ||
|
||
if (!to::run(opts, argc, argv+1)) return 0; | ||
if (argv[1]) throw to::option_error("unrecognized argument", argv[1]); | ||
|
||
for (auto& ns: nss) { | ||
std::cout << "{ "; | ||
std::ostream_iterator<int> os(std::cout, " "); | ||
for (int n: ns) *os = n; | ||
std::cout << "}\n"; | ||
} | ||
} | ||
catch (to::option_error& e) { | ||
to::usage_error(argv[0], usage_str, e.what()); | ||
return 1; | ||
} | ||
} |
Oops, something went wrong.