From fc98bf23c16baf836d2c841792d4e0b35dd82727 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sat, 24 Mar 2018 13:27:35 +0200 Subject: Return bool from parse() indicating whether anything has been parsed --- cli/header.cxx | 15 +++++++++++---- cli/options.cxx | 13 ++++++++++--- cli/options.hxx | 2 +- cli/source.cxx | 41 +++++++++++++++++++++++++++++------------ 4 files changed, 51 insertions(+), 20 deletions(-) diff --git a/cli/header.cxx b/cli/header.cxx index de20334..64b31d2 100644 --- a/cli/header.cxx +++ b/cli/header.cxx @@ -136,9 +136,16 @@ namespace // Are we generating parsing constructors or parse() functions? // - string n (options.generate_parse () - ? string ("void\n") + (name != "parse" ? "parse" : "parse_") - : name); + string n; + if (options.generate_parse ()) + { + os << "// Return true if anything has been parsed." << endl + << "//" << endl; + + n = string ("bool\n") + (name != "parse" ? "parse" : "parse_"); + } + else + n = name; os << n << " (int& argc," << endl << "char** argv," << endl @@ -247,7 +254,7 @@ namespace // if (!abst) os << "private:" << endl - << "void" << endl + << "bool" << endl << "_parse (" << cli << "::scanner&," << endl << um << " option," << endl << um << " argument);" diff --git a/cli/options.cxx b/cli/options.cxx index 0a9c0e4..149cc50 100644 --- a/cli/options.cxx +++ b/cli/options.cxx @@ -1866,11 +1866,12 @@ _parse (const char* o, ::cli::scanner& s) return false; } -void options:: +bool options:: _parse (::cli::scanner& s, ::cli::unknown_mode opt_mode, ::cli::unknown_mode arg_mode) { + bool r = false; bool opt = true; while (s.more ()) @@ -1879,12 +1880,14 @@ _parse (::cli::scanner& s, if (std::strcmp (o, "--") == 0) { - s.skip (); opt = false; + s.skip (); + r = true; continue; } - if (opt && _parse (o, s)); + if (opt && _parse (o, s)) + r = true; else if (opt && std::strncmp (o, "-", 1) == 0 && o[1] != '\0') { switch (opt_mode) @@ -1892,6 +1895,7 @@ _parse (::cli::scanner& s, case ::cli::unknown_mode::skip: { s.skip (); + r = true; continue; } case ::cli::unknown_mode::stop: @@ -1913,6 +1917,7 @@ _parse (::cli::scanner& s, case ::cli::unknown_mode::skip: { s.skip (); + r = true; continue; } case ::cli::unknown_mode::stop: @@ -1928,6 +1933,8 @@ _parse (::cli::scanner& s, break; } } + + return r; } // Begin epilogue. diff --git a/cli/options.hxx b/cli/options.hxx index a4242bc..8a93b43 100644 --- a/cli/options.hxx +++ b/cli/options.hxx @@ -1410,7 +1410,7 @@ class options _parse (const char*, ::cli::scanner&); private: - void + bool _parse (::cli::scanner&, ::cli::unknown_mode option, ::cli::unknown_mode argument); diff --git a/cli/source.cxx b/cli/source.cxx index 9c88014..37f0ef5 100644 --- a/cli/source.cxx +++ b/cli/source.cxx @@ -584,10 +584,16 @@ namespace if (!abst) { bool p (options.generate_parse ()); - string n ( - p - ? "void " + name + "::\n" + (name != "parse" ? "parse" : "parse_") - : name + "::\n" + name); + + string n, res, ret; + if (p) + { + n = "bool " + name + "::\n" + (name != "parse" ? "parse" : "parse_"); + res = "bool r = "; + ret = "return r;"; + } + else + n = name + "::\n" + name; os << n << " (int& argc," << endl << "char** argv," << endl @@ -602,7 +608,8 @@ namespace } os << "{" << cli << "::argv_scanner s (argc, argv, erase);" - << "_parse (s, opt, arg);" + << res << "_parse (s, opt, arg);" + << ret << "}"; os << n << " (int start," << endl @@ -619,7 +626,8 @@ namespace } os << "{" << cli << "::argv_scanner s (start, argc, argv, erase);" - << "_parse (s, opt, arg);" + << res << "_parse (s, opt, arg);" + << ret << "}"; os << n << " (int& argc," << endl @@ -636,8 +644,9 @@ namespace } os << "{" << cli << "::argv_scanner s (argc, argv, erase);" - << "_parse (s, opt, arg);" + << res << "_parse (s, opt, arg);" << "end = s.end ();" + << ret << "}"; os << n << " (int start," << endl @@ -655,8 +664,9 @@ namespace } os << "{" << cli << "::argv_scanner s (start, argc, argv, erase);" - << "_parse (s, opt, arg);" + << res << "_parse (s, opt, arg);" << "end = s.end ();" + << ret << "}"; os << n << " (" << cli << "::scanner& s," << endl @@ -669,7 +679,8 @@ namespace names (c, names_init); } os << "{" - << "_parse (s, opt, arg);" + << res << "_parse (s, opt, arg);" + << ret << "}"; } @@ -923,11 +934,12 @@ namespace bool pfx (!opt_prefix.empty ()); bool sep (!opt_sep.empty ()); - os << "void " << name << "::" << endl + os << "bool " << name << "::" << endl << "_parse (" << cli << "::scanner& s," << endl << um << (pfx ? " opt_mode" : "") << "," << endl << um << " arg_mode)" - << "{"; + << "{" + << "bool r = false;"; if (sep) os << "bool opt = true;" // Still recognizing options. @@ -946,12 +958,14 @@ namespace if (!options.keep_separator ()) { os << "s.skip ();" // We don't want to erase the separator. + << "r = true;" << "continue;"; } os << "}"; } - os << "if (" << (sep ? "opt && " : "") << "_parse (o, s));"; + os << "if (" << (sep ? "opt && " : "") << "_parse (o, s))" << endl + << "r = true;"; // Unknown option. // @@ -972,6 +986,7 @@ namespace << "case " << cli << "::unknown_mode::skip:" << endl << "{" << "s.skip ();" + << "r = true;" << "continue;" << "}" << "case " << cli << "::unknown_mode::stop:" << endl @@ -996,6 +1011,7 @@ namespace << "case " << cli << "::unknown_mode::skip:" << endl << "{" << "s.skip ();" + << "r = true;" << "continue;" << "}" << "case " << cli << "::unknown_mode::stop:" << endl @@ -1011,6 +1027,7 @@ namespace << "}" << "}" // for + << "return r;" << "}"; } } -- cgit v1.1