MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / ParseCommandLineFlags

Function ParseCommandLineFlags

paddle/common/flags_native.cc:481–591  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

479TEST_API void AllowUndefinedFlags() { allow_undefined_flags = true; }
480
481TEST_API void ParseCommandLineFlags(int* pargc, char*** pargv) {
482 assert(*pargc > 0);
483 size_t argv_num = *pargc - 1;
484 std::vector<std::string> argvs(*pargv + 1, *pargv + *pargc);
485
486 std::string arg_format_help =
487 "please follow the formats: \"--help(h)\", \"--name=value\""
488 " or \"--name value\".";
489 for (size_t i = 0; i < argv_num; i++) {
490 const std::string& argv = argvs[i];
491
492 if (argv.empty()) {
493 continue;
494 }
495
496 if (argv.size() < 2 || argv[0] != '-') {
497 LOG_FLAG_FATAL_ERROR("invalid commandline argument: \"" + argv + "\", " +
498 arg_format_help);
499 }
500
501 // parse arg name and value
502 size_t hyphen_num = argv[1] == '-' ? 2 : 1;
503 std::string name, value;
504 size_t split_pos = argv.find('=');
505 if (split_pos == std::string::npos) {
506 // the argv format is "--name" or "--name value"
507 name = argv.substr(hyphen_num);
508 if (name.empty()) {
509 std::string error_msg = "invalid commandline argument: \"";
510 error_msg += argv;
511 error_msg += "\", ";
512 error_msg += arg_format_help;
513 LOG_FLAG_FATAL_ERROR(error_msg);
514 }
515
516 // print help message
517 if (name == "help" || name == "h") {
518 FlagRegistry::Instance()->PrintAllFlagHelp(std::cout);
519 exit(1);
520 }
521
522 // get the value from next argv.
523 if (++i == argv_num) {
524 std::string error_msg = "expected value of flag \"";
525 error_msg += name;
526 error_msg += "\" but found none.";
527 LOG_FLAG_FATAL_ERROR(error_msg);
528 } else {
529 value = argvs[i];
530 }
531 } else {
532 // the argv format is "--name=value"
533 if (split_pos == hyphen_num || split_pos == argv.size() - 1) {
534 std::string error_msg = "invalid commandline argument: \"";
535 error_msg += argv;
536 error_msg += "\", ";
537 error_msg += arg_format_help;
538 LOG_FLAG_FATAL_ERROR(error_msg);

Callers

nothing calls this directly

Calls 12

InstanceFunction · 0.85
SetFlagsFromEnvFunction · 0.85
exit_with_errorsFunction · 0.85
PrintAllFlagHelpMethod · 0.80
SetFlagValueMethod · 0.80
emptyMethod · 0.45
sizeMethod · 0.45
findMethod · 0.45
backMethod · 0.45
pop_backMethod · 0.45
push_backMethod · 0.45
strMethod · 0.45

Tested by

no test coverage detected