1 #ifndef EUDAQ_INCLUDED_OptionParser
2 #define EUDAQ_INCLUDED_OptionParser
11 #include "Platform.hh"
29 OptionParser(
const std::string &name,
const std::string &version,
30 const std::string &desc =
"",
int minargs = -1,
32 : m_name(name), m_ver(version), m_desc(desc), m_minargs(minargs),
36 return Parse(const_cast<const char **>(args));
38 size_t NumArgs()
const {
return m_args.size(); }
39 std::string GetArg(
size_t i)
const {
return m_args[i]; }
40 void ShowHelp(std::ostream &);
43 void ExtraHelpText(
const std::string &);
44 int HandleMainException(std::ostream &err = std::cerr,
45 std::ostream &out = std::cout);
48 void LogException(
const std::string &msg, std::ostream & = std::cerr)
const;
49 std::string m_name, m_ver, m_desc, m_cmd, m_extra;
50 std::vector<OptionBase *> m_options;
51 std::map<std::string, size_t> m_names;
52 std::vector<std::string> m_args;
53 size_t m_minargs, m_maxargs;
59 const std::string &longname,
const std::string &deflt =
"",
60 const std::string &argname =
"",
const std::string &desc =
"")
61 : m_argname(argname), m_deflt(deflt), m_desc(desc),
62 m_hasarg(argname !=
""), m_isset(
false) {
64 m_names.push_back(
"-" + shortname);
66 m_names.push_back(
"--" + longname);
70 virtual void Print(std::ostream &)
const;
71 bool IsSet()
const {
return m_isset; }
75 void ParseOption(
const std::string &name,
const std::string &arg) {
79 virtual void DoParsing(
const std::string &name,
const std::string &arg) = 0;
81 std::vector<std::string> m_names;
82 std::string m_argname, m_deflt, m_desc;
83 bool m_hasarg, m_isset;
89 const std::string &longname,
const std::string &desc =
"")
90 :
OptionBase(p, shortname, longname,
"",
"", desc) {}
91 bool Value()
const {
return IsSet(); }
94 virtual void DoParsing(
const std::string & ,
95 const std::string & ) {}
101 const std::string &longname,
const T &deflt = T(),
102 const std::string &argname =
"",
const std::string &desc =
"")
105 const T &Value()
const {
return m_value; }
106 void SetValue(
const T &val) { m_value = val; }
109 virtual void DoParsing(
const std::string & ,
110 const std::string &arg) {
119 const std::string &longname,
const std::string &argname =
"",
120 const std::string &sep =
"",
const std::string &desc =
"")
121 :
OptionBase(p, shortname, longname,
"", argname, desc),
122 m_sep(sep.length() ? sep :
",") {}
123 const std::vector<T> &Value()
const {
return m_value; }
124 void SetValue(
const std::vector<T> &val = std::vector<T>()) {
127 void Resize(
size_t size) { m_value.resize(size); }
128 size_t NumItems()
const {
return m_value.size(); }
129 const T &Item(
size_t i)
const {
return m_value[i]; }
132 virtual void DoParsing(
const std::string & ,
133 const std::string &arg) {
137 i = arg.find(m_sep, start);
140 std::string valstr(arg, start,
141 (i == std::string::npos ? arg.length() : i) - start);
143 if (i != std::string::npos) {
146 }
while (i != std::string::npos);
149 std::vector<T> m_value;
152 std::vector<unsigned> DLLEXPORT parsenumbers(
const std::string &s);
155 #endif // EUDAQ_INCLUDED_OptionParser
Definition: OptionParser.hh:86
Definition: OptionParser.hh:15
T DLLEXPORT from_string(const std::string &x, const T &def=0)
Definition: Utils.hh:115
std::string to_string(const T &x, int digits=0)
Definition: Utils.hh:54
Definition: OptionParser.hh:56
Definition: OptionParser.hh:20
Definition: OptionParser.hh:27
Definition: OptionParser.hh:98