summaryrefslogtreecommitdiff
path: root/cli/source.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2009-10-04 13:58:20 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2009-10-04 13:58:20 +0200
commit6a9a911f05bbd0d2a63a06512733a4a6ff5b3e65 (patch)
tree6b5162001fa2c8d71d81542b55b9da00c27da4e8 /cli/source.cxx
parent8ed3cbc6f7a99713e6ea581c95e5a991ef829979 (diff)
Add --option-{prefix,separator} options
Diffstat (limited to 'cli/source.cxx')
-rw-r--r--cli/source.cxx106
1 files changed, 64 insertions, 42 deletions
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