parse.cc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. //
  2. // Copyright 2019 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #include "absl/flags/parse.h"
  16. #include <stdlib.h>
  17. #include <algorithm>
  18. #include <cstdint>
  19. #include <fstream>
  20. #include <iostream>
  21. #include <iterator>
  22. #include <string>
  23. #include <tuple>
  24. #include <utility>
  25. #include <vector>
  26. #ifdef _WIN32
  27. #include <windows.h>
  28. #endif
  29. #include "absl/algorithm/container.h"
  30. #include "absl/base/attributes.h"
  31. #include "absl/base/config.h"
  32. #include "absl/base/const_init.h"
  33. #include "absl/base/thread_annotations.h"
  34. #include "absl/flags/commandlineflag.h"
  35. #include "absl/flags/config.h"
  36. #include "absl/flags/flag.h"
  37. #include "absl/flags/internal/commandlineflag.h"
  38. #include "absl/flags/internal/flag.h"
  39. #include "absl/flags/internal/parse.h"
  40. #include "absl/flags/internal/private_handle_accessor.h"
  41. #include "absl/flags/internal/program_name.h"
  42. #include "absl/flags/internal/usage.h"
  43. #include "absl/flags/reflection.h"
  44. #include "absl/flags/usage.h"
  45. #include "absl/flags/usage_config.h"
  46. #include "absl/strings/ascii.h"
  47. #include "absl/strings/internal/damerau_levenshtein_distance.h"
  48. #include "absl/strings/str_cat.h"
  49. #include "absl/strings/str_join.h"
  50. #include "absl/strings/string_view.h"
  51. #include "absl/strings/strip.h"
  52. #include "absl/synchronization/mutex.h"
  53. // --------------------------------------------------------------------
  54. namespace absl {
  55. ABSL_NAMESPACE_BEGIN
  56. namespace flags_internal {
  57. namespace {
  58. ABSL_CONST_INIT absl::Mutex processing_checks_guard(absl::kConstInit);
  59. ABSL_CONST_INIT bool flagfile_needs_processing
  60. ABSL_GUARDED_BY(processing_checks_guard) = false;
  61. ABSL_CONST_INIT bool fromenv_needs_processing
  62. ABSL_GUARDED_BY(processing_checks_guard) = false;
  63. ABSL_CONST_INIT bool tryfromenv_needs_processing
  64. ABSL_GUARDED_BY(processing_checks_guard) = false;
  65. ABSL_CONST_INIT absl::Mutex specified_flags_guard(absl::kConstInit);
  66. ABSL_CONST_INIT std::vector<const CommandLineFlag*>* specified_flags
  67. ABSL_GUARDED_BY(specified_flags_guard) = nullptr;
  68. // Suggesting at most kMaxHints flags in case of misspellings.
  69. ABSL_CONST_INIT const size_t kMaxHints = 100;
  70. // Suggesting only flags which have a smaller distance than kMaxDistance.
  71. ABSL_CONST_INIT const size_t kMaxDistance = 3;
  72. struct SpecifiedFlagsCompare {
  73. bool operator()(const CommandLineFlag* a, const CommandLineFlag* b) const {
  74. return a->Name() < b->Name();
  75. }
  76. bool operator()(const CommandLineFlag* a, absl::string_view b) const {
  77. return a->Name() < b;
  78. }
  79. bool operator()(absl::string_view a, const CommandLineFlag* b) const {
  80. return a < b->Name();
  81. }
  82. };
  83. } // namespace
  84. } // namespace flags_internal
  85. ABSL_NAMESPACE_END
  86. } // namespace absl
  87. ABSL_FLAG(std::vector<std::string>, flagfile, {},
  88. "comma-separated list of files to load flags from")
  89. .OnUpdate([]() {
  90. if (absl::GetFlag(FLAGS_flagfile).empty()) return;
  91. absl::MutexLock l(&absl::flags_internal::processing_checks_guard);
  92. // Setting this flag twice before it is handled most likely an internal
  93. // error and should be reviewed by developers.
  94. if (absl::flags_internal::flagfile_needs_processing) {
  95. ABSL_INTERNAL_LOG(WARNING, "flagfile set twice before it is handled");
  96. }
  97. absl::flags_internal::flagfile_needs_processing = true;
  98. });
  99. ABSL_FLAG(std::vector<std::string>, fromenv, {},
  100. "comma-separated list of flags to set from the environment"
  101. " [use 'export FLAGS_flag1=value']")
  102. .OnUpdate([]() {
  103. if (absl::GetFlag(FLAGS_fromenv).empty()) return;
  104. absl::MutexLock l(&absl::flags_internal::processing_checks_guard);
  105. // Setting this flag twice before it is handled most likely an internal
  106. // error and should be reviewed by developers.
  107. if (absl::flags_internal::fromenv_needs_processing) {
  108. ABSL_INTERNAL_LOG(WARNING, "fromenv set twice before it is handled.");
  109. }
  110. absl::flags_internal::fromenv_needs_processing = true;
  111. });
  112. ABSL_FLAG(std::vector<std::string>, tryfromenv, {},
  113. "comma-separated list of flags to try to set from the environment if "
  114. "present")
  115. .OnUpdate([]() {
  116. if (absl::GetFlag(FLAGS_tryfromenv).empty()) return;
  117. absl::MutexLock l(&absl::flags_internal::processing_checks_guard);
  118. // Setting this flag twice before it is handled most likely an internal
  119. // error and should be reviewed by developers.
  120. if (absl::flags_internal::tryfromenv_needs_processing) {
  121. ABSL_INTERNAL_LOG(WARNING,
  122. "tryfromenv set twice before it is handled.");
  123. }
  124. absl::flags_internal::tryfromenv_needs_processing = true;
  125. });
  126. ABSL_FLAG(std::vector<std::string>, undefok, {},
  127. "comma-separated list of flag names that it is okay to specify "
  128. "on the command line even if the program does not define a flag "
  129. "with that name");
  130. namespace absl {
  131. ABSL_NAMESPACE_BEGIN
  132. namespace flags_internal {
  133. namespace {
  134. class ArgsList {
  135. public:
  136. ArgsList() : next_arg_(0) {}
  137. ArgsList(int argc, char* argv[]) : args_(argv, argv + argc), next_arg_(0) {}
  138. explicit ArgsList(const std::vector<std::string>& args)
  139. : args_(args), next_arg_(0) {}
  140. // Returns success status: true if parsing successful, false otherwise.
  141. bool ReadFromFlagfile(const std::string& flag_file_name);
  142. size_t Size() const { return args_.size() - next_arg_; }
  143. size_t FrontIndex() const { return next_arg_; }
  144. absl::string_view Front() const { return args_[next_arg_]; }
  145. void PopFront() { next_arg_++; }
  146. private:
  147. std::vector<std::string> args_;
  148. size_t next_arg_;
  149. };
  150. bool ArgsList::ReadFromFlagfile(const std::string& flag_file_name) {
  151. std::ifstream flag_file(flag_file_name);
  152. if (!flag_file) {
  153. flags_internal::ReportUsageError(
  154. absl::StrCat("Can't open flagfile ", flag_file_name), true);
  155. return false;
  156. }
  157. // This argument represents fake argv[0], which should be present in all arg
  158. // lists.
  159. args_.push_back("");
  160. std::string line;
  161. bool success = true;
  162. while (std::getline(flag_file, line)) {
  163. absl::string_view stripped = absl::StripLeadingAsciiWhitespace(line);
  164. if (stripped.empty() || stripped[0] == '#') {
  165. // Comment or empty line; just ignore.
  166. continue;
  167. }
  168. if (stripped[0] == '-') {
  169. if (stripped == "--") {
  170. flags_internal::ReportUsageError(
  171. "Flagfile can't contain position arguments or --", true);
  172. success = false;
  173. break;
  174. }
  175. args_.push_back(std::string(stripped));
  176. continue;
  177. }
  178. flags_internal::ReportUsageError(
  179. absl::StrCat("Unexpected line in the flagfile ", flag_file_name, ": ",
  180. line),
  181. true);
  182. success = false;
  183. }
  184. return success;
  185. }
  186. // --------------------------------------------------------------------
  187. // Reads the environment variable with name `name` and stores results in
  188. // `value`. If variable is not present in environment returns false, otherwise
  189. // returns true.
  190. bool GetEnvVar(const char* var_name, std::string& var_value) {
  191. #ifdef _WIN32
  192. char buf[1024];
  193. auto get_res = GetEnvironmentVariableA(var_name, buf, sizeof(buf));
  194. if (get_res >= sizeof(buf)) {
  195. return false;
  196. }
  197. if (get_res == 0) {
  198. return false;
  199. }
  200. var_value = std::string(buf, get_res);
  201. #else
  202. const char* val = ::getenv(var_name);
  203. if (val == nullptr) {
  204. return false;
  205. }
  206. var_value = val;
  207. #endif
  208. return true;
  209. }
  210. // --------------------------------------------------------------------
  211. // Returns:
  212. // Flag name or empty if arg= --
  213. // Flag value after = in --flag=value (empty if --foo)
  214. // "Is empty value" status. True if arg= --foo=, false otherwise. This is
  215. // required to separate --foo from --foo=.
  216. // For example:
  217. // arg return values
  218. // "--foo=bar" -> {"foo", "bar", false}.
  219. // "--foo" -> {"foo", "", false}.
  220. // "--foo=" -> {"foo", "", true}.
  221. std::tuple<absl::string_view, absl::string_view, bool> SplitNameAndValue(
  222. absl::string_view arg) {
  223. // Allow -foo and --foo
  224. absl::ConsumePrefix(&arg, "-");
  225. if (arg.empty()) {
  226. return std::make_tuple("", "", false);
  227. }
  228. auto equal_sign_pos = arg.find("=");
  229. absl::string_view flag_name = arg.substr(0, equal_sign_pos);
  230. absl::string_view value;
  231. bool is_empty_value = false;
  232. if (equal_sign_pos != absl::string_view::npos) {
  233. value = arg.substr(equal_sign_pos + 1);
  234. is_empty_value = value.empty();
  235. }
  236. return std::make_tuple(flag_name, value, is_empty_value);
  237. }
  238. // --------------------------------------------------------------------
  239. // Returns:
  240. // found flag or nullptr
  241. // is negative in case of --nofoo
  242. std::tuple<CommandLineFlag*, bool> LocateFlag(absl::string_view flag_name) {
  243. CommandLineFlag* flag = absl::FindCommandLineFlag(flag_name);
  244. bool is_negative = false;
  245. if (!flag && absl::ConsumePrefix(&flag_name, "no")) {
  246. flag = absl::FindCommandLineFlag(flag_name);
  247. is_negative = true;
  248. }
  249. return std::make_tuple(flag, is_negative);
  250. }
  251. // --------------------------------------------------------------------
  252. // Verify that default values of typed flags must be convertible to string and
  253. // back.
  254. void CheckDefaultValuesParsingRoundtrip() {
  255. #ifndef NDEBUG
  256. flags_internal::ForEachFlag([&](CommandLineFlag& flag) {
  257. if (flag.IsRetired()) return;
  258. #define ABSL_FLAGS_INTERNAL_IGNORE_TYPE(T, _) \
  259. if (flag.IsOfType<T>()) return;
  260. ABSL_FLAGS_INTERNAL_SUPPORTED_TYPES(ABSL_FLAGS_INTERNAL_IGNORE_TYPE)
  261. #undef ABSL_FLAGS_INTERNAL_IGNORE_TYPE
  262. flags_internal::PrivateHandleAccessor::CheckDefaultValueParsingRoundtrip(
  263. flag);
  264. });
  265. #endif
  266. }
  267. // --------------------------------------------------------------------
  268. // Returns success status, which is true if we successfully read all flag files,
  269. // in which case new ArgLists are appended to the input_args in a reverse order
  270. // of file names in the input flagfiles list. This order ensures that flags from
  271. // the first flagfile in the input list are processed before the second flagfile
  272. // etc.
  273. bool ReadFlagfiles(const std::vector<std::string>& flagfiles,
  274. std::vector<ArgsList>& input_args) {
  275. bool success = true;
  276. for (auto it = flagfiles.rbegin(); it != flagfiles.rend(); ++it) {
  277. ArgsList al;
  278. if (al.ReadFromFlagfile(*it)) {
  279. input_args.push_back(al);
  280. } else {
  281. success = false;
  282. }
  283. }
  284. return success;
  285. }
  286. // Returns success status, which is true if were able to locate all environment
  287. // variables correctly or if fail_on_absent_in_env is false. The environment
  288. // variable names are expected to be of the form `FLAGS_<flag_name>`, where
  289. // `flag_name` is a string from the input flag_names list. If successful we
  290. // append a single ArgList at the end of the input_args.
  291. bool ReadFlagsFromEnv(const std::vector<std::string>& flag_names,
  292. std::vector<ArgsList>& input_args,
  293. bool fail_on_absent_in_env) {
  294. bool success = true;
  295. std::vector<std::string> args;
  296. // This argument represents fake argv[0], which should be present in all arg
  297. // lists.
  298. args.push_back("");
  299. for (const auto& flag_name : flag_names) {
  300. // Avoid infinite recursion.
  301. if (flag_name == "fromenv" || flag_name == "tryfromenv") {
  302. flags_internal::ReportUsageError(
  303. absl::StrCat("Infinite recursion on flag ", flag_name), true);
  304. success = false;
  305. continue;
  306. }
  307. const std::string envname = absl::StrCat("FLAGS_", flag_name);
  308. std::string envval;
  309. if (!GetEnvVar(envname.c_str(), envval)) {
  310. if (fail_on_absent_in_env) {
  311. flags_internal::ReportUsageError(
  312. absl::StrCat(envname, " not found in environment"), true);
  313. success = false;
  314. }
  315. continue;
  316. }
  317. args.push_back(absl::StrCat("--", flag_name, "=", envval));
  318. }
  319. if (success) {
  320. input_args.emplace_back(args);
  321. }
  322. return success;
  323. }
  324. // --------------------------------------------------------------------
  325. // Returns success status, which is true if were able to handle all generator
  326. // flags (flagfile, fromenv, tryfromemv) successfully.
  327. bool HandleGeneratorFlags(std::vector<ArgsList>& input_args,
  328. std::vector<std::string>& flagfile_value) {
  329. bool success = true;
  330. absl::MutexLock l(&flags_internal::processing_checks_guard);
  331. // flagfile could have been set either on a command line or
  332. // programmatically before invoking ParseCommandLine. Note that we do not
  333. // actually process arguments specified in the flagfile, but instead
  334. // create a secondary arguments list to be processed along with the rest
  335. // of the comamnd line arguments. Since we always the process most recently
  336. // created list of arguments first, this will result in flagfile argument
  337. // being processed before any other argument in the command line. If
  338. // FLAGS_flagfile contains more than one file name we create multiple new
  339. // levels of arguments in a reverse order of file names. Thus we always
  340. // process arguments from first file before arguments containing in a
  341. // second file, etc. If flagfile contains another
  342. // --flagfile inside of it, it will produce new level of arguments and
  343. // processed before the rest of the flagfile. We are also collecting all
  344. // flagfiles set on original command line. Unlike the rest of the flags,
  345. // this flag can be set multiple times and is expected to be handled
  346. // multiple times. We are collecting them all into a single list and set
  347. // the value of FLAGS_flagfile to that value at the end of the parsing.
  348. if (flags_internal::flagfile_needs_processing) {
  349. auto flagfiles = absl::GetFlag(FLAGS_flagfile);
  350. if (input_args.size() == 1) {
  351. flagfile_value.insert(flagfile_value.end(), flagfiles.begin(),
  352. flagfiles.end());
  353. }
  354. success &= ReadFlagfiles(flagfiles, input_args);
  355. flags_internal::flagfile_needs_processing = false;
  356. }
  357. // Similar to flagfile fromenv/tryfromemv can be set both
  358. // programmatically and at runtime on a command line. Unlike flagfile these
  359. // can't be recursive.
  360. if (flags_internal::fromenv_needs_processing) {
  361. auto flags_list = absl::GetFlag(FLAGS_fromenv);
  362. success &= ReadFlagsFromEnv(flags_list, input_args, true);
  363. flags_internal::fromenv_needs_processing = false;
  364. }
  365. if (flags_internal::tryfromenv_needs_processing) {
  366. auto flags_list = absl::GetFlag(FLAGS_tryfromenv);
  367. success &= ReadFlagsFromEnv(flags_list, input_args, false);
  368. flags_internal::tryfromenv_needs_processing = false;
  369. }
  370. return success;
  371. }
  372. // --------------------------------------------------------------------
  373. void ResetGeneratorFlags(const std::vector<std::string>& flagfile_value) {
  374. // Setting flagfile to the value which collates all the values set on a
  375. // command line and programmatically. So if command line looked like
  376. // --flagfile=f1 --flagfile=f2 the final value of the FLAGS_flagfile flag is
  377. // going to be {"f1", "f2"}
  378. if (!flagfile_value.empty()) {
  379. absl::SetFlag(&FLAGS_flagfile, flagfile_value);
  380. absl::MutexLock l(&flags_internal::processing_checks_guard);
  381. flags_internal::flagfile_needs_processing = false;
  382. }
  383. // fromenv/tryfromenv are set to <undefined> value.
  384. if (!absl::GetFlag(FLAGS_fromenv).empty()) {
  385. absl::SetFlag(&FLAGS_fromenv, {});
  386. }
  387. if (!absl::GetFlag(FLAGS_tryfromenv).empty()) {
  388. absl::SetFlag(&FLAGS_tryfromenv, {});
  389. }
  390. absl::MutexLock l(&flags_internal::processing_checks_guard);
  391. flags_internal::fromenv_needs_processing = false;
  392. flags_internal::tryfromenv_needs_processing = false;
  393. }
  394. // --------------------------------------------------------------------
  395. // Returns:
  396. // success status
  397. // deduced value
  398. // We are also mutating curr_list in case if we need to get a hold of next
  399. // argument in the input.
  400. std::tuple<bool, absl::string_view> DeduceFlagValue(const CommandLineFlag& flag,
  401. absl::string_view value,
  402. bool is_negative,
  403. bool is_empty_value,
  404. ArgsList* curr_list) {
  405. // Value is either an argument suffix after `=` in "--foo=<value>"
  406. // or separate argument in case of "--foo" "<value>".
  407. // boolean flags have these forms:
  408. // --foo
  409. // --nofoo
  410. // --foo=true
  411. // --foo=false
  412. // --nofoo=<value> is not supported
  413. // --foo <value> is not supported
  414. // non boolean flags have these forms:
  415. // --foo=<value>
  416. // --foo <value>
  417. // --nofoo is not supported
  418. if (flag.IsOfType<bool>()) {
  419. if (value.empty()) {
  420. if (is_empty_value) {
  421. // "--bool_flag=" case
  422. flags_internal::ReportUsageError(
  423. absl::StrCat(
  424. "Missing the value after assignment for the boolean flag '",
  425. flag.Name(), "'"),
  426. true);
  427. return std::make_tuple(false, "");
  428. }
  429. // "--bool_flag" case
  430. value = is_negative ? "0" : "1";
  431. } else if (is_negative) {
  432. // "--nobool_flag=Y" case
  433. flags_internal::ReportUsageError(
  434. absl::StrCat("Negative form with assignment is not valid for the "
  435. "boolean flag '",
  436. flag.Name(), "'"),
  437. true);
  438. return std::make_tuple(false, "");
  439. }
  440. } else if (is_negative) {
  441. // "--noint_flag=1" case
  442. flags_internal::ReportUsageError(
  443. absl::StrCat("Negative form is not valid for the flag '", flag.Name(),
  444. "'"),
  445. true);
  446. return std::make_tuple(false, "");
  447. } else if (value.empty() && (!is_empty_value)) {
  448. if (curr_list->Size() == 1) {
  449. // "--int_flag" case
  450. flags_internal::ReportUsageError(
  451. absl::StrCat("Missing the value for the flag '", flag.Name(), "'"),
  452. true);
  453. return std::make_tuple(false, "");
  454. }
  455. // "--int_flag" "10" case
  456. curr_list->PopFront();
  457. value = curr_list->Front();
  458. // Heuristic to detect the case where someone treats a string arg
  459. // like a bool or just forgets to pass a value:
  460. // --my_string_var --foo=bar
  461. // We look for a flag of string type, whose value begins with a
  462. // dash and corresponds to known flag or standalone --.
  463. if (!value.empty() && value[0] == '-' && flag.IsOfType<std::string>()) {
  464. auto maybe_flag_name = std::get<0>(SplitNameAndValue(value.substr(1)));
  465. if (maybe_flag_name.empty() ||
  466. std::get<0>(LocateFlag(maybe_flag_name)) != nullptr) {
  467. // "--string_flag" "--known_flag" case
  468. ABSL_INTERNAL_LOG(
  469. WARNING,
  470. absl::StrCat("Did you really mean to set flag '", flag.Name(),
  471. "' to the value '", value, "'?"));
  472. }
  473. }
  474. }
  475. return std::make_tuple(true, value);
  476. }
  477. // --------------------------------------------------------------------
  478. bool CanIgnoreUndefinedFlag(absl::string_view flag_name) {
  479. auto undefok = absl::GetFlag(FLAGS_undefok);
  480. if (std::find(undefok.begin(), undefok.end(), flag_name) != undefok.end()) {
  481. return true;
  482. }
  483. if (absl::ConsumePrefix(&flag_name, "no") &&
  484. std::find(undefok.begin(), undefok.end(), flag_name) != undefok.end()) {
  485. return true;
  486. }
  487. return false;
  488. }
  489. } // namespace
  490. // --------------------------------------------------------------------
  491. bool WasPresentOnCommandLine(absl::string_view flag_name) {
  492. absl::MutexLock l(&specified_flags_guard);
  493. ABSL_INTERNAL_CHECK(specified_flags != nullptr,
  494. "ParseCommandLine is not invoked yet");
  495. return std::binary_search(specified_flags->begin(), specified_flags->end(),
  496. flag_name, SpecifiedFlagsCompare{});
  497. }
  498. // --------------------------------------------------------------------
  499. struct BestHints {
  500. explicit BestHints(uint8_t _max) : best_distance(_max + 1) {}
  501. bool AddHint(absl::string_view hint, uint8_t distance) {
  502. if (hints.size() >= kMaxHints) return false;
  503. if (distance == best_distance) {
  504. hints.emplace_back(hint);
  505. }
  506. if (distance < best_distance) {
  507. best_distance = distance;
  508. hints = std::vector<std::string>{std::string(hint)};
  509. }
  510. return true;
  511. }
  512. uint8_t best_distance;
  513. std::vector<std::string> hints;
  514. };
  515. // Return the list of flags with the smallest Damerau-Levenshtein distance to
  516. // the given flag.
  517. std::vector<std::string> GetMisspellingHints(const absl::string_view flag) {
  518. const size_t maxCutoff = std::min(flag.size() / 2 + 1, kMaxDistance);
  519. auto undefok = absl::GetFlag(FLAGS_undefok);
  520. BestHints best_hints(static_cast<uint8_t>(maxCutoff));
  521. absl::flags_internal::ForEachFlag([&](const CommandLineFlag& f) {
  522. if (best_hints.hints.size() >= kMaxHints) return;
  523. uint8_t distance = strings_internal::CappedDamerauLevenshteinDistance(
  524. flag, f.Name(), best_hints.best_distance);
  525. best_hints.AddHint(f.Name(), distance);
  526. // For boolean flags, also calculate distance to the negated form.
  527. if (f.IsOfType<bool>()) {
  528. const std::string negated_flag = absl::StrCat("no", f.Name());
  529. distance = strings_internal::CappedDamerauLevenshteinDistance(
  530. flag, negated_flag, best_hints.best_distance);
  531. best_hints.AddHint(negated_flag, distance);
  532. }
  533. });
  534. // Finally calculate distance to flags in "undefok".
  535. absl::c_for_each(undefok, [&](const absl::string_view f) {
  536. if (best_hints.hints.size() >= kMaxHints) return;
  537. uint8_t distance = strings_internal::CappedDamerauLevenshteinDistance(
  538. flag, f, best_hints.best_distance);
  539. best_hints.AddHint(absl::StrCat(f, " (undefok)"), distance);
  540. });
  541. return best_hints.hints;
  542. }
  543. // --------------------------------------------------------------------
  544. std::vector<char*> ParseCommandLineImpl(int argc, char* argv[],
  545. ArgvListAction arg_list_act,
  546. UsageFlagsAction usage_flag_act,
  547. OnUndefinedFlag on_undef_flag) {
  548. ABSL_INTERNAL_CHECK(argc > 0, "Missing argv[0]");
  549. // Once parsing has started we will not have more flag registrations.
  550. // If we did, they would be missing during parsing, which is a problem on
  551. // itself.
  552. flags_internal::FinalizeRegistry();
  553. // This routine does not return anything since we abort on failure.
  554. CheckDefaultValuesParsingRoundtrip();
  555. std::vector<std::string> flagfile_value;
  556. std::vector<ArgsList> input_args;
  557. input_args.push_back(ArgsList(argc, argv));
  558. std::vector<char*> output_args;
  559. std::vector<char*> positional_args;
  560. output_args.reserve(static_cast<size_t>(argc));
  561. // This is the list of undefined flags. The element of the list is the pair
  562. // consisting of boolean indicating if flag came from command line (vs from
  563. // some flag file we've read) and flag name.
  564. // TODO(rogeeff): Eliminate the first element in the pair after cleanup.
  565. std::vector<std::pair<bool, std::string>> undefined_flag_names;
  566. // Set program invocation name if it is not set before.
  567. if (ProgramInvocationName() == "UNKNOWN") {
  568. flags_internal::SetProgramInvocationName(argv[0]);
  569. }
  570. output_args.push_back(argv[0]);
  571. absl::MutexLock l(&specified_flags_guard);
  572. if (specified_flags == nullptr) {
  573. specified_flags = new std::vector<const CommandLineFlag*>;
  574. } else {
  575. specified_flags->clear();
  576. }
  577. // Iterate through the list of the input arguments. First level are arguments
  578. // originated from argc/argv. Following levels are arguments originated from
  579. // recursive parsing of flagfile(s).
  580. bool success = true;
  581. while (!input_args.empty()) {
  582. // 10. First we process the built-in generator flags.
  583. success &= HandleGeneratorFlags(input_args, flagfile_value);
  584. // 30. Select top-most (most recent) arguments list. If it is empty drop it
  585. // and re-try.
  586. ArgsList& curr_list = input_args.back();
  587. curr_list.PopFront();
  588. if (curr_list.Size() == 0) {
  589. input_args.pop_back();
  590. continue;
  591. }
  592. // 40. Pick up the front remaining argument in the current list. If current
  593. // stack of argument lists contains only one element - we are processing an
  594. // argument from the original argv.
  595. absl::string_view arg(curr_list.Front());
  596. bool arg_from_argv = input_args.size() == 1;
  597. // 50. If argument does not start with - or is just "-" - this is
  598. // positional argument.
  599. if (!absl::ConsumePrefix(&arg, "-") || arg.empty()) {
  600. ABSL_INTERNAL_CHECK(arg_from_argv,
  601. "Flagfile cannot contain positional argument");
  602. positional_args.push_back(argv[curr_list.FrontIndex()]);
  603. continue;
  604. }
  605. if (arg_from_argv && (arg_list_act == ArgvListAction::kKeepParsedArgs)) {
  606. output_args.push_back(argv[curr_list.FrontIndex()]);
  607. }
  608. // 60. Split the current argument on '=' to figure out the argument
  609. // name and value. If flag name is empty it means we've got "--". value
  610. // can be empty either if there were no '=' in argument string at all or
  611. // an argument looked like "--foo=". In a latter case is_empty_value is
  612. // true.
  613. absl::string_view flag_name;
  614. absl::string_view value;
  615. bool is_empty_value = false;
  616. std::tie(flag_name, value, is_empty_value) = SplitNameAndValue(arg);
  617. // 70. "--" alone means what it does for GNU: stop flags parsing. We do
  618. // not support positional arguments in flagfiles, so we just drop them.
  619. if (flag_name.empty()) {
  620. ABSL_INTERNAL_CHECK(arg_from_argv,
  621. "Flagfile cannot contain positional argument");
  622. curr_list.PopFront();
  623. break;
  624. }
  625. // 80. Locate the flag based on flag name. Handle both --foo and --nofoo
  626. CommandLineFlag* flag = nullptr;
  627. bool is_negative = false;
  628. std::tie(flag, is_negative) = LocateFlag(flag_name);
  629. if (flag == nullptr) {
  630. // Usage flags are not modeled as Abseil flags. Locate them separately.
  631. if (flags_internal::DeduceUsageFlags(flag_name, value)) {
  632. continue;
  633. }
  634. if (on_undef_flag != OnUndefinedFlag::kIgnoreUndefined) {
  635. undefined_flag_names.emplace_back(arg_from_argv,
  636. std::string(flag_name));
  637. }
  638. continue;
  639. }
  640. // 90. Deduce flag's value (from this or next argument)
  641. auto curr_index = curr_list.FrontIndex();
  642. bool value_success = true;
  643. std::tie(value_success, value) =
  644. DeduceFlagValue(*flag, value, is_negative, is_empty_value, &curr_list);
  645. success &= value_success;
  646. // If above call consumed an argument, it was a standalone value
  647. if (arg_from_argv && (arg_list_act == ArgvListAction::kKeepParsedArgs) &&
  648. (curr_index != curr_list.FrontIndex())) {
  649. output_args.push_back(argv[curr_list.FrontIndex()]);
  650. }
  651. // 100. Set the located flag to a new new value, unless it is retired.
  652. // Setting retired flag fails, but we ignoring it here while also reporting
  653. // access to retired flag.
  654. std::string error;
  655. if (!flags_internal::PrivateHandleAccessor::ParseFrom(
  656. *flag, value, SET_FLAGS_VALUE, kCommandLine, error)) {
  657. if (flag->IsRetired()) continue;
  658. flags_internal::ReportUsageError(error, true);
  659. success = false;
  660. } else {
  661. specified_flags->push_back(flag);
  662. }
  663. }
  664. for (const auto& flag_name : undefined_flag_names) {
  665. if (CanIgnoreUndefinedFlag(flag_name.second)) continue;
  666. // Verify if flag_name has the "no" already removed
  667. std::vector<std::string> flags;
  668. if (flag_name.first) flags = GetMisspellingHints(flag_name.second);
  669. if (flags.empty()) {
  670. flags_internal::ReportUsageError(
  671. absl::StrCat("Unknown command line flag '", flag_name.second, "'"),
  672. true);
  673. } else {
  674. flags_internal::ReportUsageError(
  675. absl::StrCat("Unknown command line flag '", flag_name.second,
  676. "'. Did you mean: ", absl::StrJoin(flags, ", "), " ?"),
  677. true);
  678. }
  679. success = false;
  680. }
  681. #if ABSL_FLAGS_STRIP_NAMES
  682. if (!success) {
  683. flags_internal::ReportUsageError(
  684. "NOTE: command line flags are disabled in this build", true);
  685. }
  686. #endif
  687. if (!success) {
  688. flags_internal::HandleUsageFlags(std::cout,
  689. ProgramUsageMessage());
  690. std::exit(1);
  691. }
  692. if (usage_flag_act == UsageFlagsAction::kHandleUsage) {
  693. int exit_code = flags_internal::HandleUsageFlags(
  694. std::cout, ProgramUsageMessage());
  695. if (exit_code != -1) {
  696. std::exit(exit_code);
  697. }
  698. }
  699. ResetGeneratorFlags(flagfile_value);
  700. // Reinstate positional args which were intermixed with flags in the arguments
  701. // list.
  702. for (auto arg : positional_args) {
  703. output_args.push_back(arg);
  704. }
  705. // All the remaining arguments are positional.
  706. if (!input_args.empty()) {
  707. for (size_t arg_index = input_args.back().FrontIndex();
  708. arg_index < static_cast<size_t>(argc); ++arg_index) {
  709. output_args.push_back(argv[arg_index]);
  710. }
  711. }
  712. // Trim and sort the vector.
  713. specified_flags->shrink_to_fit();
  714. std::sort(specified_flags->begin(), specified_flags->end(),
  715. SpecifiedFlagsCompare{});
  716. return output_args;
  717. }
  718. } // namespace flags_internal
  719. // --------------------------------------------------------------------
  720. std::vector<char*> ParseCommandLine(int argc, char* argv[]) {
  721. return flags_internal::ParseCommandLineImpl(
  722. argc, argv, flags_internal::ArgvListAction::kRemoveParsedArgs,
  723. flags_internal::UsageFlagsAction::kHandleUsage,
  724. flags_internal::OnUndefinedFlag::kAbortIfUndefined);
  725. }
  726. ABSL_NAMESPACE_END
  727. } // namespace absl