Skip to content

Commit

Permalink
Update Makefile for ex7, tidy examples.
Browse files Browse the repository at this point in the history
* Add ex7-run target to Makefile.
* Edit examples for consistency.
  • Loading branch information
halfflat committed May 1, 2024
1 parent be8b90e commit c8f4e94
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

top:=$(dir $(realpath $(lastword $(MAKEFILE_LIST))))

examples:=ex1-parse ex1-run ex2-parse ex2-run ex3-parse ex3-run ex4-run ex5-run ex6-run
examples:=ex1-parse ex1-run ex2-parse ex2-run ex3-parse ex3-run ex4-run ex5-run ex6-run ex7-run
all:: unit $(examples)

test-src:=unit.cc test_sink.cc test_maybe.cc test_option.cc test_state.cc test_parse.cc test_parsers.cc test_saved_options.cc test_run.cc test_version.cc
Expand Down
8 changes: 5 additions & 3 deletions ex/ex1-parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
const char* usage_str =
"[OPTION]...\n"
"\n"
" -n, --number=N Specify N\n"
" -f, --function=FUNC Perform FUNC, which is one of: one, two\n"
" -h, --help Display usage information and exit\n";
" -n, --number=N specify number of times to perform function\n"
" -f, --function=FUNC specify function, which is one of: one, two;\n"
" this option is mandatory\n"
"\n"
" -h, --help display usage information and exit\n";

int main(int, char** argv) {
try {
Expand Down
10 changes: 6 additions & 4 deletions ex/ex1-run.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
const char* usage_str =
"[OPTION]...\n"
"\n"
" -n, --number=N Specify N\n"
" -f, --function=FUNC Perform FUNC, which is one of: one, two\n"
" -h, --help Display usage information and exit\n";
" -n, --number=N specify number of times to perform function\n"
" -f, --function=FUNC specify function, which is one of: one, two;\n"
" this option is mandatory\n"
"\n"
" -h, --help display usage information and exit\n";

int main(int argc, char** argv) {
try {
Expand All @@ -27,7 +29,7 @@ int main(int argc, char** argv) {

if (!to::run(opts, argc, argv+1)) return 0;

if (argv[1]) throw to::option_error("unrecogonized argument", argv[1]);
if (argv[1]) throw to::option_error("unrecognized argument", argv[1]);
if (n<1) throw to::option_error("N must be at least 1");

// Do things with arguments:
Expand Down
4 changes: 2 additions & 2 deletions ex/ex2-parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
const char* usage_str =
"[OPTION]...\n"
"\n"
" --sum=N1,..,Nk Sum the integers N1 through Nk.\n"
" -h, --help Display usage information and exit\n";
" --sum=N1,..,Nk sum the integers N1 through Nk\n"
" -h, --help display usage information and exit\n";

int main(int, char** argv) {
try {
Expand Down
4 changes: 2 additions & 2 deletions ex/ex2-run.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
const char* usage_str =
"[OPTION]...\n"
"\n"
" --sum=N1,..,Nk Sum the integers N1 through Nk.\n"
" -h, --help Display usage information and exit\n";
" --sum=N1,..,Nk sum the integers N1 through Nk\n"
" -h, --help display usage information and exit\n";

int main(int argc, char** argv) {
try {
Expand Down
9 changes: 5 additions & 4 deletions ex/ex3-parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
const char* usage_str =
"[OPTION]... [ARGUMENT]...\n"
"\n"
" -a, --apple Print 'apple' but otherwise ignore.\n"
" -- Stop further argument processing.\n"
" -h, --help Display usage information and exit.\n"
" -a, --apple print 'apple'\n"
"\n"
"Throw away --apple options and report remaining arguments.\n";
" -- stop further argument processing\n"
" -h, --help display usage information and exit\n"
"\n"
"Disregarding --apple options, report remaining arguments.\n";

int main(int, char** argv) {
try {
Expand Down
9 changes: 5 additions & 4 deletions ex/ex3-run.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
const char* usage_str =
"[OPTION]... [ARGUMENT]...\n"
"\n"
" -a, --apple Print 'apple' but otherwise ignore.\n"
" -- Stop further argument processing.\n"
" -h, --help Display usage information and exit.\n"
" -a, --apple print 'apple'\n"
"\n"
"Throw away --apple options and report remaining arguments.\n";
" -- stop further argument processing\n"
" -h, --help display usage information and exit\n"
"\n"
"Disregarding --apple options, report remaining arguments.\n";

int main(int argc, char** argv) {
try {
Expand Down
9 changes: 6 additions & 3 deletions ex/ex6-run.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
#include <tinyopt/tinyopt.h>

const char* usage_str =
"[-n [ fish | cake ] | -n INT | -n] ...\n"
"[OPTIONS]...\n"
"\n"
"Parse and display -n options with either a keyword argument,\n"
"an integer argument, or without arguments.\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";
Expand Down Expand Up @@ -37,6 +39,7 @@ int main(int argc, char** argv) {
};

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());
Expand Down
28 changes: 8 additions & 20 deletions ex/ex7-run.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,34 @@
#include <tinyopt/tinyopt.h>

const char* usage_str =
"[-n [ INT [ INT [ INT ] ] ] | -a | -b ] ...\n"
"[OPTION] ...\n"
"\n"
"Collect vectors of up to 3 integers as multiple arguments to the -n option.\n"
"Count occurances of flags -a and -b.\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; };

int a = 0, b = 0;

#if 0
to::option opts[] = {
{ to::action(help), "-h", "--help", to::exit },
{ to::increment(a), to::then(0), "-a", to::flag },
{ to::increment(b), to::then(0), "-b", to::flag },
{ to::action(new_ns), to::then(1), "-n", to::flag },
{ to::action(push_ns), to::when(1) }
};
#endif
auto gt0 = [](int m) { return m>0; };
auto decrement = [](int m) { return m-1; };

to::option opts[] = {
{ to::action(help), "-h", "--help", to::exit },
{ to::increment(a), to::then(0), "-a", to::flag },
{ to::increment(b), to::then(0), "-b", to::flag },
{ 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("unrecogonized argument", argv[1]);
if (argv[1]) throw to::option_error("unrecognized argument", argv[1]);

std::cout << "a count: " << a << "\nb count: " << b << "\n";
std::cout << "ns:\n";
for (auto& ns: nss) {
std::cout << "{ ";
std::ostream_iterator<int> os(std::cout, " ");
Expand Down

0 comments on commit c8f4e94

Please sign in to comment.