parse.cc 32 KB

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