ArgsParser.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. /*
  2. * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * Copyright (c) 2022, Matthias Zimmerman <matthias291999@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "AK/JsonObject.h"
  8. #include <AK/Format.h>
  9. #include <AK/StringBuilder.h>
  10. #include <LibCore/ArgsParser.h>
  11. #include <LibCore/Version.h>
  12. #include <getopt.h>
  13. #include <limits.h>
  14. #include <math.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. static Optional<double> convert_to_double(const char* s)
  18. {
  19. char* p;
  20. double v = strtod(s, &p);
  21. if (isnan(v) || p == s)
  22. return {};
  23. return v;
  24. }
  25. namespace Core {
  26. ArgsParser::ArgsParser()
  27. {
  28. add_option(m_show_help, "Display help message and exit", "help", 0, true);
  29. add_option(m_show_version, "Print version", "version", 0, true);
  30. add_option(m_perform_autocomplete, "Perform autocompletion", "complete", 0, true);
  31. }
  32. bool ArgsParser::parse(int argc, char* const* argv, FailureBehavior failure_behavior)
  33. {
  34. auto fail = [this, argv, failure_behavior] {
  35. if (failure_behavior == FailureBehavior::PrintUsage || failure_behavior == FailureBehavior::PrintUsageAndExit)
  36. print_usage(stderr, argv[0]);
  37. if (failure_behavior == FailureBehavior::Exit || failure_behavior == FailureBehavior::PrintUsageAndExit)
  38. exit(1);
  39. };
  40. Vector<option> long_options;
  41. StringBuilder short_options_builder;
  42. if (m_stop_on_first_non_option)
  43. short_options_builder.append('+');
  44. int index_of_found_long_option = -1;
  45. // Tell getopt() to reset its internal state, and start scanning from optind = 1.
  46. // We could also set optreset = 1, but the host platform may not support that.
  47. optind = 0;
  48. for (size_t i = 0; i < m_options.size(); i++) {
  49. auto& opt = m_options[i];
  50. if (opt.long_name) {
  51. option long_opt {
  52. opt.long_name,
  53. opt.requires_argument ? required_argument : no_argument,
  54. &index_of_found_long_option,
  55. static_cast<int>(i)
  56. };
  57. long_options.append(long_opt);
  58. }
  59. if (opt.short_name) {
  60. short_options_builder.append(opt.short_name);
  61. if (opt.requires_argument)
  62. short_options_builder.append(':');
  63. }
  64. }
  65. long_options.append({ 0, 0, 0, 0 });
  66. String short_options = short_options_builder.build();
  67. while (true) {
  68. int c = getopt_long(argc, argv, short_options.characters(), long_options.data(), nullptr);
  69. if (c == -1) {
  70. // We have reached the end.
  71. break;
  72. } else if (c == '?') {
  73. // There was an error, and getopt() has already
  74. // printed its error message.
  75. fail();
  76. return false;
  77. }
  78. // Let's see what option we just found.
  79. Option* found_option = nullptr;
  80. if (c == 0) {
  81. // It was a long option.
  82. VERIFY(index_of_found_long_option >= 0);
  83. found_option = &m_options[index_of_found_long_option];
  84. index_of_found_long_option = -1;
  85. } else {
  86. // It was a short option, look it up.
  87. auto it = m_options.find_if([c](auto& opt) { return c == opt.short_name; });
  88. VERIFY(!it.is_end());
  89. found_option = &*it;
  90. }
  91. VERIFY(found_option);
  92. const char* arg = found_option->requires_argument ? optarg : nullptr;
  93. if (!found_option->accept_value(arg)) {
  94. warnln("\033[31mInvalid value for option \033[1m{}\033[22m\033[0m", found_option->name_for_display());
  95. fail();
  96. return false;
  97. }
  98. }
  99. // We're done processing options.
  100. // Now let's show version or help if requested, or perform autocompletion if needed.
  101. if (m_show_version) {
  102. print_version(stdout);
  103. if (failure_behavior == FailureBehavior::Exit || failure_behavior == FailureBehavior::PrintUsageAndExit)
  104. exit(0);
  105. return false;
  106. }
  107. if (m_show_help) {
  108. print_usage(stdout, argv[0]);
  109. if (failure_behavior == FailureBehavior::Exit || failure_behavior == FailureBehavior::PrintUsageAndExit)
  110. exit(0);
  111. return false;
  112. }
  113. if (m_perform_autocomplete) {
  114. autocomplete(stdout, argv[0], Span<char const* const> { argv + optind, static_cast<size_t>(argc - optind) });
  115. if (failure_behavior == FailureBehavior::Exit || failure_behavior == FailureBehavior::PrintUsageAndExit)
  116. exit(0);
  117. return false;
  118. }
  119. // Now let's parse positional arguments.
  120. int values_left = argc - optind;
  121. Vector<int, 16> num_values_for_arg;
  122. num_values_for_arg.resize(m_positional_args.size(), true);
  123. int total_values_required = 0;
  124. for (size_t i = 0; i < m_positional_args.size(); i++) {
  125. auto& arg = m_positional_args[i];
  126. num_values_for_arg[i] = arg.min_values;
  127. total_values_required += arg.min_values;
  128. }
  129. if (total_values_required > values_left) {
  130. fail();
  131. return false;
  132. }
  133. int extra_values_to_distribute = values_left - total_values_required;
  134. for (size_t i = 0; i < m_positional_args.size(); i++) {
  135. auto& arg = m_positional_args[i];
  136. int extra_values_to_this_arg = min(arg.max_values - arg.min_values, extra_values_to_distribute);
  137. num_values_for_arg[i] += extra_values_to_this_arg;
  138. extra_values_to_distribute -= extra_values_to_this_arg;
  139. if (extra_values_to_distribute == 0)
  140. break;
  141. }
  142. if (extra_values_to_distribute > 0) {
  143. // We still have too many values :(
  144. fail();
  145. return false;
  146. }
  147. for (size_t i = 0; i < m_positional_args.size(); i++) {
  148. auto& arg = m_positional_args[i];
  149. for (int j = 0; j < num_values_for_arg[i]; j++) {
  150. const char* value = argv[optind++];
  151. if (!arg.accept_value(value)) {
  152. warnln("Invalid value for argument {}", arg.name);
  153. fail();
  154. return false;
  155. }
  156. }
  157. }
  158. return true;
  159. }
  160. void ArgsParser::print_usage(FILE* file, const char* argv0)
  161. {
  162. char const* env_preference = getenv("ARGSPARSER_EMIT_MARKDOWN");
  163. if (env_preference != nullptr && env_preference[0] == '1' && env_preference[1] == 0) {
  164. print_usage_markdown(file, argv0);
  165. } else {
  166. print_usage_terminal(file, argv0);
  167. }
  168. }
  169. void ArgsParser::print_usage_terminal(FILE* file, const char* argv0)
  170. {
  171. out(file, "Usage:\n\t\033[1m{}\033[0m", argv0);
  172. for (auto& opt : m_options) {
  173. if (opt.hide_from_help_and_autocomplete)
  174. continue;
  175. if (opt.requires_argument)
  176. out(file, " [{} {}]", opt.name_for_display(), opt.value_name);
  177. else
  178. out(file, " [{}]", opt.name_for_display());
  179. }
  180. for (auto& arg : m_positional_args) {
  181. bool required = arg.min_values > 0;
  182. bool repeated = arg.max_values > 1;
  183. if (required && repeated)
  184. out(file, " <{}...>", arg.name);
  185. else if (required && !repeated)
  186. out(file, " <{}>", arg.name);
  187. else if (!required && repeated)
  188. out(file, " [{}...]", arg.name);
  189. else if (!required && !repeated)
  190. out(file, " [{}]", arg.name);
  191. }
  192. outln(file);
  193. if (m_general_help != nullptr && m_general_help[0] != '\0') {
  194. outln(file, "\nDescription:");
  195. outln(file, "{}", m_general_help);
  196. }
  197. if (!m_options.is_empty())
  198. outln(file, "\nOptions:");
  199. for (auto& opt : m_options) {
  200. auto print_argument = [&]() {
  201. if (opt.value_name) {
  202. if (opt.requires_argument)
  203. out(file, " {}", opt.value_name);
  204. else
  205. out(file, " [{}]", opt.value_name);
  206. }
  207. };
  208. out(file, "\t");
  209. if (opt.short_name) {
  210. out(file, "\033[1m-{}\033[0m", opt.short_name);
  211. print_argument();
  212. }
  213. if (opt.short_name && opt.long_name)
  214. out(file, ", ");
  215. if (opt.long_name) {
  216. out(file, "\033[1m--{}\033[0m", opt.long_name);
  217. print_argument();
  218. }
  219. if (opt.help_string)
  220. out(file, "\t{}", opt.help_string);
  221. outln(file);
  222. }
  223. if (!m_positional_args.is_empty())
  224. outln(file, "\nArguments:");
  225. for (auto& arg : m_positional_args) {
  226. out(file, "\t\033[1m{}\033[0m", arg.name);
  227. if (arg.help_string)
  228. out(file, "\t{}", arg.help_string);
  229. outln(file);
  230. }
  231. }
  232. void ArgsParser::print_usage_markdown(FILE* file, const char* argv0)
  233. {
  234. outln(file, "## Name\n\n{}", argv0);
  235. out(file, "\n## Synopsis\n\n```sh\n$ {}", argv0);
  236. for (auto& opt : m_options) {
  237. if (opt.hide_from_help_and_autocomplete)
  238. continue;
  239. if (opt.requires_argument)
  240. out(file, " [{} {}]", opt.name_for_display(), opt.value_name);
  241. else
  242. out(file, " [{}]", opt.name_for_display());
  243. }
  244. for (auto& arg : m_positional_args) {
  245. bool required = arg.min_values > 0;
  246. bool repeated = arg.max_values > 1;
  247. if (required && repeated)
  248. out(file, " <{}...>", arg.name);
  249. else if (required && !repeated)
  250. out(file, " <{}>", arg.name);
  251. else if (!required && repeated)
  252. out(file, " [{}...]", arg.name);
  253. else if (!required && !repeated)
  254. out(file, " [{}]", arg.name);
  255. }
  256. outln(file, "\n```");
  257. if (m_general_help != nullptr && m_general_help[0] != '\0') {
  258. outln(file, "\n## Description\n\n{}", m_general_help);
  259. }
  260. if (!m_options.is_empty())
  261. outln(file, "\n## Options:\n");
  262. for (auto& opt : m_options) {
  263. auto print_argument = [&]() {
  264. if (opt.value_name != nullptr) {
  265. if (opt.requires_argument)
  266. out(file, " {}", opt.value_name);
  267. else
  268. out(file, " [{}]", opt.value_name);
  269. }
  270. };
  271. out(file, "* ");
  272. if (opt.short_name != '\0') {
  273. out(file, "`-{}", opt.short_name);
  274. print_argument();
  275. out(file, "`");
  276. }
  277. if (opt.short_name != '\0' && opt.long_name != nullptr)
  278. out(file, ", ");
  279. if (opt.long_name != nullptr) {
  280. out(file, "`--{}", opt.long_name);
  281. print_argument();
  282. out(file, "`");
  283. }
  284. if (opt.help_string != nullptr)
  285. out(file, ": {}", opt.help_string);
  286. outln(file);
  287. }
  288. if (!m_positional_args.is_empty())
  289. outln(file, "\n## Arguments:\n");
  290. for (auto& arg : m_positional_args) {
  291. out(file, "* `{}`", arg.name);
  292. if (arg.help_string != nullptr)
  293. out(file, ": {}", arg.help_string);
  294. outln(file);
  295. }
  296. }
  297. void ArgsParser::print_version(FILE* file)
  298. {
  299. outln(file, Core::Version::SERENITY_VERSION);
  300. }
  301. void ArgsParser::add_option(Option&& option)
  302. {
  303. m_options.append(move(option));
  304. }
  305. void ArgsParser::add_ignored(const char* long_name, char short_name, bool hidden)
  306. {
  307. Option option {
  308. false,
  309. "Ignored",
  310. long_name,
  311. short_name,
  312. nullptr,
  313. [](const char*) {
  314. return true;
  315. },
  316. hidden,
  317. };
  318. add_option(move(option));
  319. }
  320. void ArgsParser::add_option(bool& value, const char* help_string, const char* long_name, char short_name, bool hidden)
  321. {
  322. Option option {
  323. false,
  324. help_string,
  325. long_name,
  326. short_name,
  327. nullptr,
  328. [&value](const char* s) {
  329. VERIFY(s == nullptr);
  330. value = true;
  331. return true;
  332. },
  333. hidden,
  334. };
  335. add_option(move(option));
  336. }
  337. void ArgsParser::add_option(const char*& value, const char* help_string, const char* long_name, char short_name, const char* value_name, bool hidden)
  338. {
  339. Option option {
  340. true,
  341. help_string,
  342. long_name,
  343. short_name,
  344. value_name,
  345. [&value](const char* s) {
  346. value = s;
  347. return true;
  348. },
  349. hidden,
  350. };
  351. add_option(move(option));
  352. }
  353. void ArgsParser::add_option(String& value, const char* help_string, const char* long_name, char short_name, const char* value_name, bool hidden)
  354. {
  355. Option option {
  356. true,
  357. help_string,
  358. long_name,
  359. short_name,
  360. value_name,
  361. [&value](const char* s) {
  362. value = s;
  363. return true;
  364. },
  365. hidden,
  366. };
  367. add_option(move(option));
  368. }
  369. void ArgsParser::add_option(StringView& value, char const* help_string, char const* long_name, char short_name, char const* value_name, bool hidden)
  370. {
  371. Option option {
  372. true,
  373. help_string,
  374. long_name,
  375. short_name,
  376. value_name,
  377. [&value](const char* s) {
  378. value = s;
  379. return true;
  380. },
  381. hidden,
  382. };
  383. add_option(move(option));
  384. }
  385. void ArgsParser::add_option(int& value, const char* help_string, const char* long_name, char short_name, const char* value_name, bool hidden)
  386. {
  387. Option option {
  388. true,
  389. help_string,
  390. long_name,
  391. short_name,
  392. value_name,
  393. [&value](const char* s) {
  394. auto opt = StringView(s).to_int();
  395. value = opt.value_or(0);
  396. return opt.has_value();
  397. },
  398. hidden,
  399. };
  400. add_option(move(option));
  401. }
  402. void ArgsParser::add_option(unsigned& value, const char* help_string, const char* long_name, char short_name, const char* value_name, bool hidden)
  403. {
  404. Option option {
  405. true,
  406. help_string,
  407. long_name,
  408. short_name,
  409. value_name,
  410. [&value](const char* s) {
  411. auto opt = StringView(s).to_uint();
  412. value = opt.value_or(0);
  413. return opt.has_value();
  414. },
  415. hidden,
  416. };
  417. add_option(move(option));
  418. }
  419. void ArgsParser::add_option(double& value, const char* help_string, const char* long_name, char short_name, const char* value_name, bool hidden)
  420. {
  421. Option option {
  422. true,
  423. help_string,
  424. long_name,
  425. short_name,
  426. value_name,
  427. [&value](const char* s) {
  428. auto opt = convert_to_double(s);
  429. value = opt.value_or(0.0);
  430. return opt.has_value();
  431. },
  432. hidden,
  433. };
  434. add_option(move(option));
  435. }
  436. void ArgsParser::add_option(Optional<double>& value, const char* help_string, const char* long_name, char short_name, const char* value_name, bool hidden)
  437. {
  438. Option option {
  439. true,
  440. help_string,
  441. long_name,
  442. short_name,
  443. value_name,
  444. [&value](const char* s) {
  445. value = convert_to_double(s);
  446. return value.has_value();
  447. },
  448. hidden,
  449. };
  450. add_option(move(option));
  451. }
  452. void ArgsParser::add_option(Optional<size_t>& value, const char* help_string, const char* long_name, char short_name, const char* value_name, bool hidden)
  453. {
  454. Option option {
  455. true,
  456. help_string,
  457. long_name,
  458. short_name,
  459. value_name,
  460. [&value](const char* s) {
  461. value = AK::StringUtils::convert_to_uint<size_t>(s);
  462. return value.has_value();
  463. },
  464. hidden,
  465. };
  466. add_option(move(option));
  467. }
  468. void ArgsParser::add_option(Vector<size_t>& values, char const* help_string, char const* long_name, char short_name, char const* value_name, char separator, bool hidden)
  469. {
  470. Option option {
  471. true,
  472. help_string,
  473. long_name,
  474. short_name,
  475. value_name,
  476. [&values, separator](char const* s) {
  477. bool parsed_all_values = true;
  478. StringView { s }.for_each_split_view(separator, false, [&](auto value) {
  479. if (auto maybe_value = AK::StringUtils::convert_to_uint<size_t>(value); maybe_value.has_value())
  480. values.append(*maybe_value);
  481. else
  482. parsed_all_values = false;
  483. });
  484. return parsed_all_values;
  485. },
  486. hidden
  487. };
  488. add_option(move(option));
  489. }
  490. void ArgsParser::add_positional_argument(Arg&& arg)
  491. {
  492. m_positional_args.append(move(arg));
  493. }
  494. void ArgsParser::add_positional_argument(const char*& value, const char* help_string, const char* name, Required required)
  495. {
  496. Arg arg {
  497. help_string,
  498. name,
  499. required == Required::Yes ? 1 : 0,
  500. 1,
  501. [&value](const char* s) {
  502. value = s;
  503. return true;
  504. }
  505. };
  506. add_positional_argument(move(arg));
  507. }
  508. void ArgsParser::add_positional_argument(String& value, const char* help_string, const char* name, Required required)
  509. {
  510. Arg arg {
  511. help_string,
  512. name,
  513. required == Required::Yes ? 1 : 0,
  514. 1,
  515. [&value](const char* s) {
  516. value = s;
  517. return true;
  518. }
  519. };
  520. add_positional_argument(move(arg));
  521. }
  522. void ArgsParser::add_positional_argument(StringView& value, char const* help_string, char const* name, Required required)
  523. {
  524. Arg arg {
  525. help_string,
  526. name,
  527. required == Required::Yes ? 1 : 0,
  528. 1,
  529. [&value](const char* s) {
  530. value = s;
  531. return true;
  532. }
  533. };
  534. add_positional_argument(move(arg));
  535. }
  536. void ArgsParser::add_positional_argument(int& value, const char* help_string, const char* name, Required required)
  537. {
  538. Arg arg {
  539. help_string,
  540. name,
  541. required == Required::Yes ? 1 : 0,
  542. 1,
  543. [&value](const char* s) {
  544. auto opt = StringView(s).to_int();
  545. value = opt.value_or(0);
  546. return opt.has_value();
  547. }
  548. };
  549. add_positional_argument(move(arg));
  550. }
  551. void ArgsParser::add_positional_argument(unsigned& value, const char* help_string, const char* name, Required required)
  552. {
  553. Arg arg {
  554. help_string,
  555. name,
  556. required == Required::Yes ? 1 : 0,
  557. 1,
  558. [&value](const char* s) {
  559. auto opt = StringView(s).to_uint();
  560. value = opt.value_or(0);
  561. return opt.has_value();
  562. }
  563. };
  564. add_positional_argument(move(arg));
  565. }
  566. void ArgsParser::add_positional_argument(double& value, const char* help_string, const char* name, Required required)
  567. {
  568. Arg arg {
  569. help_string,
  570. name,
  571. required == Required::Yes ? 1 : 0,
  572. 1,
  573. [&value](const char* s) {
  574. auto opt = convert_to_double(s);
  575. value = opt.value_or(0.0);
  576. return opt.has_value();
  577. }
  578. };
  579. add_positional_argument(move(arg));
  580. }
  581. void ArgsParser::add_positional_argument(Vector<const char*>& values, const char* help_string, const char* name, Required required)
  582. {
  583. Arg arg {
  584. help_string,
  585. name,
  586. required == Required::Yes ? 1 : 0,
  587. INT_MAX,
  588. [&values](const char* s) {
  589. values.append(s);
  590. return true;
  591. }
  592. };
  593. add_positional_argument(move(arg));
  594. }
  595. void ArgsParser::add_positional_argument(Vector<StringView>& values, char const* help_string, char const* name, Required required)
  596. {
  597. Arg arg {
  598. help_string,
  599. name,
  600. required == Required::Yes ? 1 : 0,
  601. INT_MAX,
  602. [&values](char const* s) {
  603. values.append(s);
  604. return true;
  605. }
  606. };
  607. add_positional_argument(move(arg));
  608. }
  609. void ArgsParser::autocomplete(FILE* file, StringView program_name, Span<char const* const> remaining_arguments)
  610. {
  611. // We expect the full invocation of the program to be available as positional args,
  612. // e.g. `foo --bar arg -b` (program invoked as `foo --complete -- foo --bar arg -b`)
  613. auto first = true;
  614. auto seen_all_options = false;
  615. auto skip_next = false;
  616. StringView argument_to_complete;
  617. StringView option_to_complete;
  618. auto completing_option = false;
  619. for (auto& arg : remaining_arguments) {
  620. StringView argument { arg };
  621. completing_option = false;
  622. if (skip_next) {
  623. argument_to_complete = argument;
  624. skip_next = false;
  625. continue;
  626. }
  627. // Skip over the program name.
  628. if (first && program_name == argument) {
  629. first = false;
  630. continue;
  631. }
  632. if (seen_all_options) {
  633. argument_to_complete = argument;
  634. continue;
  635. }
  636. if (argument.starts_with("--")) {
  637. option_to_complete = argument;
  638. completing_option = true;
  639. if (argument == "--") {
  640. seen_all_options = true;
  641. continue;
  642. }
  643. // Look for a long option
  644. auto option_pattern = argument.substring_view(2);
  645. auto it = m_options.find_if([&](auto& option) { return !option.hide_from_help_and_autocomplete && StringView(option.long_name) == option_pattern; });
  646. if (it.is_end())
  647. continue;
  648. if (it->requires_argument)
  649. skip_next = true;
  650. continue;
  651. }
  652. if (argument.starts_with("-")) {
  653. option_to_complete = argument;
  654. completing_option = true;
  655. if (argument == "-") {
  656. option_to_complete = argument;
  657. continue;
  658. }
  659. // Look for a short option
  660. auto option_pattern = argument[argument.length() - 1];
  661. auto it = m_options.find_if([&](auto& option) { return !option.hide_from_help_and_autocomplete && option.short_name == option_pattern; });
  662. if (it.is_end())
  663. continue;
  664. if (it->requires_argument)
  665. skip_next = true;
  666. continue;
  667. }
  668. }
  669. // We don't know how to complete arguments quite yet.
  670. if (!completing_option)
  671. return;
  672. auto write_completion = [&](auto format, auto& option, auto... args) {
  673. JsonObject object;
  674. object.set("completion", String::formatted(format, args...));
  675. object.set("static_offset", 0);
  676. object.set("invariant_offset", option_to_complete.length());
  677. object.set("display_trivia", option.help_string);
  678. object.set("trailing_trivia", option.requires_argument ? " " : "");
  679. outln(file, "{}", object.to_string());
  680. };
  681. if (option_to_complete.starts_with("--")) {
  682. // Complete a long option.
  683. auto option_pattern = option_to_complete.substring_view(2);
  684. for (auto& option : m_options) {
  685. if (option.hide_from_help_and_autocomplete)
  686. continue;
  687. StringView option_string = option.long_name;
  688. if (option_string.starts_with(option_pattern)) {
  689. write_completion("--{}", option, option_string);
  690. }
  691. }
  692. } else {
  693. // Complete a short option, note that we're not going to attempt to 'match' anything here.
  694. for (auto& option : m_options) {
  695. if (option.hide_from_help_and_autocomplete)
  696. continue;
  697. if (option.short_name == 0)
  698. continue;
  699. write_completion("-{}", option, option.short_name);
  700. }
  701. }
  702. }
  703. }