parse.cc 32 KB

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