mirror of https://github.com/aria2/aria2
Rewritten OptionParser. Made it simpler and efficient.
parent
3832ed97c6
commit
d5c8d048ef
|
@ -108,6 +108,11 @@ public:
|
|||
hidden_ = true;
|
||||
}
|
||||
|
||||
virtual const Pref* getPref() const
|
||||
{
|
||||
return pref_;
|
||||
}
|
||||
|
||||
virtual char getShortName() const
|
||||
{
|
||||
return shortName_;
|
||||
|
|
|
@ -56,6 +56,7 @@ extern const std::string PATH_TO_DIR;
|
|||
extern const std::string PATH_TO_COMMAND;
|
||||
|
||||
class Option;
|
||||
class Pref;
|
||||
|
||||
class OptionHandler {
|
||||
public:
|
||||
|
@ -82,6 +83,8 @@ public:
|
|||
|
||||
virtual void hide() = 0;
|
||||
|
||||
virtual const Pref* getPref() const = 0;
|
||||
|
||||
enum ARG_TYPE {
|
||||
REQ_ARG,
|
||||
OPT_ARG,
|
||||
|
|
|
@ -63,11 +63,6 @@ OptionHandlerException::OptionHandlerException
|
|||
|
||||
OptionHandlerException::~OptionHandlerException() throw() {}
|
||||
|
||||
const std::string& OptionHandlerException::getOptionName() const throw()
|
||||
{
|
||||
return pref_->k;
|
||||
}
|
||||
|
||||
SharedHandle<Exception> OptionHandlerException::copy() const
|
||||
{
|
||||
SharedHandle<Exception> e(new OptionHandlerException(*this));
|
||||
|
|
|
@ -57,7 +57,10 @@ public:
|
|||
|
||||
virtual ~OptionHandlerException() throw();
|
||||
|
||||
const std::string& getOptionName() const throw();
|
||||
const Pref* getPref() const
|
||||
{
|
||||
return pref_;
|
||||
}
|
||||
};
|
||||
|
||||
#define OPTION_HANDLER_EXCEPTION(arg) \
|
||||
|
|
|
@ -63,69 +63,6 @@
|
|||
|
||||
namespace aria2 {
|
||||
|
||||
NullOptionHandler::~NullOptionHandler() {}
|
||||
|
||||
bool NullOptionHandler::canHandle(const std::string& optName) { return true; }
|
||||
|
||||
void NullOptionHandler::parse(Option& option, const std::string& arg) {}
|
||||
|
||||
bool NullOptionHandler::hasTag(const std::string& tag) const { return false; }
|
||||
|
||||
void NullOptionHandler::addTag(const std::string& tag) {}
|
||||
|
||||
std::string NullOptionHandler::toTagString() const { return A2STR::NIL; }
|
||||
|
||||
const std::string& NullOptionHandler::getName() const { return A2STR::NIL; }
|
||||
|
||||
const std::string& NullOptionHandler::getDescription() const
|
||||
{
|
||||
return A2STR::NIL;
|
||||
}
|
||||
|
||||
const std::string& NullOptionHandler::getDefaultValue() const
|
||||
{
|
||||
return A2STR::NIL;
|
||||
}
|
||||
|
||||
std::string NullOptionHandler::createPossibleValuesString() const
|
||||
{
|
||||
return A2STR::NIL;
|
||||
}
|
||||
|
||||
bool NullOptionHandler::isHidden() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void NullOptionHandler::hide() {}
|
||||
|
||||
OptionHandler::ARG_TYPE NullOptionHandler::getArgType() const
|
||||
{
|
||||
return OptionHandler::NO_ARG;
|
||||
}
|
||||
|
||||
int NullOptionHandler::getOptionID() const
|
||||
{
|
||||
return id_;
|
||||
}
|
||||
|
||||
void NullOptionHandler::setOptionID(int id)
|
||||
{
|
||||
id_ = id;
|
||||
}
|
||||
|
||||
char NullOptionHandler::getShortName() const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool NullOptionHandler::getEraseAfterParse() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void NullOptionHandler::setEraseAfterParse(bool eraseAfterParse) {}
|
||||
|
||||
BooleanOptionHandler::BooleanOptionHandler
|
||||
(const Pref* pref,
|
||||
const std::string& description,
|
||||
|
@ -828,6 +765,11 @@ void DeprecatedOptionHandler::hide()
|
|||
depOptHandler_->hide();
|
||||
}
|
||||
|
||||
const Pref* DeprecatedOptionHandler::getPref() const
|
||||
{
|
||||
return depOptHandler_->getPref();
|
||||
}
|
||||
|
||||
OptionHandler::ARG_TYPE DeprecatedOptionHandler::getArgType() const
|
||||
{
|
||||
return depOptHandler_->getArgType();
|
||||
|
|
|
@ -46,30 +46,6 @@ namespace aria2 {
|
|||
class Option;
|
||||
class Pref;
|
||||
|
||||
class NullOptionHandler : public OptionHandler {
|
||||
private:
|
||||
int id_;
|
||||
public:
|
||||
virtual ~NullOptionHandler();
|
||||
virtual bool canHandle(const std::string& optName);
|
||||
virtual void parse(Option& option, const std::string& arg);
|
||||
virtual bool hasTag(const std::string& tag) const;
|
||||
virtual void addTag(const std::string& tag);
|
||||
virtual std::string toTagString() const;
|
||||
virtual const std::string& getName() const;
|
||||
virtual const std::string& getDescription() const;
|
||||
virtual const std::string& getDefaultValue() const;
|
||||
virtual std::string createPossibleValuesString() const;
|
||||
virtual bool isHidden() const;
|
||||
virtual void hide();
|
||||
virtual OptionHandler::ARG_TYPE getArgType() const;
|
||||
virtual int getOptionID() const;
|
||||
virtual void setOptionID(int id);
|
||||
virtual char getShortName() const;
|
||||
virtual bool getEraseAfterParse() const;
|
||||
virtual void setEraseAfterParse(bool eraseAfterParse);
|
||||
};
|
||||
|
||||
class BooleanOptionHandler : public NameMatchOptionHandler {
|
||||
public:
|
||||
BooleanOptionHandler(const Pref* pref,
|
||||
|
@ -329,6 +305,7 @@ public:
|
|||
virtual const std::string& getDefaultValue() const;
|
||||
virtual bool isHidden() const;
|
||||
virtual void hide();
|
||||
virtual const Pref* getPref() const;
|
||||
virtual ARG_TYPE getArgType() const;
|
||||
virtual char getShortName() const;
|
||||
virtual int getOptionID() const;
|
||||
|
|
|
@ -54,8 +54,9 @@
|
|||
|
||||
namespace aria2 {
|
||||
|
||||
OptionParser::OptionParser():
|
||||
idCounter_(0)
|
||||
OptionParser::OptionParser()
|
||||
: handlers_(option::countOption()),
|
||||
shortOpts_(256)
|
||||
{}
|
||||
|
||||
OptionParser::~OptionParser() {}
|
||||
|
@ -66,7 +67,7 @@ size_t countPublicOption(InputIterator first, InputIterator last)
|
|||
{
|
||||
size_t count = 0;
|
||||
for(; first != last; ++first) {
|
||||
if(!(*first)->isHidden()) {
|
||||
if(*first && !(*first)->isHidden()) {
|
||||
++count;
|
||||
}
|
||||
}
|
||||
|
@ -80,7 +81,7 @@ void putOptions(struct option* longOpts, int* plopt,
|
|||
InputIterator first, InputIterator last)
|
||||
{
|
||||
for(; first != last; ++first) {
|
||||
if(!(*first)->isHidden()) {
|
||||
if(*first && !(*first)->isHidden()) {
|
||||
#ifdef HAVE_OPTION_CONST_NAME
|
||||
(*longOpts).name = (*first)->getName().c_str();
|
||||
#else // !HAVE_OPTION_CONST_NAME
|
||||
|
@ -101,7 +102,7 @@ void putOptions(struct option* longOpts, int* plopt,
|
|||
}
|
||||
if((*first)->getShortName() == 0) {
|
||||
(*longOpts).flag = plopt;
|
||||
(*longOpts).val = (*first)->getOptionID();
|
||||
(*longOpts).val = (*first)->getPref()->i;
|
||||
} else {
|
||||
(*longOpts).flag = 0;
|
||||
(*longOpts).val = (*first)->getShortName();
|
||||
|
@ -122,7 +123,7 @@ std::string createOptstring(InputIterator first, InputIterator last)
|
|||
{
|
||||
std::string str = "";
|
||||
for(; first != last; ++first) {
|
||||
if(!(*first)->isHidden()) {
|
||||
if(*first && !(*first)->isHidden()) {
|
||||
if((*first)->getShortName() != 0) {
|
||||
str += (*first)->getShortName();
|
||||
if((*first)->getArgType() == OptionHandler::REQ_ARG) {
|
||||
|
@ -139,15 +140,14 @@ std::string createOptstring(InputIterator first, InputIterator last)
|
|||
|
||||
void OptionParser::parseArg
|
||||
(std::ostream& out, std::vector<std::string>& nonopts,
|
||||
int argc, char* argv[])
|
||||
int argc, char* argv[]) const
|
||||
{
|
||||
size_t numPublicOption = countPublicOption(optionHandlers_.begin(),
|
||||
optionHandlers_.end());
|
||||
size_t numPublicOption = countPublicOption(handlers_.begin(),
|
||||
handlers_.end());
|
||||
int lopt;
|
||||
array_ptr<struct option> longOpts(new struct option[numPublicOption+1]);
|
||||
putOptions(longOpts, &lopt,optionHandlers_.begin(),optionHandlers_.end());
|
||||
std::string optstring = createOptstring(optionHandlers_.begin(),
|
||||
optionHandlers_.end());
|
||||
putOptions(longOpts, &lopt,handlers_.begin(),handlers_.end());
|
||||
std::string optstring = createOptstring(handlers_.begin(), handlers_.end());
|
||||
while(1) {
|
||||
int c = getopt_long(argc, argv, optstring.c_str(), longOpts, 0);
|
||||
if(c == -1) {
|
||||
|
@ -155,7 +155,7 @@ void OptionParser::parseArg
|
|||
}
|
||||
SharedHandle<OptionHandler> op;
|
||||
if(c == 0) {
|
||||
op = findByID(lopt);
|
||||
op = findById(lopt);
|
||||
} else {
|
||||
op = findByShortName(c);
|
||||
}
|
||||
|
@ -177,12 +177,10 @@ void OptionParser::parseArg
|
|||
std::copy(argv+optind, argv+argc, std::back_inserter(nonopts));
|
||||
}
|
||||
|
||||
void OptionParser::parse(Option& option, std::istream& is)
|
||||
void OptionParser::parse(Option& option, std::istream& is) const
|
||||
{
|
||||
std::string line;
|
||||
int32_t linenum = 0;
|
||||
while(getline(is, line)) {
|
||||
++linenum;
|
||||
if(util::startsWith(line, A2STR::SHARP_C)) {
|
||||
continue;
|
||||
}
|
||||
|
@ -191,217 +189,106 @@ void OptionParser::parse(Option& option, std::istream& is)
|
|||
if(nv.first.empty()) {
|
||||
continue;
|
||||
}
|
||||
OptionHandlerHandle handler = getOptionHandlerByName(nv.first);
|
||||
handler->parse(option, nv.second);
|
||||
const SharedHandle<OptionHandler>& handler = find(option::k2p(nv.first));
|
||||
if(handler) {
|
||||
handler->parse(option, nv.second);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
class DummyOptionHandler:public NullOptionHandler {
|
||||
protected:
|
||||
virtual void parseArg(Option& option, const std::string& arg) {}
|
||||
public:
|
||||
DummyOptionHandler(const std::string& optName)
|
||||
: NullOptionHandler(),
|
||||
optName_(optName)
|
||||
{}
|
||||
|
||||
virtual const std::string& getName() const
|
||||
{
|
||||
return optName_;
|
||||
}
|
||||
private:
|
||||
std::string optName_;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
OptionHandlerHandle OptionParser::getOptionHandlerByName
|
||||
(const std::string& optName)
|
||||
{
|
||||
SharedHandle<OptionHandler> handler(new DummyOptionHandler(optName));
|
||||
std::vector<SharedHandle<OptionHandler> >::const_iterator i =
|
||||
std::lower_bound(optionHandlers_.begin(), optionHandlers_.end(),
|
||||
handler, OptionHandlerNameLesser());
|
||||
if(i != optionHandlers_.end() && (*i)->canHandle(optName)) {
|
||||
handler = *i;
|
||||
} else {
|
||||
handler.reset(new NullOptionHandler());
|
||||
}
|
||||
return handler;
|
||||
}
|
||||
|
||||
void OptionParser::setOptionHandlers
|
||||
(const std::vector<SharedHandle<OptionHandler> >& optionHandlers)
|
||||
(const std::vector<SharedHandle<OptionHandler> >& handlers)
|
||||
{
|
||||
optionHandlers_ = optionHandlers;
|
||||
for(std::vector<SharedHandle<OptionHandler> >::const_iterator i =
|
||||
optionHandlers_.begin(), eoi = optionHandlers_.end();
|
||||
i != eoi; ++i) {
|
||||
(*i)->setOptionID(++idCounter_);
|
||||
handlers.begin(), eoi = handlers.end(); i != eoi; ++i) {
|
||||
addOptionHandler(*i);
|
||||
}
|
||||
std::sort(optionHandlers_.begin(), optionHandlers_.end(),
|
||||
OptionHandlerNameLesser());
|
||||
}
|
||||
|
||||
void OptionParser::addOptionHandler
|
||||
(const SharedHandle<OptionHandler>& optionHandler)
|
||||
void OptionParser::addOptionHandler(const SharedHandle<OptionHandler>& handler)
|
||||
{
|
||||
optionHandler->setOptionID(++idCounter_);
|
||||
std::vector<SharedHandle<OptionHandler> >::iterator i =
|
||||
std::lower_bound(optionHandlers_.begin(), optionHandlers_.end(),
|
||||
optionHandler, OptionHandlerNameLesser());
|
||||
optionHandlers_.insert(i, optionHandler);
|
||||
size_t optId = handler->getPref()->i;
|
||||
assert(optId < handlers_.size());
|
||||
handlers_[optId] = handler;
|
||||
if(handler->getShortName()) {
|
||||
shortOpts_[static_cast<unsigned char>(handler->getShortName())] = optId;
|
||||
}
|
||||
}
|
||||
|
||||
void OptionParser::parseDefaultValues(Option& option) const
|
||||
{
|
||||
for(std::vector<SharedHandle<OptionHandler> >::const_iterator i =
|
||||
optionHandlers_.begin(), eoi = optionHandlers_.end();
|
||||
i != eoi; ++i) {
|
||||
if(!(*i)->getDefaultValue().empty()) {
|
||||
handlers_.begin(), eoi = handlers_.end(); i != eoi; ++i) {
|
||||
if(*i && !(*i)->getDefaultValue().empty()) {
|
||||
(*i)->parse(option, (*i)->getDefaultValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
class FindOptionHandlerByTag :
|
||||
public std::unary_function<SharedHandle<OptionHandler>, bool> {
|
||||
private:
|
||||
std::string tag_;
|
||||
public:
|
||||
FindOptionHandlerByTag(const std::string& tag):tag_(tag) {}
|
||||
|
||||
bool operator()(const SharedHandle<OptionHandler>& optionHandler) const
|
||||
{
|
||||
return !optionHandler->isHidden() && optionHandler->hasTag(tag_);
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
std::vector<SharedHandle<OptionHandler> >
|
||||
OptionParser::findByTag(const std::string& tag) const
|
||||
{
|
||||
std::vector<SharedHandle<OptionHandler> > result;
|
||||
std::remove_copy_if(optionHandlers_.begin(), optionHandlers_.end(),
|
||||
std::back_inserter(result),
|
||||
std::not1(FindOptionHandlerByTag(tag)));
|
||||
std::sort(result.begin(), result.end(), OptionHandlerIDLesser());
|
||||
for(std::vector<SharedHandle<OptionHandler> >::const_iterator i =
|
||||
handlers_.begin(), eoi = handlers_.end(); i != eoi; ++i) {
|
||||
if(*i && !(*i)->isHidden() && (*i)->hasTag(tag)) {
|
||||
result.push_back(*i);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
namespace {
|
||||
class FindOptionHandlerByNameSubstring :
|
||||
public std::unary_function<SharedHandle<OptionHandler> , bool> {
|
||||
private:
|
||||
std::string substring_;
|
||||
public:
|
||||
FindOptionHandlerByNameSubstring
|
||||
(const std::string& substring):substring_(substring) {}
|
||||
|
||||
bool operator()(const SharedHandle<OptionHandler>& optionHandler) const
|
||||
{
|
||||
return !optionHandler->isHidden() &&
|
||||
optionHandler->getName().find(substring_) != std::string::npos;
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
std::vector<SharedHandle<OptionHandler> >
|
||||
OptionParser::findByNameSubstring(const std::string& substring) const
|
||||
{
|
||||
std::vector<SharedHandle<OptionHandler> > result;
|
||||
std::remove_copy_if(optionHandlers_.begin(), optionHandlers_.end(),
|
||||
std::back_inserter(result),
|
||||
std::not1(FindOptionHandlerByNameSubstring(substring)));
|
||||
std::sort(result.begin(), result.end(), OptionHandlerIDLesser());
|
||||
for(std::vector<SharedHandle<OptionHandler> >::const_iterator i =
|
||||
handlers_.begin(), eoi = handlers_.end(); i != eoi; ++i) {
|
||||
if(*i && !(*i)->isHidden() &&
|
||||
(*i)->getName().find(substring) != std::string::npos) {
|
||||
result.push_back(*i);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<SharedHandle<OptionHandler> > OptionParser::findAll() const
|
||||
{
|
||||
std::vector<SharedHandle<OptionHandler> > result;
|
||||
std::remove_copy_if(optionHandlers_.begin(), optionHandlers_.end(),
|
||||
std::back_inserter(result),
|
||||
mem_fun_sh(&OptionHandler::isHidden));
|
||||
std::sort(result.begin(), result.end(), OptionHandlerIDLesser());
|
||||
for(std::vector<SharedHandle<OptionHandler> >::const_iterator i =
|
||||
handlers_.begin(), eoi = handlers_.end(); i != eoi; ++i) {
|
||||
if(*i && !(*i)->isHidden()) {
|
||||
result.push_back(*i);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
namespace {
|
||||
template<typename InputIterator, typename Predicate>
|
||||
SharedHandle<OptionHandler> findOptionHandler
|
||||
(InputIterator first, InputIterator last, Predicate pred)
|
||||
const SharedHandle<OptionHandler>& OptionParser::find(const Pref* pref) const
|
||||
{
|
||||
SharedHandle<OptionHandler> handler;
|
||||
InputIterator i = std::find_if(first, last, pred);
|
||||
if(i != last) {
|
||||
handler = *i;
|
||||
}
|
||||
return handler;
|
||||
return findById(pref->i);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
SharedHandle<OptionHandler>
|
||||
OptionParser::findByName(const std::string& name) const
|
||||
const SharedHandle<OptionHandler>& OptionParser::findById(size_t id) const
|
||||
{
|
||||
SharedHandle<OptionHandler> handler(new DummyOptionHandler(name));
|
||||
std::vector<SharedHandle<OptionHandler> >::const_iterator i =
|
||||
std::lower_bound(optionHandlers_.begin(), optionHandlers_.end(),
|
||||
handler, OptionHandlerNameLesser());
|
||||
if(i != optionHandlers_.end() && (*i)->getName() == name &&
|
||||
!(*i)->isHidden()) {
|
||||
handler = *i;
|
||||
if(id >= handlers_.size()) {
|
||||
return handlers_[0];
|
||||
}
|
||||
const SharedHandle<OptionHandler>& h = handlers_[id];
|
||||
if(!h || h->isHidden()) {
|
||||
return handlers_[0];
|
||||
} else {
|
||||
handler.reset();
|
||||
return h;
|
||||
}
|
||||
return handler;
|
||||
}
|
||||
|
||||
namespace {
|
||||
class FindOptionHandlerByID:public std::unary_function
|
||||
<SharedHandle<OptionHandler>, bool> {
|
||||
private:
|
||||
int id_;
|
||||
public:
|
||||
FindOptionHandlerByID(int id):id_(id) {}
|
||||
|
||||
bool operator()(const SharedHandle<OptionHandler>& optionHandler) const
|
||||
{
|
||||
return !optionHandler->isHidden() && optionHandler->getOptionID() == id_;
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
SharedHandle<OptionHandler> OptionParser::findByID(int id) const
|
||||
const SharedHandle<OptionHandler>& OptionParser::findByShortName
|
||||
(char shortName) const
|
||||
{
|
||||
return findOptionHandler(optionHandlers_.begin(), optionHandlers_.end(),
|
||||
FindOptionHandlerByID(id));
|
||||
size_t idx = static_cast<unsigned char>(shortName);
|
||||
return findById(shortOpts_[idx]);
|
||||
}
|
||||
|
||||
namespace {
|
||||
class FindOptionHandlerByShortName:
|
||||
public std::unary_function<SharedHandle<OptionHandler>, bool> {
|
||||
private:
|
||||
char shortName_;
|
||||
public:
|
||||
FindOptionHandlerByShortName(char shortName):shortName_(shortName) {}
|
||||
|
||||
bool operator()(const SharedHandle<OptionHandler>& optionHandler) const
|
||||
{
|
||||
return !optionHandler->isHidden() &&
|
||||
optionHandler->getShortName() == shortName_;
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
SharedHandle<OptionHandler> OptionParser::findByShortName(char shortName) const
|
||||
{
|
||||
return findOptionHandler(optionHandlers_.begin(), optionHandlers_.end(),
|
||||
FindOptionHandlerByShortName(shortName));
|
||||
}
|
||||
|
||||
|
||||
SharedHandle<OptionParser> OptionParser::optionParser_;
|
||||
|
||||
const SharedHandle<OptionParser>& OptionParser::getInstance()
|
||||
|
|
|
@ -47,38 +47,32 @@ namespace aria2 {
|
|||
|
||||
class Option;
|
||||
class OptionHandler;
|
||||
class Pref;
|
||||
|
||||
class OptionParser {
|
||||
private:
|
||||
int idCounter_;
|
||||
|
||||
// optionHandlers_ is sorted by OptionHandler::getName() in
|
||||
// ascending order.
|
||||
std::vector<SharedHandle<OptionHandler> > optionHandlers_;
|
||||
|
||||
SharedHandle<OptionHandler>
|
||||
getOptionHandlerByName(const std::string& optName);
|
||||
|
||||
std::vector<SharedHandle<OptionHandler> > handlers_;
|
||||
// Index of handler in handlers_ for option who has short option name.
|
||||
std::vector<size_t> shortOpts_;
|
||||
static SharedHandle<OptionParser> optionParser_;
|
||||
public:
|
||||
OptionParser();
|
||||
|
||||
~OptionParser();
|
||||
|
||||
// Parses options in argv and writes option name and value to out in
|
||||
// NAME=VALUE format. Non-option strings are stored in nonopts.
|
||||
// Throws Exception when an unrecognized option is found.
|
||||
void parseArg(std::ostream& out, std::vector<std::string>& nonopts,
|
||||
int argc, char* argv[]);
|
||||
int argc, char* argv[]) const;
|
||||
|
||||
void parse(Option& option, std::istream& ios);
|
||||
void parse(Option& option, std::istream& ios) const;
|
||||
|
||||
void parseDefaultValues(Option& option) const;
|
||||
|
||||
void setOptionHandlers
|
||||
(const std::vector<SharedHandle<OptionHandler> >& optionHandlers);
|
||||
(const std::vector<SharedHandle<OptionHandler> >& handlers);
|
||||
|
||||
void addOptionHandler(const SharedHandle<OptionHandler>& optionHandler);
|
||||
void addOptionHandler(const SharedHandle<OptionHandler>& handler);
|
||||
|
||||
// Hidden options are not returned.
|
||||
std::vector<SharedHandle<OptionHandler> >
|
||||
|
@ -92,20 +86,17 @@ public:
|
|||
std::vector<SharedHandle<OptionHandler> > findAll() const;
|
||||
|
||||
// Hidden options are not returned.
|
||||
SharedHandle<OptionHandler>
|
||||
findByName(const std::string& name) const;
|
||||
const SharedHandle<OptionHandler>& find(const Pref* pref) const;
|
||||
|
||||
// Hidden options are not returned.
|
||||
SharedHandle<OptionHandler> findByID(int id) const;
|
||||
const SharedHandle<OptionHandler>& findById(size_t id) const;
|
||||
|
||||
// Hidden options are not returned.
|
||||
SharedHandle<OptionHandler> findByShortName(char shortName) const;
|
||||
const SharedHandle<OptionHandler>& findByShortName(char shortName) const;
|
||||
|
||||
static const SharedHandle<OptionParser>& getInstance();
|
||||
};
|
||||
|
||||
typedef SharedHandle<OptionParser> OptionParserHandle;
|
||||
|
||||
} // namespace aria2
|
||||
|
||||
#endif // D_OPTION_PARSER_H
|
||||
|
|
|
@ -93,16 +93,16 @@ void gatherOption
|
|||
(fmt("%s option cannot be used in this context.",
|
||||
optionName.c_str()));
|
||||
} else {
|
||||
SharedHandle<OptionHandler> optionHandler =
|
||||
optionParser->findByName(optionName);
|
||||
if(!optionHandler) {
|
||||
const Pref* pref = option::k2p(optionName);
|
||||
const SharedHandle<OptionHandler>& handler = optionParser->find(pref);
|
||||
if(!handler) {
|
||||
throw DL_ABORT_EX
|
||||
(fmt("We don't know how to deal with %s option",
|
||||
optionName.c_str()));
|
||||
}
|
||||
const String* opval = downcast<String>((*first).second);
|
||||
if(opval) {
|
||||
optionHandler->parse(*option.get(), opval->s());
|
||||
handler->parse(*option.get(), opval->s());
|
||||
} else {
|
||||
// header and index-out option can take array as value
|
||||
const List* oplist = downcast<List>((*first).second);
|
||||
|
@ -112,7 +112,7 @@ void gatherOption
|
|||
eoi = oplist->end(); argiter != eoi; ++argiter) {
|
||||
const String* opval = downcast<String>(*argiter);
|
||||
if(opval) {
|
||||
optionHandler->parse(*option.get(), opval->s());
|
||||
handler->parse(*option.get(), opval->s());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1244,8 +1244,8 @@ SharedHandle<ValueBase> GetGlobalOptionRpcMethod::process
|
|||
if(!e->getOption()->defined(pref)) {
|
||||
continue;
|
||||
}
|
||||
SharedHandle<OptionHandler> h = getOptionParser()->findByName(pref->k);
|
||||
if(h && !h->isHidden()) {
|
||||
const SharedHandle<OptionHandler>& h = getOptionParser()->find(pref);
|
||||
if(h) {
|
||||
result->put(pref->k, e->getOption()->get(pref));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,14 +43,13 @@
|
|||
#include "OptionHandler.h"
|
||||
#include "A2STR.h"
|
||||
#include "BufferedFile.h"
|
||||
#include "OptionParser.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
UriListParser::UriListParser(const std::string& filename)
|
||||
: fp_(filename, BufferedFile::READ)
|
||||
{
|
||||
optparser_.setOptionHandlers(OptionHandlerFactory::createOptionHandlers());
|
||||
}
|
||||
{}
|
||||
|
||||
UriListParser::~UriListParser() {}
|
||||
|
||||
|
@ -64,6 +63,7 @@ void UriListParser::parseNext(std::vector<std::string>& uris, Option& op)
|
|||
}
|
||||
line_.assign(&buf[0], &buf[strlen(buf)]);
|
||||
}
|
||||
const SharedHandle<OptionParser>& optparser = OptionParser::getInstance();
|
||||
while(1) {
|
||||
if(!util::startsWith(line_, A2STR::SHARP_C) && !util::strip(line_).empty()){
|
||||
util::split(line_, std::back_inserter(uris), "\t", true);
|
||||
|
@ -83,7 +83,7 @@ void UriListParser::parseNext(std::vector<std::string>& uris, Option& op)
|
|||
break;
|
||||
}
|
||||
}
|
||||
optparser_.parse(op, ss);
|
||||
optparser->parse(op, ss);
|
||||
return;
|
||||
}
|
||||
if(!fp_.getsn(buf, sizeof(buf))) {
|
||||
|
|
|
@ -42,7 +42,6 @@
|
|||
#include <iosfwd>
|
||||
|
||||
#include "Option.h"
|
||||
#include "OptionParser.h"
|
||||
#include "BufferedFile.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
@ -51,8 +50,6 @@ class UriListParser {
|
|||
private:
|
||||
BufferedFile fp_;
|
||||
|
||||
OptionParser optparser_;
|
||||
|
||||
std::string line_;
|
||||
public:
|
||||
UriListParser(const std::string& filename);
|
||||
|
|
|
@ -64,17 +64,20 @@
|
|||
namespace aria2 {
|
||||
|
||||
extern void showVersion();
|
||||
extern void showUsage(const std::string& keyword, const OptionParser& oparser);
|
||||
extern void showUsage
|
||||
(const std::string& keyword, const SharedHandle<OptionParser>& oparser);
|
||||
|
||||
namespace {
|
||||
void overrideWithEnv(Option& op, const OptionParser& optionParser,
|
||||
const Pref* pref,
|
||||
const std::string& envName)
|
||||
void overrideWithEnv
|
||||
(Option& op,
|
||||
const SharedHandle<OptionParser>& optionParser,
|
||||
const Pref* pref,
|
||||
const std::string& envName)
|
||||
{
|
||||
char* value = getenv(envName.c_str());
|
||||
if(value) {
|
||||
try {
|
||||
optionParser.findByName(pref->k)->parse(op, value);
|
||||
optionParser->find(pref)->parse(op, value);
|
||||
} catch(Exception& e) {
|
||||
global::cerr()->printf
|
||||
("Caught Error while parsing environment variable '%s'\n%s\n",
|
||||
|
@ -88,17 +91,16 @@ void overrideWithEnv(Option& op, const OptionParser& optionParser,
|
|||
void option_processing(Option& op, std::vector<std::string>& uris,
|
||||
int argc, char* argv[])
|
||||
{
|
||||
OptionParser oparser;
|
||||
oparser.setOptionHandlers(OptionHandlerFactory::createOptionHandlers());
|
||||
const SharedHandle<OptionParser>& oparser = OptionParser::getInstance();
|
||||
try {
|
||||
bool noConf = false;
|
||||
std::string ucfname;
|
||||
std::stringstream cmdstream;
|
||||
oparser.parseArg(cmdstream, uris, argc, argv);
|
||||
oparser->parseArg(cmdstream, uris, argc, argv);
|
||||
{
|
||||
// first evaluate --no-conf and --conf-path options.
|
||||
Option op;
|
||||
oparser.parse(op, cmdstream);
|
||||
oparser->parse(op, cmdstream);
|
||||
noConf = op.getAsBool(PREF_NO_CONF);
|
||||
ucfname = op.get(PREF_CONF_PATH);
|
||||
|
||||
|
@ -125,13 +127,12 @@ void option_processing(Option& op, std::vector<std::string>& uris,
|
|||
}
|
||||
}
|
||||
|
||||
oparser.parseDefaultValues(op);
|
||||
oparser->parseDefaultValues(op);
|
||||
|
||||
if(!noConf) {
|
||||
std::string cfname =
|
||||
ucfname.empty() ?
|
||||
oparser.findByName(PREF_CONF_PATH->k)->getDefaultValue():
|
||||
ucfname;
|
||||
oparser->find(PREF_CONF_PATH)->getDefaultValue() : ucfname;
|
||||
|
||||
if(File(cfname).isFile()) {
|
||||
std::stringstream ss;
|
||||
|
@ -142,16 +143,14 @@ void option_processing(Option& op, std::vector<std::string>& uris,
|
|||
}
|
||||
}
|
||||
try {
|
||||
oparser.parse(op, ss);
|
||||
oparser->parse(op, ss);
|
||||
} catch(OptionHandlerException& e) {
|
||||
global::cerr()->printf("Parse error in %s\n%s\n",
|
||||
cfname.c_str(),
|
||||
e.stackTrace().c_str());
|
||||
SharedHandle<OptionHandler> h = oparser.findByName(e.getOptionName());
|
||||
const SharedHandle<OptionHandler>& h = oparser->find(e.getPref());
|
||||
if(h) {
|
||||
global::cerr()->printf
|
||||
("Usage:\n%s\n",
|
||||
oparser.findByName(e.getOptionName())->getDescription().c_str());
|
||||
global::cerr()->printf("Usage:\n%s\n", h->getDescription().c_str());
|
||||
}
|
||||
exit(e.getErrorCode());
|
||||
} catch(Exception& e) {
|
||||
|
@ -178,7 +177,7 @@ void option_processing(Option& op, std::vector<std::string>& uris,
|
|||
cmdstream.clear();
|
||||
cmdstream.seekg(0, std::ios::beg);
|
||||
// finaly let's parse and store command-iine options.
|
||||
oparser.parse(op, cmdstream);
|
||||
oparser->parse(op, cmdstream);
|
||||
#ifdef __MINGW32__
|
||||
for(std::map<std::string, std::string>::iterator i = op.begin();
|
||||
i != op.end(); ++i) {
|
||||
|
@ -189,7 +188,7 @@ void option_processing(Option& op, std::vector<std::string>& uris,
|
|||
#endif // __MINGW32__
|
||||
} catch(OptionHandlerException& e) {
|
||||
global::cerr()->printf("%s\n", e.stackTrace().c_str());
|
||||
SharedHandle<OptionHandler> h = oparser.findByName(e.getOptionName());
|
||||
const SharedHandle<OptionHandler>& h = oparser->find(e.getPref());
|
||||
if(h) {
|
||||
std::ostringstream ss;
|
||||
ss << *h;
|
||||
|
|
|
@ -80,20 +80,22 @@ void showVersion() {
|
|||
<< _("Visit") << " " << PACKAGE_URL << std::endl;
|
||||
}
|
||||
|
||||
void showUsage(const std::string& keyword, const OptionParser& oparser) {
|
||||
void showUsage
|
||||
(const std::string& keyword,
|
||||
const SharedHandle<OptionParser>& oparser) {
|
||||
std::cout << _("Usage: aria2c [OPTIONS] [URI | MAGNET | TORRENT_FILE |"
|
||||
" METALINK_FILE]...") << "\n"
|
||||
<< "\n";
|
||||
if(util::startsWith(keyword, "#")) {
|
||||
std::vector<SharedHandle<OptionHandler> > handlers =
|
||||
keyword == TAG_ALL ? oparser.findAll():oparser.findByTag(keyword);
|
||||
keyword == TAG_ALL ? oparser->findAll() : oparser->findByTag(keyword);
|
||||
if(keyword == TAG_ALL) {
|
||||
std::cout << _("Printing all options.");
|
||||
} else {
|
||||
std::cout << fmt(_("Printing options tagged with '%s'."),
|
||||
keyword.c_str());
|
||||
std::cout << "\n";
|
||||
SharedHandle<OptionHandler> help = oparser.findByName("help");
|
||||
const SharedHandle<OptionHandler>& help = oparser->find(PREF_HELP);
|
||||
std::cout << fmt(_("See -h option to know other command-line"
|
||||
" options(%s)."),
|
||||
help->createPossibleValuesString().c_str());
|
||||
|
@ -106,7 +108,7 @@ void showUsage(const std::string& keyword, const OptionParser& oparser) {
|
|||
}
|
||||
} else {
|
||||
std::vector<SharedHandle<OptionHandler> > handlers =
|
||||
oparser.findByNameSubstring(keyword);
|
||||
oparser->findByNameSubstring(keyword);
|
||||
if(!handlers.empty()) {
|
||||
std::cout << fmt(_("Printing options whose name includes '%s'."),
|
||||
keyword.c_str())
|
||||
|
@ -119,7 +121,7 @@ void showUsage(const std::string& keyword, const OptionParser& oparser) {
|
|||
} else {
|
||||
std::cout << fmt(_("No option matching with '%s'."),
|
||||
keyword.c_str())
|
||||
<< "\n" << *oparser.findByName("help") << "\n";
|
||||
<< "\n" << *oparser->find(PREF_HELP) << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,7 +11,6 @@ namespace aria2 {
|
|||
class OptionHandlerTest:public CppUnit::TestFixture {
|
||||
|
||||
CPPUNIT_TEST_SUITE(OptionHandlerTest);
|
||||
CPPUNIT_TEST(testNullOptionHandler);
|
||||
CPPUNIT_TEST(testBooleanOptionHandler);
|
||||
CPPUNIT_TEST(testNumberOptionHandler);
|
||||
CPPUNIT_TEST(testNumberOptionHandler_min);
|
||||
|
@ -33,7 +32,6 @@ class OptionHandlerTest:public CppUnit::TestFixture {
|
|||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
public:
|
||||
void testNullOptionHandler();
|
||||
void testBooleanOptionHandler();
|
||||
void testNumberOptionHandler();
|
||||
void testNumberOptionHandler_min();
|
||||
|
@ -57,15 +55,6 @@ public:
|
|||
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION( OptionHandlerTest );
|
||||
|
||||
void OptionHandlerTest::testNullOptionHandler()
|
||||
{
|
||||
NullOptionHandler handler;
|
||||
CPPUNIT_ASSERT(handler.canHandle("foo"));
|
||||
Option option;
|
||||
handler.parse(option, "bar");
|
||||
CPPUNIT_ASSERT(!option.defined(PREF_TIMEOUT));
|
||||
}
|
||||
|
||||
void OptionHandlerTest::testBooleanOptionHandler()
|
||||
{
|
||||
BooleanOptionHandler handler(PREF_DAEMON);
|
||||
|
|
|
@ -20,9 +20,9 @@ class OptionParserTest:public CppUnit::TestFixture {
|
|||
CPPUNIT_TEST(testFindAll);
|
||||
CPPUNIT_TEST(testFindByNameSubstring);
|
||||
CPPUNIT_TEST(testFindByTag);
|
||||
CPPUNIT_TEST(testFindByName);
|
||||
CPPUNIT_TEST(testFind);
|
||||
CPPUNIT_TEST(testFindByShortName);
|
||||
CPPUNIT_TEST(testFindByID);
|
||||
CPPUNIT_TEST(testFindById);
|
||||
CPPUNIT_TEST(testParseDefaultValues);
|
||||
CPPUNIT_TEST(testParseArg);
|
||||
CPPUNIT_TEST(testParse);
|
||||
|
@ -66,9 +66,9 @@ public:
|
|||
void testFindAll();
|
||||
void testFindByNameSubstring();
|
||||
void testFindByTag();
|
||||
void testFindByName();
|
||||
void testFind();
|
||||
void testFindByShortName();
|
||||
void testFindByID();
|
||||
void testFindById();
|
||||
void testParseDefaultValues();
|
||||
void testParseArg();
|
||||
void testParse();
|
||||
|
@ -104,35 +104,36 @@ void OptionParserTest::testFindByTag()
|
|||
CPPUNIT_ASSERT_EQUAL(std::string("out"), res[1]->getName());
|
||||
}
|
||||
|
||||
void OptionParserTest::testFindByName()
|
||||
void OptionParserTest::testFind()
|
||||
{
|
||||
SharedHandle<OptionHandler> dir = oparser_->findByName("dir");
|
||||
const SharedHandle<OptionHandler>& dir = oparser_->find(PREF_DIR);
|
||||
CPPUNIT_ASSERT(dir);
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("dir"), dir->getName());
|
||||
|
||||
SharedHandle<OptionHandler> daemon = oparser_->findByName("daemon");
|
||||
const SharedHandle<OptionHandler>& daemon = oparser_->find(PREF_DAEMON);
|
||||
CPPUNIT_ASSERT(!daemon);
|
||||
|
||||
SharedHandle<OptionHandler> timeout2 = oparser_->findByName("timeout2");
|
||||
CPPUNIT_ASSERT(!timeout2);
|
||||
const SharedHandle<OptionHandler>& log = oparser_->find(PREF_LOG);
|
||||
CPPUNIT_ASSERT(!log);
|
||||
}
|
||||
|
||||
void OptionParserTest::testFindByShortName()
|
||||
{
|
||||
SharedHandle<OptionHandler> timeout = oparser_->findByShortName('A');
|
||||
const SharedHandle<OptionHandler>& timeout = oparser_->findByShortName('A');
|
||||
CPPUNIT_ASSERT(timeout);
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("timeout"), timeout->getName());
|
||||
|
||||
CPPUNIT_ASSERT(!oparser_->findByShortName('C'));
|
||||
}
|
||||
|
||||
void OptionParserTest::testFindByID()
|
||||
void OptionParserTest::testFindById()
|
||||
{
|
||||
SharedHandle<OptionHandler> timeout = oparser_->findByID(1);
|
||||
const SharedHandle<OptionHandler>& timeout =
|
||||
oparser_->findById(PREF_TIMEOUT->i);
|
||||
CPPUNIT_ASSERT(timeout);
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("timeout"), timeout->getName());
|
||||
|
||||
CPPUNIT_ASSERT(!oparser_->findByID(3));
|
||||
CPPUNIT_ASSERT(!oparser_->findById(9999));
|
||||
}
|
||||
|
||||
void OptionParserTest::testParseDefaultValues()
|
||||
|
|
Loading…
Reference in New Issue