From 6a9a911f05bbd0d2a63a06512733a4a6ff5b3e65 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sun, 4 Oct 2009 13:58:20 +0200 Subject: Add --option-{prefix,separator} options --- cli/source.cxx | 106 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 64 insertions(+), 42 deletions(-) (limited to 'cli/source.cxx') diff --git a/cli/source.cxx b/cli/source.cxx index afac17e..991968c 100644 --- a/cli/source.cxx +++ b/cli/source.cxx @@ -193,60 +193,82 @@ namespace << "} " << map << "_init_;" << endl; + bool pfx (!opt_prefix.empty ()); + bool sep (!opt_sep.empty ()); + os << "int " << name << "::" << endl << "_parse (int start," << endl << "int argc," << endl << "char** argv," << endl << um << " opt_mode," << endl << um << " arg_mode)" + << "{"; + + if (sep) + os << "bool opt (true);" // Still recognizing options. + << endl; + + os << "for (; start < argc;)" << "{" - << "bool opt (true);" // Still recognizing options. - << endl - << "for (; start < argc;)" - << "{" - << "const char* s (argv[start]);" - << endl - << "if (std::strcmp (s, \"--\") == 0)" - << "{" - << "start++;" - << "opt = false;" - << "continue;" - << "}" - << map << "::const_iterator i (" << endl - << "opt ? " << map << "_.find (s) : " << map << "_.end ());" - << endl + << "const char* s (argv[start]);"; + + if (sep) + os << endl + << "if (std::strcmp (s, \"" << opt_sep << "\") == 0)" + << "{" + << "start++;" + << "opt = false;" + << "continue;" + << "}" + << map << "::const_iterator i (" << endl + << "opt ? " << map << "_.find (s) : " << map << "_.end ());"; + else + os << map << "::const_iterator i (" << map << "_.find (s));"; + + os << endl << "if (i != " << map << "_.end ())" << "{" << "start += (*(i->second)) (*this, argv + start, argc - start);" - << "}" - << "else if (opt && s[0] == '-' && s[1] != '\\0')" - << "{" + << "}"; - // Unknown option. - // - << "switch (opt_mode)" - << "{" - << "case ::cli::unknown_mode::skip:" << endl - << "{" - << "start++;" - << "continue;" - << "}" - << "case ::cli::unknown_mode::stop:" << endl - << "{" - << "break;" - << "}" - << "case ::cli::unknown_mode::fail:" << endl - << "{" - << "throw ::cli::unknown_option (s);" - << "}" - << "}" // switch - << "break;" // The stop case. - << "}" - << "else" - << "{" + // Unknown option. + // + if (pfx) + { + size_t n (opt_prefix.size ()); + + os << "else if ("; + + if (sep) + os << "opt && "; + + os << "std::strncmp (s, \"" << opt_prefix << "\", " << + n << ") == 0 && s[" << n << "] != '\\0')" + << "{" + << "switch (opt_mode)" + << "{" + << "case ::cli::unknown_mode::skip:" << endl + << "{" + << "start++;" + << "continue;" + << "}" + << "case ::cli::unknown_mode::stop:" << endl + << "{" + << "break;" + << "}" + << "case ::cli::unknown_mode::fail:" << endl + << "{" + << "throw ::cli::unknown_option (s);" + << "}" + << "}" // switch + << "break;" // The stop case. + << "}"; + } - // Unknown argument. - // + // Unknown argument. + // + os << "else" + << "{" << "switch (arg_mode)" << "{" << "case ::cli::unknown_mode::skip:" << endl -- cgit v1.1