OptionHandlerImpl.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "OptionHandlerImpl.h"
  36. #include <cassert>
  37. #include <cstdio>
  38. #include <utility>
  39. #include <algorithm>
  40. #include <numeric>
  41. #include <sstream>
  42. #include <iterator>
  43. #include <vector>
  44. #include "util.h"
  45. #include "DlAbortEx.h"
  46. #include "prefs.h"
  47. #include "Option.h"
  48. #include "fmt.h"
  49. #include "A2STR.h"
  50. #include "Request.h"
  51. #include "a2functional.h"
  52. #include "message.h"
  53. #include "File.h"
  54. #include "FileEntry.h"
  55. #include "a2io.h"
  56. namespace aria2 {
  57. NullOptionHandler::~NullOptionHandler() {}
  58. bool NullOptionHandler::canHandle(const std::string& optName) { return true; }
  59. void NullOptionHandler::parse(Option& option, const std::string& arg) {}
  60. bool NullOptionHandler::hasTag(const std::string& tag) const { return false; }
  61. void NullOptionHandler::addTag(const std::string& tag) {}
  62. std::string NullOptionHandler::toTagString() const { return A2STR::NIL; }
  63. const std::string& NullOptionHandler::getName() const { return A2STR::NIL; }
  64. const std::string& NullOptionHandler::getDescription() const
  65. {
  66. return A2STR::NIL;
  67. }
  68. const std::string& NullOptionHandler::getDefaultValue() const
  69. {
  70. return A2STR::NIL;
  71. }
  72. std::string NullOptionHandler::createPossibleValuesString() const
  73. {
  74. return A2STR::NIL;
  75. }
  76. bool NullOptionHandler::isHidden() const
  77. {
  78. return true;
  79. }
  80. void NullOptionHandler::hide() {}
  81. OptionHandler::ARG_TYPE NullOptionHandler::getArgType() const
  82. {
  83. return OptionHandler::NO_ARG;
  84. }
  85. int NullOptionHandler::getOptionID() const
  86. {
  87. return id_;
  88. }
  89. void NullOptionHandler::setOptionID(int id)
  90. {
  91. id_ = id;
  92. }
  93. char NullOptionHandler::getShortName() const
  94. {
  95. return 0;
  96. }
  97. BooleanOptionHandler::BooleanOptionHandler
  98. (const std::string& optName,
  99. const std::string& description,
  100. const std::string& defaultValue,
  101. OptionHandler::ARG_TYPE argType,
  102. char shortName)
  103. : NameMatchOptionHandler(optName, description, defaultValue,
  104. argType, shortName)
  105. {}
  106. BooleanOptionHandler::~BooleanOptionHandler() {}
  107. void BooleanOptionHandler::parseArg(Option& option, const std::string& optarg)
  108. {
  109. if(optarg == "true" ||
  110. ((argType_ == OptionHandler::OPT_ARG ||
  111. argType_ == OptionHandler::NO_ARG)
  112. && optarg.empty())) {
  113. option.put(optName_, A2_V_TRUE);
  114. } else if(optarg == "false") {
  115. option.put(optName_, A2_V_FALSE);
  116. } else {
  117. std::string msg = optName_;
  118. strappend(msg, " ", _("must be either 'true' or 'false'."));
  119. throw DL_ABORT_EX(msg);
  120. }
  121. }
  122. std::string BooleanOptionHandler::createPossibleValuesString() const
  123. {
  124. return "true, false";
  125. }
  126. IntegerRangeOptionHandler::IntegerRangeOptionHandler
  127. (const std::string& optName,
  128. const std::string& description,
  129. const std::string& defaultValue,
  130. int32_t min, int32_t max,
  131. char shortName)
  132. : NameMatchOptionHandler(optName, description, defaultValue,
  133. OptionHandler::REQ_ARG, shortName),
  134. min_(min),
  135. max_(max)
  136. {}
  137. IntegerRangeOptionHandler::~IntegerRangeOptionHandler() {}
  138. void IntegerRangeOptionHandler::parseArg
  139. (Option& option, const std::string& optarg)
  140. {
  141. IntSequence seq = util::parseIntRange(optarg);
  142. while(seq.hasNext()) {
  143. int32_t v = seq.next();
  144. if(v < min_ || max_ < v) {
  145. std::string msg = optName_;
  146. strappend(msg, " ", _("must be between %s and %s."));
  147. throw DL_ABORT_EX
  148. (fmt(msg.c_str(), util::itos(min_).c_str(),
  149. util::itos(max_).c_str()));
  150. }
  151. option.put(optName_, optarg);
  152. }
  153. }
  154. std::string IntegerRangeOptionHandler::createPossibleValuesString() const
  155. {
  156. return util::itos(min_)+"-"+util::itos(max_);
  157. }
  158. NumberOptionHandler::NumberOptionHandler
  159. (const std::string& optName,
  160. const std::string& description,
  161. const std::string& defaultValue,
  162. int64_t min,
  163. int64_t max,
  164. char shortName)
  165. : NameMatchOptionHandler(optName, description, defaultValue,
  166. OptionHandler::REQ_ARG, shortName),
  167. min_(min),
  168. max_(max)
  169. {}
  170. NumberOptionHandler::~NumberOptionHandler() {}
  171. void NumberOptionHandler::parseArg(Option& option, const std::string& optarg)
  172. {
  173. int64_t num = util::parseLLInt(optarg);
  174. parseArg(option, num);
  175. }
  176. void NumberOptionHandler::parseArg(Option& option, int64_t number)
  177. {
  178. if((min_ == -1 || min_ <= number) && (max_ == -1 || number <= max_)) {
  179. option.put(optName_, util::itos(number));
  180. } else {
  181. std::string msg = optName_;
  182. msg += " ";
  183. if(min_ == -1 && max_ != -1) {
  184. msg += fmt(_("must be smaller than or equal to %s."),
  185. util::itos(max_).c_str());
  186. } else if(min_ != -1 && max_ != -1) {
  187. msg += fmt(_("must be between %s and %s."),
  188. util::itos(min_).c_str(),
  189. util::itos(max_).c_str());
  190. } else if(min_ != -1 && max_ == -1) {
  191. msg += fmt(_("must be greater than or equal to %s."),
  192. util::itos(min_).c_str());
  193. } else {
  194. msg += _("must be a number.");
  195. }
  196. throw DL_ABORT_EX(msg);
  197. }
  198. }
  199. std::string NumberOptionHandler::createPossibleValuesString() const
  200. {
  201. std::string values;
  202. if(min_ == -1) {
  203. values += "*";
  204. } else {
  205. values += util::itos(min_);
  206. }
  207. values += "-";
  208. if(max_ == -1) {
  209. values += "*";
  210. } else {
  211. values += util::itos(max_);
  212. }
  213. return values;
  214. }
  215. UnitNumberOptionHandler::UnitNumberOptionHandler
  216. (const std::string& optName,
  217. const std::string& description,
  218. const std::string& defaultValue,
  219. int64_t min,
  220. int64_t max,
  221. char shortName)
  222. : NumberOptionHandler(optName, description, defaultValue, min, max,
  223. shortName)
  224. {}
  225. UnitNumberOptionHandler::~UnitNumberOptionHandler() {}
  226. void UnitNumberOptionHandler::parseArg
  227. (Option& option, const std::string& optarg)
  228. {
  229. int64_t num = util::getRealSize(optarg);
  230. NumberOptionHandler::parseArg(option, num);
  231. }
  232. FloatNumberOptionHandler::FloatNumberOptionHandler
  233. (const std::string& optName,
  234. const std::string& description,
  235. const std::string& defaultValue,
  236. double min,
  237. double max,
  238. char shortName)
  239. : NameMatchOptionHandler(optName, description, defaultValue,
  240. OptionHandler::REQ_ARG, shortName),
  241. min_(min),
  242. max_(max)
  243. {}
  244. FloatNumberOptionHandler::~FloatNumberOptionHandler() {}
  245. void FloatNumberOptionHandler::parseArg
  246. (Option& option, const std::string& optarg)
  247. {
  248. double number = strtod(optarg.c_str(), 0);
  249. if((min_ < 0 || min_ <= number) && (max_ < 0 || number <= max_)) {
  250. option.put(optName_, optarg);
  251. } else {
  252. std::string msg = optName_;
  253. msg += " ";
  254. if(min_ < 0 && max_ >= 0) {
  255. msg += fmt(_("must be smaller than or equal to %.1f."), max_);
  256. } else if(min_ >= 0 && max_ >= 0) {
  257. msg += fmt(_("must be between %.1f and %.1f."), min_, max_);
  258. } else if(min_ >= 0 && max_ < 0) {
  259. msg += fmt(_("must be greater than or equal to %.1f."), min_);
  260. } else {
  261. msg += _("must be a number.");
  262. }
  263. throw DL_ABORT_EX(msg);
  264. }
  265. }
  266. std::string FloatNumberOptionHandler::createPossibleValuesString() const
  267. {
  268. std::string valuesString;
  269. if(min_ < 0) {
  270. valuesString += "*";
  271. } else {
  272. char buf[11];
  273. snprintf(buf, sizeof(buf), "%.1f", min_);
  274. valuesString += buf;
  275. }
  276. valuesString += "-";
  277. if(max_ < 0) {
  278. valuesString += "*";
  279. } else {
  280. char buf[11];
  281. snprintf(buf, sizeof(buf), "%.1f", max_);
  282. valuesString += buf;
  283. }
  284. return valuesString;
  285. }
  286. DefaultOptionHandler::DefaultOptionHandler
  287. (const std::string& optName,
  288. const std::string& description,
  289. const std::string& defaultValue,
  290. const std::string& possibleValuesString,
  291. OptionHandler::ARG_TYPE argType,
  292. char shortName)
  293. : NameMatchOptionHandler(optName, description, defaultValue, argType,
  294. shortName),
  295. possibleValuesString_(possibleValuesString)
  296. {}
  297. DefaultOptionHandler::~DefaultOptionHandler() {}
  298. void DefaultOptionHandler::parseArg(Option& option, const std::string& optarg)
  299. {
  300. option.put(optName_, optarg);
  301. }
  302. std::string DefaultOptionHandler::createPossibleValuesString() const
  303. {
  304. return possibleValuesString_;
  305. }
  306. CumulativeOptionHandler::CumulativeOptionHandler
  307. (const std::string& optName,
  308. const std::string& description,
  309. const std::string& defaultValue,
  310. const std::string& delim,
  311. const std::string& possibleValuesString,
  312. OptionHandler::ARG_TYPE argType,
  313. char shortName)
  314. : NameMatchOptionHandler(optName, description, defaultValue, argType,
  315. shortName),
  316. delim_(delim),
  317. possibleValuesString_(possibleValuesString)
  318. {}
  319. CumulativeOptionHandler::~CumulativeOptionHandler() {}
  320. void CumulativeOptionHandler::parseArg
  321. (Option& option, const std::string& optarg)
  322. {
  323. std::string value = option.get(optName_);
  324. strappend(value, optarg, delim_);
  325. option.put(optName_, value);
  326. }
  327. std::string CumulativeOptionHandler::createPossibleValuesString() const
  328. {
  329. return possibleValuesString_;
  330. }
  331. IndexOutOptionHandler::IndexOutOptionHandler
  332. (const std::string& optName,
  333. const std::string& description,
  334. char shortName)
  335. : NameMatchOptionHandler(optName, description, NO_DEFAULT_VALUE,
  336. OptionHandler::REQ_ARG, shortName)
  337. {}
  338. IndexOutOptionHandler::~IndexOutOptionHandler() {}
  339. void IndexOutOptionHandler::parseArg(Option& option, const std::string& optarg)
  340. {
  341. // See optarg is in the fomrat of "INDEX=PATH"
  342. util::parseIndexPath(optarg);
  343. std::string value = option.get(optName_);
  344. strappend(value, optarg, "\n");
  345. option.put(optName_, value);
  346. }
  347. std::string IndexOutOptionHandler::createPossibleValuesString() const
  348. {
  349. return "INDEX=PATH";
  350. }
  351. ParameterOptionHandler::ParameterOptionHandler
  352. (const std::string& optName,
  353. const std::string& description,
  354. const std::string& defaultValue,
  355. const std::vector<std::string>& validParamValues,
  356. char shortName)
  357. : NameMatchOptionHandler(optName, description, defaultValue,
  358. OptionHandler::REQ_ARG, shortName),
  359. validParamValues_(validParamValues)
  360. {}
  361. ParameterOptionHandler::ParameterOptionHandler
  362. (const std::string& optName,
  363. const std::string& description,
  364. const std::string& defaultValue,
  365. const std::string& validParamValue,
  366. char shortName)
  367. : NameMatchOptionHandler(optName, description, defaultValue,
  368. OptionHandler::REQ_ARG, shortName)
  369. {
  370. validParamValues_.push_back(validParamValue);
  371. }
  372. ParameterOptionHandler::ParameterOptionHandler
  373. (const std::string& optName,
  374. const std::string& description,
  375. const std::string& defaultValue,
  376. const std::string& validParamValue1,
  377. const std::string& validParamValue2,
  378. char shortName)
  379. : NameMatchOptionHandler(optName, description, defaultValue,
  380. OptionHandler::REQ_ARG, shortName)
  381. {
  382. validParamValues_.push_back(validParamValue1);
  383. validParamValues_.push_back(validParamValue2);
  384. }
  385. ParameterOptionHandler::ParameterOptionHandler
  386. (const std::string& optName,
  387. const std::string& description,
  388. const std::string& defaultValue,
  389. const std::string& validParamValue1,
  390. const std::string& validParamValue2,
  391. const std::string& validParamValue3,
  392. char shortName)
  393. : NameMatchOptionHandler(optName, description, defaultValue,
  394. OptionHandler::REQ_ARG, shortName)
  395. {
  396. validParamValues_.push_back(validParamValue1);
  397. validParamValues_.push_back(validParamValue2);
  398. validParamValues_.push_back(validParamValue3);
  399. }
  400. ParameterOptionHandler::~ParameterOptionHandler() {}
  401. void ParameterOptionHandler::parseArg(Option& option, const std::string& optarg)
  402. {
  403. std::vector<std::string>::const_iterator itr =
  404. std::find(validParamValues_.begin(), validParamValues_.end(), optarg);
  405. if(itr == validParamValues_.end()) {
  406. std::string msg = optName_;
  407. strappend(msg, " ", _("must be one of the following:"));
  408. if(validParamValues_.size() == 0) {
  409. msg += "''";
  410. } else {
  411. for(std::vector<std::string>::const_iterator itr =
  412. validParamValues_.begin(), eoi = validParamValues_.end();
  413. itr != eoi; ++itr) {
  414. strappend(msg, "'", *itr, "' ");
  415. }
  416. }
  417. throw DL_ABORT_EX(msg);
  418. } else {
  419. option.put(optName_, optarg);
  420. }
  421. }
  422. std::string ParameterOptionHandler::createPossibleValuesString() const
  423. {
  424. std::stringstream s;
  425. std::copy(validParamValues_.begin(), validParamValues_.end(),
  426. std::ostream_iterator<std::string>(s, ", "));
  427. return util::strip(s.str(), ", ");
  428. }
  429. HostPortOptionHandler::HostPortOptionHandler
  430. (const std::string& optName,
  431. const std::string& description,
  432. const std::string& defaultValue,
  433. const std::string& hostOptionName,
  434. const std::string& portOptionName,
  435. char shortName)
  436. : NameMatchOptionHandler(optName, description, defaultValue,
  437. OptionHandler::REQ_ARG, shortName),
  438. hostOptionName_(hostOptionName),
  439. portOptionName_(portOptionName)
  440. {}
  441. HostPortOptionHandler::~HostPortOptionHandler() {}
  442. void HostPortOptionHandler::parseArg(Option& option, const std::string& optarg)
  443. {
  444. std::string uri = "http://";
  445. uri += optarg;
  446. Request req;
  447. if(!req.setUri(uri)) {
  448. throw DL_ABORT_EX(_("Unrecognized format"));
  449. }
  450. option.put(optName_, optarg);
  451. setHostAndPort(option, req.getHost(), req.getPort());
  452. }
  453. void HostPortOptionHandler::setHostAndPort
  454. (Option& option, const std::string& hostname, uint16_t port)
  455. {
  456. option.put(hostOptionName_, hostname);
  457. option.put(portOptionName_, util::uitos(port));
  458. }
  459. std::string HostPortOptionHandler::createPossibleValuesString() const
  460. {
  461. return "HOST:PORT";
  462. }
  463. HttpProxyUserOptionHandler::HttpProxyUserOptionHandler
  464. (const std::string& optName,
  465. const std::string& description,
  466. const std::string& defaultValue,
  467. char shortName)
  468. : NameMatchOptionHandler(optName, description, defaultValue,
  469. OptionHandler::REQ_ARG, shortName)
  470. {}
  471. void HttpProxyUserOptionHandler::parseArg
  472. (Option& option, const std::string& optarg)
  473. {
  474. if(util::endsWith(optName_, "-user")) {
  475. const std::string proxyPref = optName_.substr(0, optName_.size()-5);
  476. const std::string& olduri = option.get(proxyPref);
  477. if(!olduri.empty()) {
  478. Request req;
  479. bool b = req.setUri(olduri);
  480. assert(b);
  481. std::string uri = "http://";
  482. if(!optarg.empty()) {
  483. uri += util::percentEncode(optarg);
  484. }
  485. if(req.hasPassword()) {
  486. uri += A2STR::COLON_C;
  487. uri += util::percentEncode(req.getPassword());
  488. }
  489. if(uri.size() > 7) {
  490. uri += "@";
  491. }
  492. strappend(uri, req.getHost(),A2STR::COLON_C,util::uitos(req.getPort()));
  493. option.put(proxyPref, uri);
  494. }
  495. }
  496. option.put(optName_, optarg);
  497. }
  498. std::string HttpProxyUserOptionHandler::createPossibleValuesString() const
  499. {
  500. return "";
  501. }
  502. HttpProxyPasswdOptionHandler::HttpProxyPasswdOptionHandler
  503. (const std::string& optName,
  504. const std::string& description,
  505. const std::string& defaultValue,
  506. char shortName)
  507. : NameMatchOptionHandler(optName, description, defaultValue,
  508. OptionHandler::REQ_ARG, shortName)
  509. {}
  510. void HttpProxyPasswdOptionHandler::parseArg
  511. (Option& option, const std::string& optarg)
  512. {
  513. if(util::endsWith(optName_, "-passwd")) {
  514. const std::string proxyPref = optName_.substr(0, optName_.size()-7);
  515. const std::string& olduri = option.get(proxyPref);
  516. if(!olduri.empty()) {
  517. Request req;
  518. bool b = req.setUri(olduri);
  519. assert(b);
  520. std::string uri = "http://";
  521. if(!req.getUsername().empty()) {
  522. uri += util::percentEncode(req.getUsername());
  523. }
  524. uri += A2STR::COLON_C;
  525. if(!optarg.empty()) {
  526. uri += util::percentEncode(optarg);
  527. }
  528. if(uri.size() > 7) {
  529. uri += "@";
  530. }
  531. strappend(uri, req.getHost(), A2STR::COLON_C,util::itos(req.getPort()));
  532. option.put(proxyPref, uri);
  533. }
  534. }
  535. option.put(optName_, optarg);
  536. }
  537. std::string HttpProxyPasswdOptionHandler::createPossibleValuesString() const
  538. {
  539. return "";
  540. }
  541. HttpProxyOptionHandler::HttpProxyOptionHandler
  542. (const std::string& optName,
  543. const std::string& description,
  544. const std::string& defaultValue,
  545. char shortName)
  546. : NameMatchOptionHandler(optName, description, defaultValue,
  547. OptionHandler::REQ_ARG, shortName),
  548. proxyUserPref_(optName_+"-user"),
  549. proxyPasswdPref_(optName_+"-passwd")
  550. {}
  551. HttpProxyOptionHandler::~HttpProxyOptionHandler() {}
  552. void HttpProxyOptionHandler::parseArg(Option& option, const std::string& optarg)
  553. {
  554. if(optarg.empty()) {
  555. option.put(optName_, optarg);
  556. } else {
  557. Request req;
  558. std::string uri;
  559. if(util::startsWith(optarg, "http://")) {
  560. uri = optarg;
  561. } else {
  562. uri = "http://"+optarg;
  563. }
  564. if(!req.setUri(uri)) {
  565. throw DL_ABORT_EX(_("unrecognized proxy format"));
  566. }
  567. uri = "http://";
  568. if(req.getUsername().empty()) {
  569. if(option.defined(proxyUserPref_)) {
  570. uri += util::percentEncode(option.get(proxyUserPref_));
  571. }
  572. } else {
  573. uri += util::percentEncode(req.getUsername());
  574. }
  575. if(!req.hasPassword()) {
  576. if(option.defined(proxyPasswdPref_)) {
  577. uri += A2STR::COLON_C;
  578. uri += util::percentEncode(option.get(proxyPasswdPref_));
  579. }
  580. } else {
  581. uri += A2STR::COLON_C;
  582. uri += util::percentEncode(req.getPassword());
  583. }
  584. if(uri.size() > 7) {
  585. uri += "@";
  586. }
  587. strappend(uri, req.getHost(), A2STR::COLON_C, util::uitos(req.getPort()));
  588. option.put(optName_, uri);
  589. }
  590. }
  591. std::string HttpProxyOptionHandler::createPossibleValuesString() const
  592. {
  593. return "[http://][USER:PASSWORD@]HOST[:PORT]";
  594. }
  595. LocalFilePathOptionHandler::LocalFilePathOptionHandler
  596. (const std::string& optName,
  597. const std::string& description,
  598. const std::string& defaultValue,
  599. bool acceptStdin,
  600. char shortName)
  601. : NameMatchOptionHandler(optName, description, defaultValue,
  602. OptionHandler::REQ_ARG, shortName),
  603. acceptStdin_(acceptStdin)
  604. {}
  605. void LocalFilePathOptionHandler::parseArg
  606. (Option& option, const std::string& optarg)
  607. {
  608. if(acceptStdin_ && optarg == "-") {
  609. option.put(optName_, DEV_STDIN);
  610. } else {
  611. File f(optarg);
  612. if(!f.exists() || f.isDir()) {
  613. throw DL_ABORT_EX(fmt(MSG_NOT_FILE, optarg.c_str()));
  614. }
  615. option.put(optName_, optarg);
  616. }
  617. }
  618. std::string LocalFilePathOptionHandler::createPossibleValuesString() const
  619. {
  620. if(acceptStdin_) {
  621. return PATH_TO_FILE_STDIN;
  622. } else {
  623. return PATH_TO_FILE;
  624. }
  625. }
  626. PrioritizePieceOptionHandler::PrioritizePieceOptionHandler
  627. (const std::string& optName,
  628. const std::string& description,
  629. const std::string& defaultValue,
  630. char shortName)
  631. : NameMatchOptionHandler(optName, description, defaultValue,
  632. OptionHandler::REQ_ARG, shortName)
  633. {}
  634. void PrioritizePieceOptionHandler::parseArg
  635. (Option& option, const std::string& optarg)
  636. {
  637. // Parse optarg against empty FileEntry list to detect syntax
  638. // error.
  639. std::vector<size_t> result;
  640. util::parsePrioritizePieceRange
  641. (result, optarg, std::vector<SharedHandle<FileEntry> >(), 1024);
  642. option.put(optName_, optarg);
  643. }
  644. std::string PrioritizePieceOptionHandler::createPossibleValuesString() const
  645. {
  646. return "head[=SIZE], tail[=SIZE]";
  647. }
  648. } // namespace aria2