diff --git a/Makefile b/Makefile index 442d6c7..4b3c777 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/ex/ex1-parse.cc b/ex/ex1-parse.cc index 9ed713c..c244492 100644 --- a/ex/ex1-parse.cc +++ b/ex/ex1-parse.cc @@ -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 { diff --git a/ex/ex1-run.cc b/ex/ex1-run.cc index 1a794d9..af0db92 100644 --- a/ex/ex1-run.cc +++ b/ex/ex1-run.cc @@ -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 { @@ -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: diff --git a/ex/ex2-parse.cc b/ex/ex2-parse.cc index 12bc98a..86b6597 100644 --- a/ex/ex2-parse.cc +++ b/ex/ex2-parse.cc @@ -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 { diff --git a/ex/ex2-run.cc b/ex/ex2-run.cc index 7c23375..6931944 100644 --- a/ex/ex2-run.cc +++ b/ex/ex2-run.cc @@ -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 { diff --git a/ex/ex3-parse.cc b/ex/ex3-parse.cc index 9c3f195..e680b91 100644 --- a/ex/ex3-parse.cc +++ b/ex/ex3-parse.cc @@ -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 { diff --git a/ex/ex3-run.cc b/ex/ex3-run.cc index f05b593..7de0d06 100644 --- a/ex/ex3-run.cc +++ b/ex/ex3-run.cc @@ -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 { diff --git a/ex/ex6-run.cc b/ex/ex6-run.cc index 1fbcc26..72d0473 100644 --- a/ex/ex6-run.cc +++ b/ex/ex6-run.cc @@ -4,10 +4,12 @@ #include 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"; @@ -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()); diff --git a/ex/ex7-run.cc b/ex/ex7-run.cc index 2752122..43a38f1 100644 --- a/ex/ex7-run.cc +++ b/ex/ex7-run.cc @@ -5,46 +5,34 @@ #include 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> 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 os(std::cout, " ");