2007-03-26 12:16:57 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* 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
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2007-03-26 12:16:57 +00:00
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "OptionParser.h"
|
2009-02-07 11:00:34 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <istream>
|
|
|
|
#include <utility>
|
|
|
|
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2007-03-26 12:16:57 +00:00
|
|
|
#include "OptionHandlerImpl.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Option.h"
|
2008-05-14 15:33:42 +00:00
|
|
|
#include "A2STR.h"
|
2008-09-22 09:26:57 +00:00
|
|
|
#include "a2functional.h"
|
2009-02-07 11:00:34 +00:00
|
|
|
#include "array_fun.h"
|
2009-05-08 07:58:50 +00:00
|
|
|
#include "OptionHandlerFactory.h"
|
2010-11-14 07:17:55 +00:00
|
|
|
#include "DlAbortEx.h"
|
2010-12-01 12:26:58 +00:00
|
|
|
#include "error_code.h"
|
2007-03-26 12:16:57 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
|
|
|
|
2010-01-09 09:35:18 +00:00
|
|
|
OptionParser::OptionParser():
|
2010-06-21 13:51:56 +00:00
|
|
|
idCounter_(0)
|
2010-01-09 09:35:18 +00:00
|
|
|
{}
|
2009-02-07 11:00:34 +00:00
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
OptionParser::~OptionParser() {}
|
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
2009-02-07 11:00:34 +00:00
|
|
|
template<typename InputIterator>
|
2010-10-30 16:02:15 +00:00
|
|
|
size_t countPublicOption(InputIterator first, InputIterator last)
|
2009-02-07 11:00:34 +00:00
|
|
|
{
|
|
|
|
size_t count = 0;
|
|
|
|
for(; first != last; ++first) {
|
|
|
|
if(!(*first)->isHidden()) {
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2009-02-07 11:00:34 +00:00
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
2009-02-07 11:00:34 +00:00
|
|
|
template<typename InputIterator>
|
2010-10-30 16:02:15 +00:00
|
|
|
void putOptions(struct option* longOpts, int* plopt,
|
|
|
|
InputIterator first, InputIterator last)
|
2009-02-07 11:00:34 +00:00
|
|
|
{
|
|
|
|
for(; first != last; ++first) {
|
|
|
|
if(!(*first)->isHidden()) {
|
2009-07-14 13:22:26 +00:00
|
|
|
#ifdef HAVE_OPTION_CONST_NAME
|
2009-02-07 11:00:34 +00:00
|
|
|
(*longOpts).name = (*first)->getName().c_str();
|
2009-07-14 13:22:26 +00:00
|
|
|
#else // !HAVE_OPTION_CONST_NAME
|
|
|
|
(*longOpts).name = strdup((*first)->getName().c_str());
|
|
|
|
#endif // !HAVE_OPTION_CONST_NAME
|
2009-02-07 11:00:34 +00:00
|
|
|
switch((*first)->getArgType()) {
|
|
|
|
case OptionHandler::REQ_ARG:
|
2010-01-05 16:01:46 +00:00
|
|
|
(*longOpts).has_arg = required_argument;
|
|
|
|
break;
|
2009-02-07 11:00:34 +00:00
|
|
|
case OptionHandler::OPT_ARG:
|
2010-01-05 16:01:46 +00:00
|
|
|
(*longOpts).has_arg = optional_argument;
|
|
|
|
break;
|
2009-02-07 11:00:34 +00:00
|
|
|
case OptionHandler::NO_ARG:
|
2010-01-05 16:01:46 +00:00
|
|
|
(*longOpts).has_arg = no_argument;
|
|
|
|
break;
|
2009-02-07 11:00:34 +00:00
|
|
|
default:
|
2010-01-05 16:01:46 +00:00
|
|
|
abort();
|
2009-02-07 11:00:34 +00:00
|
|
|
}
|
|
|
|
if((*first)->getShortName() == 0) {
|
2010-01-05 16:01:46 +00:00
|
|
|
(*longOpts).flag = plopt;
|
|
|
|
(*longOpts).val = (*first)->getOptionID();
|
2009-02-07 11:00:34 +00:00
|
|
|
} else {
|
2010-01-05 16:01:46 +00:00
|
|
|
(*longOpts).flag = 0;
|
|
|
|
(*longOpts).val = (*first)->getShortName();
|
2009-02-07 11:00:34 +00:00
|
|
|
}
|
|
|
|
++longOpts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(*longOpts).name = 0;
|
|
|
|
(*longOpts).has_arg = 0;
|
|
|
|
(*longOpts).flag = 0;
|
|
|
|
(*longOpts).val = 0;
|
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2009-02-07 11:00:34 +00:00
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
2009-02-07 11:00:34 +00:00
|
|
|
template<typename InputIterator>
|
2010-10-30 16:02:15 +00:00
|
|
|
std::string createOptstring(InputIterator first, InputIterator last)
|
2009-02-07 11:00:34 +00:00
|
|
|
{
|
|
|
|
std::string str = "";
|
|
|
|
for(; first != last; ++first) {
|
|
|
|
if(!(*first)->isHidden()) {
|
|
|
|
if((*first)->getShortName() != 0) {
|
2010-01-05 16:01:46 +00:00
|
|
|
str += (*first)->getShortName();
|
|
|
|
if((*first)->getArgType() == OptionHandler::REQ_ARG) {
|
|
|
|
str += ":";
|
|
|
|
} else if((*first)->getArgType() == OptionHandler::OPT_ARG) {
|
|
|
|
str += "::";
|
|
|
|
}
|
2009-02-07 11:00:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2009-02-07 11:00:34 +00:00
|
|
|
|
|
|
|
void OptionParser::parseArg
|
2010-02-28 12:30:11 +00:00
|
|
|
(std::ostream& out, std::vector<std::string>& nonopts,
|
2011-04-22 14:38:59 +00:00
|
|
|
int argc, char* argv[])
|
2009-02-07 11:00:34 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
size_t numPublicOption = countPublicOption(optionHandlers_.begin(),
|
|
|
|
optionHandlers_.end());
|
2009-02-07 11:00:34 +00:00
|
|
|
int lopt;
|
|
|
|
array_ptr<struct option> longOpts(new struct option[numPublicOption+1]);
|
2010-06-21 13:51:56 +00:00
|
|
|
putOptions(longOpts, &lopt,optionHandlers_.begin(),optionHandlers_.end());
|
|
|
|
std::string optstring = createOptstring(optionHandlers_.begin(),
|
|
|
|
optionHandlers_.end());
|
2009-02-07 11:00:34 +00:00
|
|
|
while(1) {
|
2009-02-12 13:12:54 +00:00
|
|
|
int c = getopt_long(argc, argv, optstring.c_str(), longOpts, 0);
|
2009-02-07 11:00:34 +00:00
|
|
|
if(c == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
SharedHandle<OptionHandler> op;
|
|
|
|
if(c == 0) {
|
|
|
|
op = findByID(lopt);
|
|
|
|
} else {
|
|
|
|
op = findByShortName(c);
|
|
|
|
}
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!op) {
|
2010-12-01 12:26:58 +00:00
|
|
|
throw DL_ABORT_EX2("Failed to parse command-line options.",
|
|
|
|
error_code::OPTION_ERROR);
|
2009-02-07 11:00:34 +00:00
|
|
|
}
|
|
|
|
out << op->getName() << "=";
|
|
|
|
if(optarg) {
|
|
|
|
out << optarg;
|
2011-04-22 14:38:59 +00:00
|
|
|
if(op->getEraseAfterParse()) {
|
|
|
|
for(char* p = optarg; *p != '\0'; ++p) {
|
|
|
|
*p = '*';
|
|
|
|
}
|
|
|
|
}
|
2009-02-07 11:00:34 +00:00
|
|
|
}
|
|
|
|
out << "\n";
|
|
|
|
}
|
|
|
|
std::copy(argv+optind, argv+argc, std::back_inserter(nonopts));
|
|
|
|
}
|
|
|
|
|
|
|
|
void OptionParser::parse(Option& option, std::istream& is)
|
2007-03-26 12:16:57 +00:00
|
|
|
{
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string line;
|
2007-03-26 12:16:57 +00:00
|
|
|
int32_t linenum = 0;
|
|
|
|
while(getline(is, line)) {
|
|
|
|
++linenum;
|
2009-10-22 15:09:00 +00:00
|
|
|
if(util::startsWith(line, A2STR::SHARP_C)) {
|
2007-03-26 12:16:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-10-10 03:39:00 +00:00
|
|
|
std::pair<std::string, std::string> nv;
|
|
|
|
util::divide(nv, line, '=');
|
2010-01-09 09:35:18 +00:00
|
|
|
if(nv.first.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
2007-03-26 12:16:57 +00:00
|
|
|
OptionHandlerHandle handler = getOptionHandlerByName(nv.first);
|
2007-11-21 16:14:40 +00:00
|
|
|
handler->parse(option, nv.second);
|
2007-03-26 12:16:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-31 13:33:05 +00:00
|
|
|
namespace {
|
2009-12-20 14:25:51 +00:00
|
|
|
class DummyOptionHandler:public NameMatchOptionHandler {
|
|
|
|
protected:
|
|
|
|
virtual void parseArg(Option& option, const std::string& arg) {}
|
|
|
|
public:
|
|
|
|
DummyOptionHandler(const std::string& name):NameMatchOptionHandler(name) {}
|
|
|
|
|
|
|
|
virtual std::string createPossibleValuesString() const
|
|
|
|
{
|
|
|
|
return A2STR::NIL;
|
|
|
|
}
|
|
|
|
};
|
2010-10-30 14:53:40 +00:00
|
|
|
} // namespace
|
2009-12-20 14:25:51 +00:00
|
|
|
|
|
|
|
OptionHandlerHandle OptionParser::getOptionHandlerByName
|
|
|
|
(const std::string& optName)
|
2007-03-26 12:16:57 +00:00
|
|
|
{
|
2009-12-20 14:25:51 +00:00
|
|
|
SharedHandle<OptionHandler> handler(new DummyOptionHandler(optName));
|
|
|
|
std::vector<SharedHandle<OptionHandler> >::const_iterator i =
|
2010-06-21 13:51:56 +00:00
|
|
|
std::lower_bound(optionHandlers_.begin(), optionHandlers_.end(),
|
2010-01-05 16:01:46 +00:00
|
|
|
handler, OptionHandlerNameLesser());
|
2010-06-21 13:51:56 +00:00
|
|
|
if(i != optionHandlers_.end() && (*i)->canHandle(optName)) {
|
2009-12-20 14:25:51 +00:00
|
|
|
handler = *i;
|
2010-01-09 09:35:18 +00:00
|
|
|
} else {
|
|
|
|
handler.reset(new NullOptionHandler());
|
2007-03-26 12:16:57 +00:00
|
|
|
}
|
2009-12-20 14:25:51 +00:00
|
|
|
return handler;
|
2007-03-26 12:16:57 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2009-12-20 14:25:51 +00:00
|
|
|
void OptionParser::setOptionHandlers
|
|
|
|
(const std::vector<SharedHandle<OptionHandler> >& optionHandlers)
|
2008-02-08 15:53:45 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
optionHandlers_ = optionHandlers;
|
2010-02-28 16:04:52 +00:00
|
|
|
for(std::vector<SharedHandle<OptionHandler> >::const_iterator i =
|
2010-06-21 13:51:56 +00:00
|
|
|
optionHandlers_.begin(), eoi = optionHandlers_.end();
|
2010-02-28 16:04:52 +00:00
|
|
|
i != eoi; ++i) {
|
2010-06-21 13:51:56 +00:00
|
|
|
(*i)->setOptionID(++idCounter_);
|
2009-02-07 11:00:34 +00:00
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
std::sort(optionHandlers_.begin(), optionHandlers_.end(),
|
2010-01-05 16:01:46 +00:00
|
|
|
OptionHandlerNameLesser());
|
2008-02-08 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
2008-09-22 09:26:57 +00:00
|
|
|
void OptionParser::addOptionHandler
|
|
|
|
(const SharedHandle<OptionHandler>& optionHandler)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
optionHandler->setOptionID(++idCounter_);
|
2009-12-20 14:25:51 +00:00
|
|
|
std::vector<SharedHandle<OptionHandler> >::iterator i =
|
2010-06-21 13:51:56 +00:00
|
|
|
std::lower_bound(optionHandlers_.begin(), optionHandlers_.end(),
|
2010-01-05 16:01:46 +00:00
|
|
|
optionHandler, OptionHandlerNameLesser());
|
2010-06-21 13:51:56 +00:00
|
|
|
optionHandlers_.insert(i, optionHandler);
|
2008-09-22 09:26:57 +00:00
|
|
|
}
|
|
|
|
|
2009-02-07 11:00:34 +00:00
|
|
|
void OptionParser::parseDefaultValues(Option& option) const
|
2008-09-22 09:26:57 +00:00
|
|
|
{
|
2009-12-20 14:25:51 +00:00
|
|
|
for(std::vector<SharedHandle<OptionHandler> >::const_iterator i =
|
2010-06-21 13:51:56 +00:00
|
|
|
optionHandlers_.begin(), eoi = optionHandlers_.end();
|
2010-02-28 16:04:52 +00:00
|
|
|
i != eoi; ++i) {
|
2008-09-22 09:26:57 +00:00
|
|
|
if(!(*i)->getDefaultValue().empty()) {
|
|
|
|
(*i)->parse(option, (*i)->getDefaultValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-31 13:33:05 +00:00
|
|
|
namespace {
|
2009-12-20 14:25:51 +00:00
|
|
|
class FindOptionHandlerByTag :
|
2010-01-05 16:01:46 +00:00
|
|
|
public std::unary_function<SharedHandle<OptionHandler>, bool> {
|
2008-09-22 09:26:57 +00:00
|
|
|
private:
|
2010-06-21 13:51:56 +00:00
|
|
|
std::string tag_;
|
2008-09-22 09:26:57 +00:00
|
|
|
public:
|
2010-06-21 13:51:56 +00:00
|
|
|
FindOptionHandlerByTag(const std::string& tag):tag_(tag) {}
|
2008-09-22 09:26:57 +00:00
|
|
|
|
|
|
|
bool operator()(const SharedHandle<OptionHandler>& optionHandler) const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return !optionHandler->isHidden() && optionHandler->hasTag(tag_);
|
2008-09-22 09:26:57 +00:00
|
|
|
}
|
|
|
|
};
|
2010-10-30 14:53:40 +00:00
|
|
|
} // namespace
|
2008-09-22 09:26:57 +00:00
|
|
|
|
2009-12-20 14:25:51 +00:00
|
|
|
std::vector<SharedHandle<OptionHandler> >
|
2008-09-22 09:26:57 +00:00
|
|
|
OptionParser::findByTag(const std::string& tag) const
|
|
|
|
{
|
2009-12-20 14:25:51 +00:00
|
|
|
std::vector<SharedHandle<OptionHandler> > result;
|
2010-06-21 13:51:56 +00:00
|
|
|
std::remove_copy_if(optionHandlers_.begin(), optionHandlers_.end(),
|
2010-01-05 16:01:46 +00:00
|
|
|
std::back_inserter(result),
|
|
|
|
std::not1(FindOptionHandlerByTag(tag)));
|
2009-12-20 14:25:51 +00:00
|
|
|
std::sort(result.begin(), result.end(), OptionHandlerIDLesser());
|
2008-09-22 09:26:57 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-08-31 13:33:05 +00:00
|
|
|
namespace {
|
2009-12-20 14:25:51 +00:00
|
|
|
class FindOptionHandlerByNameSubstring :
|
2010-01-05 16:01:46 +00:00
|
|
|
public std::unary_function<SharedHandle<OptionHandler> , bool> {
|
2008-09-22 09:26:57 +00:00
|
|
|
private:
|
2010-06-21 13:51:56 +00:00
|
|
|
std::string substring_;
|
2008-09-22 09:26:57 +00:00
|
|
|
public:
|
2009-12-20 14:25:51 +00:00
|
|
|
FindOptionHandlerByNameSubstring
|
2010-06-21 13:51:56 +00:00
|
|
|
(const std::string& substring):substring_(substring) {}
|
2008-09-22 09:26:57 +00:00
|
|
|
|
|
|
|
bool operator()(const SharedHandle<OptionHandler>& optionHandler) const
|
|
|
|
{
|
|
|
|
return !optionHandler->isHidden() &&
|
2010-06-21 13:51:56 +00:00
|
|
|
optionHandler->getName().find(substring_) != std::string::npos;
|
2008-09-22 09:26:57 +00:00
|
|
|
}
|
|
|
|
};
|
2010-10-30 14:53:40 +00:00
|
|
|
} // namespace
|
2008-09-22 09:26:57 +00:00
|
|
|
|
2009-12-20 14:25:51 +00:00
|
|
|
std::vector<SharedHandle<OptionHandler> >
|
2008-09-22 09:26:57 +00:00
|
|
|
OptionParser::findByNameSubstring(const std::string& substring) const
|
|
|
|
{
|
2009-12-20 14:25:51 +00:00
|
|
|
std::vector<SharedHandle<OptionHandler> > result;
|
2010-06-21 13:51:56 +00:00
|
|
|
std::remove_copy_if(optionHandlers_.begin(), optionHandlers_.end(),
|
2010-01-05 16:01:46 +00:00
|
|
|
std::back_inserter(result),
|
|
|
|
std::not1(FindOptionHandlerByNameSubstring(substring)));
|
2009-12-20 14:25:51 +00:00
|
|
|
std::sort(result.begin(), result.end(), OptionHandlerIDLesser());
|
2008-09-22 09:26:57 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-12-20 14:25:51 +00:00
|
|
|
std::vector<SharedHandle<OptionHandler> > OptionParser::findAll() const
|
2009-02-07 11:00:34 +00:00
|
|
|
{
|
2009-12-20 14:25:51 +00:00
|
|
|
std::vector<SharedHandle<OptionHandler> > result;
|
2010-06-21 13:51:56 +00:00
|
|
|
std::remove_copy_if(optionHandlers_.begin(), optionHandlers_.end(),
|
2010-01-05 16:01:46 +00:00
|
|
|
std::back_inserter(result),
|
|
|
|
mem_fun_sh(&OptionHandler::isHidden));
|
2009-12-20 14:25:51 +00:00
|
|
|
std::sort(result.begin(), result.end(), OptionHandlerIDLesser());
|
2009-02-07 11:00:34 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
2009-02-07 11:00:34 +00:00
|
|
|
template<typename InputIterator, typename Predicate>
|
2010-10-30 16:02:15 +00:00
|
|
|
SharedHandle<OptionHandler> findOptionHandler
|
2009-02-07 11:00:34 +00:00
|
|
|
(InputIterator first, InputIterator last, Predicate pred)
|
|
|
|
{
|
2009-12-20 14:25:51 +00:00
|
|
|
SharedHandle<OptionHandler> handler;
|
2009-02-07 11:00:34 +00:00
|
|
|
InputIterator i = std::find_if(first, last, pred);
|
2009-12-20 14:25:51 +00:00
|
|
|
if(i != last) {
|
|
|
|
handler = *i;
|
2009-02-07 11:00:34 +00:00
|
|
|
}
|
2009-12-20 14:25:51 +00:00
|
|
|
return handler;
|
2009-02-07 11:00:34 +00:00
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2009-02-07 11:00:34 +00:00
|
|
|
|
|
|
|
SharedHandle<OptionHandler>
|
|
|
|
OptionParser::findByName(const std::string& name) const
|
2008-09-22 09:26:57 +00:00
|
|
|
{
|
2009-12-20 14:25:51 +00:00
|
|
|
SharedHandle<OptionHandler> handler(new DummyOptionHandler(name));
|
|
|
|
std::vector<SharedHandle<OptionHandler> >::const_iterator i =
|
2010-06-21 13:51:56 +00:00
|
|
|
std::lower_bound(optionHandlers_.begin(), optionHandlers_.end(),
|
2010-01-05 16:01:46 +00:00
|
|
|
handler, OptionHandlerNameLesser());
|
2010-06-21 13:51:56 +00:00
|
|
|
if(i != optionHandlers_.end() && (*i)->getName() == name &&
|
2010-01-23 12:10:43 +00:00
|
|
|
!(*i)->isHidden()) {
|
2009-12-20 14:25:51 +00:00
|
|
|
handler = *i;
|
2010-01-23 12:10:43 +00:00
|
|
|
} else {
|
|
|
|
handler.reset();
|
2009-12-20 14:25:51 +00:00
|
|
|
}
|
|
|
|
return handler;
|
2008-09-22 09:26:57 +00:00
|
|
|
}
|
|
|
|
|
2010-08-31 13:33:05 +00:00
|
|
|
namespace {
|
2009-12-20 14:25:51 +00:00
|
|
|
class FindOptionHandlerByID:public std::unary_function
|
|
|
|
<SharedHandle<OptionHandler>, bool> {
|
2009-02-07 11:00:34 +00:00
|
|
|
private:
|
2010-06-21 13:51:56 +00:00
|
|
|
int id_;
|
2009-02-07 11:00:34 +00:00
|
|
|
public:
|
2010-06-21 13:51:56 +00:00
|
|
|
FindOptionHandlerByID(int id):id_(id) {}
|
2009-02-07 11:00:34 +00:00
|
|
|
|
|
|
|
bool operator()(const SharedHandle<OptionHandler>& optionHandler) const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return !optionHandler->isHidden() && optionHandler->getOptionID() == id_;
|
2009-02-07 11:00:34 +00:00
|
|
|
}
|
|
|
|
};
|
2010-10-30 14:53:40 +00:00
|
|
|
} // namespace
|
2009-02-07 11:00:34 +00:00
|
|
|
|
|
|
|
SharedHandle<OptionHandler> OptionParser::findByID(int id) const
|
2008-09-22 09:26:57 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return findOptionHandler(optionHandlers_.begin(), optionHandlers_.end(),
|
2010-01-05 16:01:46 +00:00
|
|
|
FindOptionHandlerByID(id));
|
2009-02-07 11:00:34 +00:00
|
|
|
}
|
|
|
|
|
2010-08-31 13:33:05 +00:00
|
|
|
namespace {
|
2009-12-20 14:25:51 +00:00
|
|
|
class FindOptionHandlerByShortName:
|
2009-02-07 11:00:34 +00:00
|
|
|
public std::unary_function<SharedHandle<OptionHandler>, bool> {
|
|
|
|
private:
|
2010-06-21 13:51:56 +00:00
|
|
|
char shortName_;
|
2009-02-07 11:00:34 +00:00
|
|
|
public:
|
2010-06-21 13:51:56 +00:00
|
|
|
FindOptionHandlerByShortName(char shortName):shortName_(shortName) {}
|
2009-02-07 11:00:34 +00:00
|
|
|
|
|
|
|
bool operator()(const SharedHandle<OptionHandler>& optionHandler) const
|
|
|
|
{
|
|
|
|
return !optionHandler->isHidden() &&
|
2010-06-21 13:51:56 +00:00
|
|
|
optionHandler->getShortName() == shortName_;
|
2008-09-22 09:26:57 +00:00
|
|
|
}
|
2009-02-07 11:00:34 +00:00
|
|
|
};
|
2010-10-30 14:53:40 +00:00
|
|
|
} // namespace
|
2009-02-07 11:00:34 +00:00
|
|
|
|
|
|
|
SharedHandle<OptionHandler> OptionParser::findByShortName(char shortName) const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return findOptionHandler(optionHandlers_.begin(), optionHandlers_.end(),
|
2010-01-05 16:01:46 +00:00
|
|
|
FindOptionHandlerByShortName(shortName));
|
2008-09-22 09:26:57 +00:00
|
|
|
}
|
|
|
|
|
2009-02-07 11:00:34 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
SharedHandle<OptionParser> OptionParser::optionParser_;
|
2009-05-08 07:58:50 +00:00
|
|
|
|
2010-05-08 12:25:28 +00:00
|
|
|
const SharedHandle<OptionParser>& OptionParser::getInstance()
|
2009-05-08 07:58:50 +00:00
|
|
|
{
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!optionParser_) {
|
2010-06-21 13:51:56 +00:00
|
|
|
optionParser_.reset(new OptionParser());
|
|
|
|
optionParser_->setOptionHandlers(OptionHandlerFactory::createOptionHandlers());
|
2009-05-08 07:58:50 +00:00
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
return optionParser_;
|
2009-05-08 07:58:50 +00:00
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|