- template <typename T>
- bool nol_find_prefix(const T& nol, const char* descr, std::string& name)
- {
- bool result = nol.exists(name);
- if (!result) {
- std::stack<std::string> candidates;
- for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
- const std::string& fullname = nol.get_name(it);
- if (fullname.compare(0, name.length(), name) == 0)
- candidates.push(fullname);
- }
- switch (candidates.size()) {
- case 0:
- ERROR2("unknownw %s -- %s", descr, name.c_str());
- break;
- case 1:
- name = candidates.top();
+ // local helper class
+ class opt_helper {
+ public:
+ template <typename T>
+ static bool parse_arg(char opt, const char *arg, T& val);
+ static const char* on_off(bool b);
+ const char* descr(const char* str);
+ template <typename T>
+ const char* val_or_string(const T& val, const char* str,
+ const T& deflt = 0);
+ template <typename T>
+ static bool nol_find_prefix(const T& nol, const char* descr,
+ std::string& name);
+
+ private:
+ std::string descr_str;
+ std::string val_or_string_str;
+ };
+
+} // namespace
+
+template <typename T>
+bool opt_helper::parse_arg(char opt, const char *arg, T& val)
+{
+ std::istringstream str(arg);
+ bool result = (str >> val) && str.eof();
+ if (!result)
+ XBT_ERROR("invalid argument for option '-%c' -- \"%s\"", opt, arg);
+ return result;
+}
+
+const char* opt_helper::on_off(bool b)
+{
+ return b ? "on" : "off";
+}
+
+const char* opt_helper::descr(const char* str)
+{
+ const int descr_width = 40;
+ std::string& res = descr_str;
+ res = str;
+ res.resize(descr_width, '.');
+ return res.c_str();
+}
+
+template <typename T>
+const char* opt_helper::val_or_string(const T& val, const char* str,
+ const T& deflt)
+{
+ std::string& res = val_or_string_str;
+ if (val != deflt) {
+ std::ostringstream oss;
+ oss << val;
+ res = oss.str();
+ } else {
+ res = str;
+ }
+ return res.c_str();
+}
+
+template <typename T>
+bool opt_helper::nol_find_prefix(const T& nol, const char* descr,
+ std::string& name)
+{
+ bool result = nol.exists(name);
+ if (!result) {
+ std::stack<std::string> candidates;
+ for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
+ const std::string& fullname = nol.get_name(it);
+ if (fullname.compare(0, name.length(), name) == 0)
+ candidates.push(fullname);
+ }
+ switch (candidates.size()) {
+ case 0:
+ XBT_ERROR("unknownw %s -- %s", descr, name.c_str());
+ break;
+ case 1:
+ name = candidates.top();
+ candidates.pop();
+ result = true;
+ XBT_DEBUG("infered %s -- %s", descr, name.c_str());
+ break;
+ default:
+ XBT_ERROR("ambiguous %s -- %s", descr, name.c_str());
+ while (!candidates.empty()) {
+ XBT_ERROR(" candidates are -- %s", candidates.top().c_str());