summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2013-05-02 10:03:00 +0900
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2013-05-02 10:03:00 +0900
commitaee621b3d40a270d827f5d3441dfccec66705ca9 (patch)
treec27eec43a940c2489dedb2aa550842873815e4fd
parente5cccd335cc99f28615d44d5cb0b4400239c63cd (diff)
Don't exit on error in option_processing
Still it exits when -v or -h is given. They are now guarded by standalone variable and only do so when it is true.
-rw-r--r--src/Context.cc18
-rw-r--r--src/aria2api.cc7
-rw-r--r--src/option_processing.cc63
3 files changed, 52 insertions, 36 deletions
diff --git a/src/Context.cc b/src/Context.cc
index 7543a28f..81022e24 100644
--- a/src/Context.cc
+++ b/src/Context.cc
@@ -175,17 +175,25 @@ void showFiles
} // namespace
#endif // ENABLE_BITTORRENT || ENABLE_METALINK
-extern void option_processing(Option& option, bool standalone,
- std::vector<std::string>& uris,
- int argc, char** argv, const KeyVals& options);
+extern error_code::Value option_processing(Option& option, bool standalone,
+ std::vector<std::string>& uris,
+ int argc, char** argv,
+ const KeyVals& options);
Context::Context(bool standalone,
int argc, char** argv, const KeyVals& options)
{
std::vector<std::string> args;
SharedHandle<Option> op(new Option());
- option_processing(*op.get(), standalone, args, argc, argv, options);
-
+ error_code::Value rv;
+ rv = option_processing(*op.get(), standalone, args, argc, argv, options);
+ if(rv != error_code::FINISHED) {
+ if(standalone) {
+ exit(rv);
+ } else {
+ throw DL_ABORT_EX("Option processing failed");
+ }
+ }
SimpleRandomizer::init();
#ifdef ENABLE_BITTORRENT
bittorrent::generateStaticPeerId(op->get(PREF_PEER_ID_PREFIX));
diff --git a/src/aria2api.cc b/src/aria2api.cc
index 02171b7a..d856d205 100644
--- a/src/aria2api.cc
+++ b/src/aria2api.cc
@@ -85,7 +85,12 @@ int libraryDeinit()
Session* sessionNew(const KeyVals& options)
{
int rv;
- Session* session = new Session(options);
+ Session* session;
+ try {
+ session = new Session(options);
+ } catch(RecoverableException& e) {
+ return 0;
+ }
if(session->context->reqinfo) {
rv = session->context->reqinfo->prepare();
if(rv != 0) {
diff --git a/src/option_processing.cc b/src/option_processing.cc
index c84aa6e1..3b2d01e0 100644
--- a/src/option_processing.cc
+++ b/src/option_processing.cc
@@ -190,9 +190,10 @@ void optionNativeToUtf8(Option& op)
} // namespace
#endif // __MINGW32__
-void option_processing(Option& op, bool standalone,
- std::vector<std::string>& uris,
- int argc, char** argv, const KeyVals& options)
+error_code::Value option_processing(Option& op, bool standalone,
+ std::vector<std::string>& uris,
+ int argc, char** argv,
+ const KeyVals& options)
{
const SharedHandle<OptionParser>& oparser = OptionParser::getInstance();
try {
@@ -206,27 +207,28 @@ void option_processing(Option& op, bool standalone,
oparser->parse(op, cmdstream);
noConf = op.getAsBool(PREF_NO_CONF);
ucfname = op.get(PREF_CONF_PATH);
-
- if(op.defined(PREF_VERSION)) {
- showVersion();
- exit(error_code::FINISHED);
- }
- if(op.defined(PREF_HELP)) {
- std::string keyword;
- if(op.get(PREF_HELP).empty()) {
- keyword = strHelpTag(TAG_BASIC);
- } else {
- keyword = op.get(PREF_HELP);
- if(util::startsWith(keyword, "--")) {
- keyword.erase(keyword.begin(), keyword.begin()+2);
- }
- std::string::size_type eqpos = keyword.find("=");
- if(eqpos != std::string::npos) {
- keyword.erase(keyword.begin()+eqpos, keyword.end());
+ if(standalone) {
+ if(op.defined(PREF_VERSION)) {
+ showVersion();
+ exit(error_code::FINISHED);
+ }
+ if(op.defined(PREF_HELP)) {
+ std::string keyword;
+ if(op.get(PREF_HELP).empty()) {
+ keyword = strHelpTag(TAG_BASIC);
+ } else {
+ keyword = op.get(PREF_HELP);
+ if(util::startsWith(keyword, "--")) {
+ keyword.erase(keyword.begin(), keyword.begin()+2);
+ }
+ std::string::size_type eqpos = keyword.find("=");
+ if(eqpos != std::string::npos) {
+ keyword.erase(keyword.begin()+eqpos, keyword.end());
+ }
}
+ showUsage(keyword, oparser, global::cout());
+ exit(error_code::FINISHED);
}
- showUsage(keyword, oparser, global::cout());
- exit(error_code::FINISHED);
}
}
SharedHandle<Option> confOption(new Option());
@@ -254,18 +256,18 @@ void option_processing(Option& op, bool standalone,
global::cerr()->printf(_("Usage:"));
global::cerr()->printf("\n%s\n", h->getDescription());
}
- exit(e.getErrorCode());
+ return e.getErrorCode();
} catch(Exception& e) {
global::cerr()->printf(_("Parse error in %s"), cfname.c_str());
global::cerr()->printf("\n%s", e.stackTrace().c_str());
- exit(e.getErrorCode());
+ return e.getErrorCode();
}
} else if(!ucfname.empty()) {
global::cerr()->printf(_("Configuration file %s is not found."),
cfname.c_str());
global::cerr()->printf("\n");
showUsage(strHelpTag(TAG_HELP), oparser, global::cerr());
- exit(error_code::UNKNOWN_ERROR);
+ return error_code::UNKNOWN_ERROR;
}
}
// Override configuration with environment variables.
@@ -298,15 +300,15 @@ void option_processing(Option& op, bool standalone,
global::cerr()->printf("\n");
write(global::cerr(), *h);
}
- exit(e.getErrorCode());
+ return e.getErrorCode();
} catch(UnknownOptionException& e) {
showUsage("", oparser, global::cerr());
showCandidates(e.getUnknownOption(), oparser);
- exit(e.getErrorCode());
+ return e.getErrorCode();
} catch(Exception& e) {
global::cerr()->printf("%s", e.stackTrace().c_str());
showUsage("", oparser, global::cerr());
- exit(e.getErrorCode());
+ return e.getErrorCode();
}
if(standalone &&
!op.getAsBool(PREF_ENABLE_RPC) &&
@@ -321,15 +323,16 @@ void option_processing(Option& op, bool standalone,
global::cerr()->printf(MSG_URI_REQUIRED);
global::cerr()->printf("\n");
showUsage("", oparser, global::cerr());
- exit(error_code::UNKNOWN_ERROR);
+ return error_code::UNKNOWN_ERROR;
}
}
if(op.getAsBool(PREF_DAEMON)) {
if(daemon(0, 0) < 0) {
perror(MSG_DAEMON_FAILED);
- exit(error_code::UNKNOWN_ERROR);
+ return error_code::UNKNOWN_ERROR;
}
}
+ return error_code::FINISHED;
}
} // namespace aria2