OptionHandlerImpl.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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 <cstring>
  39. #include <utility>
  40. #include <algorithm>
  41. #include <numeric>
  42. #include <sstream>
  43. #include <iterator>
  44. #include <vector>
  45. #include <stdexcept>
  46. #include "util.h"
  47. #include "DlAbortEx.h"
  48. #include "prefs.h"
  49. #include "Option.h"
  50. #include "fmt.h"
  51. #include "A2STR.h"
  52. #include "Request.h"
  53. #include "a2functional.h"
  54. #include "message.h"
  55. #include "File.h"
  56. #include "FileEntry.h"
  57. #include "a2io.h"
  58. #include "LogFactory.h"
  59. #include "uri.h"
  60. #include "SegList.h"
  61. #include "array_fun.h"
  62. #include "help_tags.h"
  63. #include "MessageDigest.h"
  64. namespace aria2 {
  65. BooleanOptionHandler::BooleanOptionHandler(PrefPtr pref,
  66. const char* description,
  67. const std::string& defaultValue,
  68. OptionHandler::ARG_TYPE argType,
  69. char shortName)
  70. : AbstractOptionHandler(pref, description, defaultValue, argType, shortName)
  71. {
  72. }
  73. BooleanOptionHandler::~BooleanOptionHandler() = default;
  74. void BooleanOptionHandler::parseArg(Option& option,
  75. const std::string& optarg) const
  76. {
  77. if (optarg == "true" || ((argType_ == OptionHandler::OPT_ARG ||
  78. argType_ == OptionHandler::NO_ARG) &&
  79. optarg.empty())) {
  80. option.put(pref_, A2_V_TRUE);
  81. }
  82. else if (optarg == "false") {
  83. option.put(pref_, A2_V_FALSE);
  84. }
  85. else {
  86. std::string msg = pref_->k;
  87. msg += " ";
  88. msg += _("must be either 'true' or 'false'.");
  89. throw DL_ABORT_EX(msg);
  90. }
  91. }
  92. std::string BooleanOptionHandler::createPossibleValuesString() const
  93. {
  94. return "true, false";
  95. }
  96. IntegerRangeOptionHandler::IntegerRangeOptionHandler(
  97. PrefPtr pref, const char* description, const std::string& defaultValue,
  98. int32_t min, int32_t max, char shortName)
  99. : AbstractOptionHandler(pref, description, defaultValue,
  100. OptionHandler::REQ_ARG, shortName),
  101. min_(min),
  102. max_(max)
  103. {
  104. }
  105. IntegerRangeOptionHandler::~IntegerRangeOptionHandler() = default;
  106. void IntegerRangeOptionHandler::parseArg(Option& option,
  107. const std::string& optarg) const
  108. {
  109. auto sgl = util::parseIntSegments(optarg);
  110. sgl.normalize();
  111. while (sgl.hasNext()) {
  112. int v = sgl.next();
  113. if (v < min_ || max_ < v) {
  114. std::string msg = pref_->k;
  115. msg += " ";
  116. msg += _("must be between %d and %d.");
  117. throw DL_ABORT_EX(fmt(msg.c_str(), min_, max_));
  118. }
  119. option.put(pref_, optarg);
  120. }
  121. }
  122. std::string IntegerRangeOptionHandler::createPossibleValuesString() const
  123. {
  124. return fmt("%d-%d", min_, max_);
  125. }
  126. NumberOptionHandler::NumberOptionHandler(PrefPtr pref, const char* description,
  127. const std::string& defaultValue,
  128. int64_t min, int64_t max,
  129. char shortName)
  130. : AbstractOptionHandler(pref, description, defaultValue,
  131. OptionHandler::REQ_ARG, shortName),
  132. min_(min),
  133. max_(max)
  134. {
  135. }
  136. NumberOptionHandler::~NumberOptionHandler() = default;
  137. void NumberOptionHandler::parseArg(Option& option,
  138. const std::string& optarg) const
  139. {
  140. int64_t number;
  141. if (util::parseLLIntNoThrow(number, optarg)) {
  142. parseArg(option, number);
  143. }
  144. else {
  145. throw DL_ABORT_EX(fmt("Bad number %s", optarg.c_str()));
  146. }
  147. }
  148. void NumberOptionHandler::parseArg(Option& option, int64_t number) const
  149. {
  150. if ((min_ == -1 || min_ <= number) && (max_ == -1 || number <= max_)) {
  151. option.put(pref_, util::itos(number));
  152. return;
  153. }
  154. std::string msg = pref_->k;
  155. msg += " ";
  156. if (min_ == -1 && max_ != -1) {
  157. msg += fmt(_("must be smaller than or equal to %" PRId64 "."), max_);
  158. }
  159. else if (min_ != -1 && max_ != -1) {
  160. msg += fmt(_("must be between %" PRId64 " and %" PRId64 "."), min_, max_);
  161. }
  162. else if (min_ != -1 && max_ == -1) {
  163. msg += fmt(_("must be greater than or equal to %" PRId64 "."), min_);
  164. }
  165. else {
  166. msg += _("must be a number.");
  167. }
  168. throw DL_ABORT_EX(msg);
  169. }
  170. std::string NumberOptionHandler::createPossibleValuesString() const
  171. {
  172. std::string values;
  173. if (min_ == -1) {
  174. values += "*";
  175. }
  176. else {
  177. values += util::itos(min_);
  178. }
  179. values += "-";
  180. if (max_ == -1) {
  181. values += "*";
  182. }
  183. else {
  184. values += util::itos(max_);
  185. }
  186. return values;
  187. }
  188. UnitNumberOptionHandler::UnitNumberOptionHandler(
  189. PrefPtr pref, const char* description, const std::string& defaultValue,
  190. int64_t min, int64_t max, char shortName)
  191. : NumberOptionHandler(pref, description, defaultValue, min, max, shortName)
  192. {
  193. }
  194. UnitNumberOptionHandler::~UnitNumberOptionHandler() = default;
  195. void UnitNumberOptionHandler::parseArg(Option& option,
  196. const std::string& optarg) const
  197. {
  198. int64_t num = util::getRealSize(optarg);
  199. NumberOptionHandler::parseArg(option, num);
  200. }
  201. FloatNumberOptionHandler::FloatNumberOptionHandler(
  202. PrefPtr pref, const char* description, const std::string& defaultValue,
  203. double min, double max, char shortName)
  204. : AbstractOptionHandler(pref, description, defaultValue,
  205. OptionHandler::REQ_ARG, shortName),
  206. min_(min),
  207. max_(max)
  208. {
  209. }
  210. FloatNumberOptionHandler::~FloatNumberOptionHandler() = default;
  211. void FloatNumberOptionHandler::parseArg(Option& option,
  212. const std::string& optarg) const
  213. {
  214. double number = strtod(optarg.c_str(), nullptr);
  215. if ((min_ < 0 || min_ <= number) && (max_ < 0 || number <= max_)) {
  216. option.put(pref_, optarg);
  217. }
  218. else {
  219. std::string msg = pref_->k;
  220. msg += " ";
  221. if (min_ < 0 && max_ >= 0) {
  222. msg += fmt(_("must be smaller than or equal to %.1f."), max_);
  223. }
  224. else if (min_ >= 0 && max_ >= 0) {
  225. msg += fmt(_("must be between %.1f and %.1f."), min_, max_);
  226. }
  227. else if (min_ >= 0 && max_ < 0) {
  228. msg += fmt(_("must be greater than or equal to %.1f."), min_);
  229. }
  230. else {
  231. msg += _("must be a number.");
  232. }
  233. throw DL_ABORT_EX(msg);
  234. }
  235. }
  236. std::string FloatNumberOptionHandler::createPossibleValuesString() const
  237. {
  238. std::string valuesString;
  239. if (min_ < 0) {
  240. valuesString += "*";
  241. }
  242. else {
  243. valuesString += fmt("%.1f", min_);
  244. }
  245. valuesString += "-";
  246. if (max_ < 0) {
  247. valuesString += "*";
  248. }
  249. else {
  250. valuesString += fmt("%.1f", max_);
  251. }
  252. return valuesString;
  253. }
  254. DefaultOptionHandler::DefaultOptionHandler(
  255. PrefPtr pref, const char* description, const std::string& defaultValue,
  256. const std::string& possibleValuesString, OptionHandler::ARG_TYPE argType,
  257. char shortName)
  258. : AbstractOptionHandler(pref, description, defaultValue, argType,
  259. shortName),
  260. possibleValuesString_(possibleValuesString),
  261. allowEmpty_(true)
  262. {
  263. }
  264. DefaultOptionHandler::~DefaultOptionHandler() = default;
  265. void DefaultOptionHandler::parseArg(Option& option,
  266. const std::string& optarg) const
  267. {
  268. if (!allowEmpty_ && optarg.empty()) {
  269. throw DL_ABORT_EX("Empty string is not allowed");
  270. }
  271. option.put(pref_, optarg);
  272. }
  273. std::string DefaultOptionHandler::createPossibleValuesString() const
  274. {
  275. return possibleValuesString_;
  276. }
  277. void DefaultOptionHandler::setAllowEmpty(bool allow) { allowEmpty_ = allow; }
  278. CumulativeOptionHandler::CumulativeOptionHandler(
  279. PrefPtr pref, const char* description, const std::string& defaultValue,
  280. const std::string& delim, const std::string& possibleValuesString,
  281. OptionHandler::ARG_TYPE argType, char shortName)
  282. : AbstractOptionHandler(pref, description, defaultValue, argType,
  283. shortName),
  284. delim_(delim),
  285. possibleValuesString_(possibleValuesString)
  286. {
  287. }
  288. CumulativeOptionHandler::~CumulativeOptionHandler() = default;
  289. void CumulativeOptionHandler::parseArg(Option& option,
  290. const std::string& optarg) const
  291. {
  292. std::string value = option.get(pref_);
  293. value += optarg;
  294. value += delim_;
  295. option.put(pref_, value);
  296. }
  297. std::string CumulativeOptionHandler::createPossibleValuesString() const
  298. {
  299. return possibleValuesString_;
  300. }
  301. IndexOutOptionHandler::IndexOutOptionHandler(PrefPtr pref,
  302. const char* description,
  303. char shortName)
  304. : AbstractOptionHandler(pref, description, NO_DEFAULT_VALUE,
  305. OptionHandler::REQ_ARG, shortName)
  306. {
  307. }
  308. IndexOutOptionHandler::~IndexOutOptionHandler() = default;
  309. void IndexOutOptionHandler::parseArg(Option& option,
  310. const std::string& optarg) const
  311. {
  312. // See optarg is in the format of "INDEX=PATH"
  313. util::parseIndexPath(optarg);
  314. std::string value = option.get(pref_);
  315. value += optarg;
  316. value += "\n";
  317. option.put(pref_, value);
  318. }
  319. std::string IndexOutOptionHandler::createPossibleValuesString() const
  320. {
  321. return "INDEX=PATH";
  322. }
  323. ChecksumOptionHandler::ChecksumOptionHandler(PrefPtr pref,
  324. const char* description,
  325. char shortName)
  326. : AbstractOptionHandler(pref, description, NO_DEFAULT_VALUE,
  327. OptionHandler::REQ_ARG, shortName)
  328. {
  329. }
  330. ChecksumOptionHandler::ChecksumOptionHandler(
  331. PrefPtr pref, const char* description,
  332. std::vector<std::string> acceptableTypes, char shortName)
  333. : AbstractOptionHandler(pref, description, NO_DEFAULT_VALUE,
  334. OptionHandler::REQ_ARG, shortName),
  335. acceptableTypes_(std::move(acceptableTypes))
  336. {
  337. }
  338. ChecksumOptionHandler::~ChecksumOptionHandler() = default;
  339. void ChecksumOptionHandler::parseArg(Option& option,
  340. const std::string& optarg) const
  341. {
  342. auto p = util::divide(std::begin(optarg), std::end(optarg), '=');
  343. std::string hashType(p.first.first, p.first.second);
  344. if (!acceptableTypes_.empty() &&
  345. std::find(std::begin(acceptableTypes_), std::end(acceptableTypes_),
  346. hashType) == std::end(acceptableTypes_)) {
  347. throw DL_ABORT_EX(
  348. fmt("Checksum type %s is not acceptable", hashType.c_str()));
  349. }
  350. std::string hexDigest(p.second.first, p.second.second);
  351. util::lowercase(hashType);
  352. util::lowercase(hexDigest);
  353. if (!MessageDigest::isValidHash(hashType, hexDigest)) {
  354. throw DL_ABORT_EX(_("Unrecognized checksum"));
  355. }
  356. option.put(pref_, optarg);
  357. }
  358. std::string ChecksumOptionHandler::createPossibleValuesString() const
  359. {
  360. return "HASH_TYPE=HEX_DIGEST";
  361. }
  362. ParameterOptionHandler::ParameterOptionHandler(
  363. PrefPtr pref, const char* description, const std::string& defaultValue,
  364. std::vector<std::string> validParamValues, char shortName)
  365. : AbstractOptionHandler(pref, description, defaultValue,
  366. OptionHandler::REQ_ARG, shortName),
  367. validParamValues_(std::move(validParamValues))
  368. {
  369. }
  370. ParameterOptionHandler::~ParameterOptionHandler() = default;
  371. void ParameterOptionHandler::parseArg(Option& option,
  372. const std::string& optarg) const
  373. {
  374. auto itr =
  375. std::find(validParamValues_.begin(), validParamValues_.end(), optarg);
  376. if (itr == validParamValues_.end()) {
  377. std::string msg = pref_->k;
  378. msg += " ";
  379. msg += _("must be one of the following:");
  380. if (validParamValues_.size() == 0) {
  381. msg += "''";
  382. }
  383. else {
  384. for (const auto& p : validParamValues_) {
  385. msg += "'";
  386. msg += p;
  387. msg += "' ";
  388. }
  389. }
  390. throw DL_ABORT_EX(msg);
  391. }
  392. else {
  393. option.put(pref_, optarg);
  394. }
  395. }
  396. std::string ParameterOptionHandler::createPossibleValuesString() const
  397. {
  398. std::stringstream s;
  399. std::copy(validParamValues_.begin(), validParamValues_.end(),
  400. std::ostream_iterator<std::string>(s, ", "));
  401. return util::strip(s.str(), ", ");
  402. }
  403. HostPortOptionHandler::HostPortOptionHandler(
  404. PrefPtr pref, const char* description, const std::string& defaultValue,
  405. PrefPtr hostOptionName, PrefPtr portOptionName, char shortName)
  406. : AbstractOptionHandler(pref, description, defaultValue,
  407. OptionHandler::REQ_ARG, shortName),
  408. hostOptionName_(hostOptionName),
  409. portOptionName_(portOptionName)
  410. {
  411. }
  412. HostPortOptionHandler::~HostPortOptionHandler() = default;
  413. void HostPortOptionHandler::parseArg(Option& option,
  414. const std::string& optarg) const
  415. {
  416. std::string uri = "http://";
  417. uri += optarg;
  418. Request req;
  419. if (!req.setUri(uri)) {
  420. throw DL_ABORT_EX(_("Unrecognized format"));
  421. }
  422. option.put(pref_, optarg);
  423. setHostAndPort(option, req.getHost(), req.getPort());
  424. }
  425. void HostPortOptionHandler::setHostAndPort(Option& option,
  426. const std::string& hostname,
  427. uint16_t port) const
  428. {
  429. option.put(hostOptionName_, hostname);
  430. option.put(portOptionName_, util::uitos(port));
  431. }
  432. std::string HostPortOptionHandler::createPossibleValuesString() const
  433. {
  434. return "HOST:PORT";
  435. }
  436. HttpProxyOptionHandler::HttpProxyOptionHandler(PrefPtr pref,
  437. const char* description,
  438. const std::string& defaultValue,
  439. char shortName)
  440. : AbstractOptionHandler(pref, description, defaultValue,
  441. OptionHandler::REQ_ARG, shortName),
  442. proxyUserPref_(option::k2p(std::string(pref->k) + "-user")),
  443. proxyPasswdPref_(option::k2p(std::string(pref->k) + "-passwd"))
  444. {
  445. }
  446. HttpProxyOptionHandler::~HttpProxyOptionHandler() = default;
  447. void HttpProxyOptionHandler::parseArg(Option& option,
  448. const std::string& optarg) const
  449. {
  450. if (optarg.empty()) {
  451. option.put(pref_, optarg);
  452. }
  453. else {
  454. std::string uri;
  455. if (util::startsWith(optarg, "http://") ||
  456. util::startsWith(optarg, "https://") ||
  457. util::startsWith(optarg, "ftp://")) {
  458. uri = optarg;
  459. }
  460. else {
  461. uri = "http://";
  462. uri += optarg;
  463. }
  464. uri::UriStruct us;
  465. if (!uri::parse(us, uri)) {
  466. throw DL_ABORT_EX(_("unrecognized proxy format"));
  467. }
  468. us.protocol = "http";
  469. option.put(pref_, uri::construct(us));
  470. }
  471. }
  472. std::string HttpProxyOptionHandler::createPossibleValuesString() const
  473. {
  474. return "[http://][USER:PASSWORD@]HOST[:PORT]";
  475. }
  476. LocalFilePathOptionHandler::LocalFilePathOptionHandler(
  477. PrefPtr pref, const char* description, const std::string& defaultValue,
  478. bool acceptStdin, char shortName, bool mustExist,
  479. const std::string& possibleValuesString)
  480. : AbstractOptionHandler(pref, description, defaultValue,
  481. OptionHandler::REQ_ARG, shortName),
  482. possibleValuesString_(possibleValuesString),
  483. acceptStdin_(acceptStdin),
  484. mustExist_(mustExist)
  485. {
  486. }
  487. void LocalFilePathOptionHandler::parseArg(Option& option,
  488. const std::string& optarg) const
  489. {
  490. if (acceptStdin_ && optarg == "-") {
  491. option.put(pref_, DEV_STDIN);
  492. }
  493. else {
  494. auto path = util::replace(optarg, "${HOME}", util::getHomeDir());
  495. if (mustExist_) {
  496. File f(path);
  497. if (!f.exists() || f.isDir()) {
  498. throw DL_ABORT_EX(fmt(MSG_NOT_FILE, optarg.c_str()));
  499. }
  500. }
  501. option.put(pref_, path);
  502. }
  503. }
  504. std::string LocalFilePathOptionHandler::createPossibleValuesString() const
  505. {
  506. if (!possibleValuesString_.empty()) {
  507. return possibleValuesString_;
  508. }
  509. if (acceptStdin_) {
  510. return PATH_TO_FILE_STDIN;
  511. }
  512. else {
  513. return PATH_TO_FILE;
  514. }
  515. }
  516. PrioritizePieceOptionHandler::PrioritizePieceOptionHandler(
  517. PrefPtr pref, const char* description, const std::string& defaultValue,
  518. char shortName)
  519. : AbstractOptionHandler(pref, description, defaultValue,
  520. OptionHandler::REQ_ARG, shortName)
  521. {
  522. }
  523. void PrioritizePieceOptionHandler::parseArg(Option& option,
  524. const std::string& optarg) const
  525. {
  526. // Parse optarg against empty FileEntry list to detect syntax
  527. // error.
  528. std::vector<size_t> result;
  529. util::parsePrioritizePieceRange(
  530. result, optarg, std::vector<std::shared_ptr<FileEntry>>(), 1024);
  531. option.put(pref_, optarg);
  532. }
  533. std::string PrioritizePieceOptionHandler::createPossibleValuesString() const
  534. {
  535. return "head[=SIZE], tail[=SIZE]";
  536. }
  537. OptimizeConcurrentDownloadsOptionHandler::
  538. OptimizeConcurrentDownloadsOptionHandler(PrefPtr pref,
  539. const char* description,
  540. const std::string& defaultValue,
  541. char shortName)
  542. : AbstractOptionHandler(pref, description, defaultValue,
  543. OptionHandler::OPT_ARG, shortName)
  544. {
  545. }
  546. void OptimizeConcurrentDownloadsOptionHandler::parseArg(
  547. Option& option, const std::string& optarg) const
  548. {
  549. if (optarg == "true" || optarg.empty()) {
  550. option.put(pref_, A2_V_TRUE);
  551. }
  552. else if (optarg == "false") {
  553. option.put(pref_, A2_V_FALSE);
  554. }
  555. else {
  556. auto p = util::divide(std::begin(optarg), std::end(optarg), ':');
  557. std::string coeff_b(p.second.first, p.second.second);
  558. if (coeff_b.empty()) {
  559. std::string msg = pref_->k;
  560. msg += " ";
  561. msg += _("must be either 'true', 'false' or a pair numeric coefficients "
  562. "A and B under the form 'A:B'.");
  563. throw DL_ABORT_EX(msg);
  564. }
  565. std::string coeff_a(p.first.first, p.first.second);
  566. PrefPtr pref = PREF_OPTIMIZE_CONCURRENT_DOWNLOADS_COEFFA;
  567. std::string* sptr = &coeff_a;
  568. for (;;) {
  569. char* end;
  570. errno = 0;
  571. strtod(sptr->c_str(), &end);
  572. if (errno != 0 || sptr->c_str() + sptr->size() != end) {
  573. throw DL_ABORT_EX(fmt("Bad number '%s'", sptr->c_str()));
  574. }
  575. option.put(pref, *sptr);
  576. if (pref == PREF_OPTIMIZE_CONCURRENT_DOWNLOADS_COEFFB) {
  577. break;
  578. }
  579. pref = PREF_OPTIMIZE_CONCURRENT_DOWNLOADS_COEFFB;
  580. sptr = &coeff_b;
  581. }
  582. option.put(pref_, A2_V_TRUE);
  583. }
  584. }
  585. std::string
  586. OptimizeConcurrentDownloadsOptionHandler::createPossibleValuesString() const
  587. {
  588. return "true, false, A:B";
  589. }
  590. DeprecatedOptionHandler::DeprecatedOptionHandler(
  591. OptionHandler* depOptHandler, const OptionHandler* repOptHandler,
  592. bool stillWork, std::string additionalMessage)
  593. : depOptHandler_(depOptHandler),
  594. repOptHandler_(repOptHandler),
  595. stillWork_(stillWork),
  596. additionalMessage_(std::move(additionalMessage))
  597. {
  598. depOptHandler_->addTag(TAG_DEPRECATED);
  599. }
  600. DeprecatedOptionHandler::~DeprecatedOptionHandler()
  601. {
  602. delete depOptHandler_;
  603. // We don't delete repOptHandler_.
  604. }
  605. void DeprecatedOptionHandler::parse(Option& option,
  606. const std::string& arg) const
  607. {
  608. if (repOptHandler_) {
  609. A2_LOG_WARN(fmt(_("--%s option is deprecated. Use --%s option instead. %s"),
  610. depOptHandler_->getName(), repOptHandler_->getName(),
  611. additionalMessage_.c_str()));
  612. repOptHandler_->parse(option, arg);
  613. }
  614. else if (stillWork_) {
  615. A2_LOG_WARN(fmt(_("--%s option will be deprecated in the future release. "
  616. "%s"),
  617. depOptHandler_->getName(), additionalMessage_.c_str()));
  618. depOptHandler_->parse(option, arg);
  619. }
  620. else {
  621. A2_LOG_WARN(fmt(_("--%s option is deprecated. %s"),
  622. depOptHandler_->getName(), additionalMessage_.c_str()));
  623. }
  624. }
  625. std::string DeprecatedOptionHandler::createPossibleValuesString() const
  626. {
  627. return depOptHandler_->createPossibleValuesString();
  628. }
  629. bool DeprecatedOptionHandler::hasTag(uint32_t tag) const
  630. {
  631. return depOptHandler_->hasTag(tag);
  632. }
  633. void DeprecatedOptionHandler::addTag(uint32_t tag)
  634. {
  635. depOptHandler_->addTag(tag);
  636. }
  637. std::string DeprecatedOptionHandler::toTagString() const
  638. {
  639. return depOptHandler_->toTagString();
  640. }
  641. const char* DeprecatedOptionHandler::getName() const
  642. {
  643. return depOptHandler_->getName();
  644. }
  645. const char* DeprecatedOptionHandler::getDescription() const
  646. {
  647. return depOptHandler_->getDescription();
  648. }
  649. const std::string& DeprecatedOptionHandler::getDefaultValue() const
  650. {
  651. return depOptHandler_->getDefaultValue();
  652. }
  653. bool DeprecatedOptionHandler::isHidden() const
  654. {
  655. return depOptHandler_->isHidden();
  656. }
  657. void DeprecatedOptionHandler::hide() { depOptHandler_->hide(); }
  658. PrefPtr DeprecatedOptionHandler::getPref() const
  659. {
  660. return depOptHandler_->getPref();
  661. }
  662. OptionHandler::ARG_TYPE DeprecatedOptionHandler::getArgType() const
  663. {
  664. return depOptHandler_->getArgType();
  665. }
  666. char DeprecatedOptionHandler::getShortName() const
  667. {
  668. return depOptHandler_->getShortName();
  669. }
  670. bool DeprecatedOptionHandler::getEraseAfterParse() const
  671. {
  672. return depOptHandler_->getEraseAfterParse();
  673. }
  674. void DeprecatedOptionHandler::setEraseAfterParse(bool eraseAfterParse)
  675. {
  676. depOptHandler_->setEraseAfterParse(eraseAfterParse);
  677. }
  678. bool DeprecatedOptionHandler::getInitialOption() const
  679. {
  680. return depOptHandler_->getInitialOption();
  681. }
  682. void DeprecatedOptionHandler::setInitialOption(bool f)
  683. {
  684. depOptHandler_->setInitialOption(f);
  685. }
  686. bool DeprecatedOptionHandler::getChangeOption() const
  687. {
  688. return depOptHandler_->getChangeOption();
  689. }
  690. void DeprecatedOptionHandler::setChangeOption(bool f)
  691. {
  692. depOptHandler_->setChangeOption(f);
  693. }
  694. bool DeprecatedOptionHandler::getChangeOptionForReserved() const
  695. {
  696. return depOptHandler_->getChangeOptionForReserved();
  697. }
  698. void DeprecatedOptionHandler::setChangeOptionForReserved(bool f)
  699. {
  700. depOptHandler_->setChangeOptionForReserved(f);
  701. }
  702. bool DeprecatedOptionHandler::getChangeGlobalOption() const
  703. {
  704. return depOptHandler_->getChangeGlobalOption();
  705. }
  706. void DeprecatedOptionHandler::setChangeGlobalOption(bool f)
  707. {
  708. depOptHandler_->setChangeGlobalOption(f);
  709. }
  710. bool DeprecatedOptionHandler::getCumulative() const
  711. {
  712. return depOptHandler_->getCumulative();
  713. }
  714. void DeprecatedOptionHandler::setCumulative(bool f)
  715. {
  716. depOptHandler_->setCumulative(f);
  717. }
  718. } // namespace aria2