1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
/*
* Copyright (C) 2000,2003 Daniel Heck
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "ecl_argp.hh"
#include <iostream>
using namespace std;
using ecl::ArgParser;
ArgParser::ArgParser() {
}
ArgParser::~ArgParser(){
}
void ArgParser::on_error (ErrorType t, const std::string &arg) {
cerr << "Error: " << errormsg(t, arg) << endl;
}
void ArgParser::on_argument (const std::string &/*arg*/) {
}
void ArgParser::on_option (int /*id*/, const std::string &/*param*/)
{
}
void ArgParser::def (int id, const std::string &name,
char abbr, bool takesparam)
{
m_opts.push_back (Option(id, abbr, string("--") + name, takesparam));
}
void ArgParser::def (bool *boolvar, const std::string &name,
char abbr)
{
Option opt(-1, abbr, string("--") + name, false);
opt.boolvar = boolvar;
m_opts.push_back(opt);
}
string ArgParser::errormsg(ErrorType t, const std::string &arg) const {
string errmsg;
switch (t) {
case InvalidParam: errmsg = "Invalid parameter for option `"; break;
case AmbiguousOpt: errmsg = "Ambiguous command line option `"; break;
case UnknownOpt: errmsg = "Unknown command line option `"; break;
case MissingParam: errmsg = "Missing parameter for option `"; break;
default:
return string();
}
errmsg += arg;
errmsg += "'.";
return errmsg;
}
void ArgParser::process_option(Option &opt, const std::string ¶m) {
if (opt.boolvar)
*opt.boolvar = true;
else
on_option(opt.id, param);
}
void ArgParser::getlongopt (const std::string &arg) {
string::const_iterator eqpos = find(arg.begin(), arg.end(), '=');
string optname(arg.begin(), eqpos);
option_iterator i, j;
j = i = find_prefix (m_opts.begin(), m_opts.end(), optname);
if (i == m_opts.end()) {
on_error (UnknownOpt, optname);
}
else if (find_prefix(++j, m_opts.end(), optname) != m_opts.end()) {
on_error (AmbiguousOpt, optname);
}
else if (i->takesparam) {
if (eqpos != arg.end()) {
process_option (*i, string(eqpos+1, arg.end()));
}
else if (!m_arglist.empty()) {
process_option (*i, m_arglist.front());
m_arglist.pop_front();
}
else
on_error (MissingParam, optname);
}
else if (eqpos != arg.end()) {
on_error (InvalidParam, optname);
}
else
process_option(*i, "");
}
void ArgParser::getshortopt (const std::string &arg) {
option_iterator i = m_opts.begin();
for (; i != m_opts.end(); ++i)
if (i->shortopt == arg[1])
break;
string option = arg.substr(0, 2);
if (i == m_opts.end()) {
on_error (UnknownOpt, option);
}
else if (i->takesparam) {
string param = arg.substr(2);
if (param.empty()) {
if (!m_arglist.empty()) {
param = m_arglist.front();
m_arglist.pop_front();
process_option (*i, param);
}
else
on_error (MissingParam, option);
} else
process_option (*i, param);
}
else {
process_option (*i, "");
// The following characters can be options in their own right
// (single-character options can be grouped, as in -hvx), so
// put them back into the argument list.
std::string newarg("-");
newarg.append(arg.begin()+2, arg.end());
if (newarg != "-")
m_arglist.push_front(newarg);
}
}
void ArgParser::parse () {
bool no_more_opts = false;
while (!m_arglist.empty()) {
std::string arg = m_arglist.front();
m_arglist.pop_front();
if (!no_more_opts && arg.size() >= 2 && arg[0] == '-') {
// Note: "-" is treated as an ordinary argument
if (arg[1] == '-') {
if (arg.size() == 2) // "--" terminates the option list
no_more_opts = true;
else
getlongopt (arg);
}
else
getshortopt (arg);
}
else
on_argument (arg);
}
}
|