OptionHandlerImpl.cc 23 KB

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