char const *desc = cd.description ();
if (desc != NULL)
std::cout << desc;
+
+ options const &opts = cd.opts ();
+ if (!opts.empty ())
+ {
+ std::cout << "recognized options:" << std::endl;
+ for (options::const_iterator ot = opts.begin ();
+ ot != opts.end (); ++ot)
+ std::cout << " " << ot->second->format () << std::endl;
+ }
+
std::cout << std::endl;
}
}
, _m_seen (false)
{}
+std::string
+option_common::format () const
+{
+ std::string ret;
+ bool has_short = _m_opt.key < 127;
+ if (has_short)
+ {
+ char buf[3] = {};
+ std::sprintf (buf, "-%c", _m_opt.key);
+ std::string xxx (buf);
+ ret += buf;
+ }
+
+ if (_m_opt.name != NULL)
+ {
+ if (has_short)
+ ret += ", ";
+ ret += "--";
+ ret += _m_opt.name;
+ }
+
+ if (_m_opt.arg != NULL)
+ {
+ bool optional = !!(_m_opt.flags & OPTION_ARG_OPTIONAL);
+ if (optional)
+ ret += '[';
+ ret += '=';
+ ret += _m_opt.arg;
+ if (optional)
+ ret += ']';
+ }
+
+ if (_m_opt.doc != NULL)
+ {
+ ret += "\t";
+ ret += _m_opt.doc;
+ }
+
+ return ret;
+}
+
options global_opts;
option_i const *getopt (int key) const;
argp build_argp (bool toplev = false) const;
void add (option_i *opt);
- bool empty () const
- {
- return std::map<int, option_i *>::empty ();
- }
+ using std::map<int, option_i *>::empty;
+ using std::map<int, option_i *>::begin;
+ using std::map<int, option_i *>::end;
+ using std::map<int, option_i *>::const_iterator;
};
// Wrapper of argp parsing. While in general argp does a decent job,
virtual argp_option const &build_option () const = 0;
virtual error_t parse_opt (char *arg, argp_state *state) = 0;
virtual int key () const = 0;
+ virtual std::string format () const = 0;
virtual ~option_i () {}
};
{
return _m_opt.key;
}
+
+ std::string format () const;
};
template<class arg_type>