You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

flag_parser.cc 6.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  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. * http://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. */
  16. #include "common/flag_parser.h"
  17. namespace mindspore {
  18. namespace predict {
  19. // parse flags read from command line
  20. Option<std::string> FlagParser::ParseFlags(int argc, const char *const *argv, bool supportUnknown,
  21. bool supportDuplicate) {
  22. MS_ASSERT(argv != nullptr);
  23. const int FLAG_PREFIX_LEN = 2;
  24. // Get binary name
  25. binName = GetFileName(argv[0]);
  26. std::multimap<std::string, Option<std::string>> keyValues;
  27. for (int i = 1; i < argc; i++) {
  28. std::string tmp = argv[i];
  29. Trim(&tmp);
  30. const std::string flagItem(tmp);
  31. if (flagItem == "--") {
  32. break;
  33. }
  34. if (flagItem.find("--") == std::string::npos) {
  35. continue;
  36. }
  37. std::string key;
  38. Option<std::string> value = Option<std::string>(None());
  39. size_t pos = flagItem.find_first_of("=");
  40. if (pos == std::string::npos && flagItem.find("--no-") != std::string::npos) {
  41. key = flagItem.substr(FLAG_PREFIX_LEN);
  42. } else if (pos == std::string::npos) {
  43. key = flagItem.substr(FLAG_PREFIX_LEN);
  44. } else {
  45. key = flagItem.substr(FLAG_PREFIX_LEN, pos - FLAG_PREFIX_LEN);
  46. value = Option<std::string>(flagItem.substr(pos + 1));
  47. }
  48. keyValues.insert(std::pair<std::string, Option<std::string>>(key, value));
  49. }
  50. Option<std::string> ret = Option<std::string>(InnerParseFlags(&keyValues));
  51. if (ret.IsSome()) {
  52. return Option<std::string>(ret.Get());
  53. }
  54. return Option<std::string>(None());
  55. }
  56. bool FlagParser::GetRealFlagName(const std::string &oriFlagName, std::string *flagName) {
  57. MS_ASSERT(flagName != nullptr);
  58. const int BOOL_TYPE_FLAG_PREFIX_LEN = 3;
  59. bool opaque = false;
  60. if (StartsWithPrefix(oriFlagName, "no-")) {
  61. *flagName = oriFlagName.substr(BOOL_TYPE_FLAG_PREFIX_LEN);
  62. opaque = true;
  63. } else {
  64. *flagName = oriFlagName;
  65. }
  66. return opaque;
  67. }
  68. // Inner parse function
  69. Option<std::string> FlagParser::InnerParseFlags(std::multimap<std::string, Option<std::string>> *keyValues) {
  70. MS_ASSERT(keyValues != nullptr);
  71. for (auto it = keyValues->begin(); it != keyValues->end(); ++it) {
  72. std::string flagName;
  73. bool opaque = GetRealFlagName((*it).first, &flagName);
  74. Option<std::string> flagValue = (*it).second;
  75. auto item = flags.find(flagName);
  76. if (item == flags.end()) {
  77. return Option<std::string>(std::string(flagName + " is not a valid flag"));
  78. }
  79. FlagInfo *flag = &(item->second);
  80. if (flag == nullptr) {
  81. return Option<std::string>("Failed: flag is nullptr");
  82. }
  83. if (flag->isParsed) {
  84. return Option<std::string>("Failed: already parsed flag: " + flagName);
  85. }
  86. std::string tmpValue;
  87. if (!flag->isBoolean) {
  88. if (opaque) {
  89. return Option<std::string>(flagName + " is not a boolean type");
  90. }
  91. if (flagValue.IsNone()) {
  92. return Option<std::string>("No value provided for non-boolean type: " + flagName);
  93. }
  94. tmpValue = flagValue.Get();
  95. } else {
  96. if (flagValue.IsNone() || flagValue.Get().empty()) {
  97. tmpValue = !opaque ? "true" : "false";
  98. } else if (!opaque) {
  99. tmpValue = flagValue.Get();
  100. } else {
  101. return Option<std::string>(std::string("Boolean flag can not have non-empty value"));
  102. }
  103. }
  104. // begin to parse value
  105. Option<Nothing> ret = flag->parse(this, tmpValue);
  106. if (ret.IsNone()) {
  107. return Option<std::string>("Failed to parse value for: " + flag->flagName);
  108. }
  109. flag->isParsed = true;
  110. }
  111. // to check flags not given in command line but added as in constructor
  112. for (auto &flag : flags) {
  113. if (flag.second.isRequired && !flag.second.isParsed) {
  114. return Option<std::string>("Error, value of '" + flag.first + "' not provided");
  115. }
  116. }
  117. return Option<std::string>(None());
  118. }
  119. void Replaceall(std::string *str, const std::string &oldValue, const std::string &newValue) {
  120. if (str == nullptr) {
  121. MS_LOGE("Input str is nullptr");
  122. return;
  123. }
  124. while (true) {
  125. std::string::size_type pos(0);
  126. if ((pos = str->find(oldValue)) != std::string::npos) {
  127. str->replace(pos, oldValue.length(), newValue);
  128. } else {
  129. break;
  130. }
  131. }
  132. }
  133. std::string FlagParser::Usage(const Option<std::string> &usgMsg) const {
  134. // first line, brief of the usage
  135. std::string usageString = usgMsg.IsSome() ? usgMsg.Get() + "\n" : "";
  136. // usage of bin name
  137. usageString += usageMsg.IsNone() ? "usage: " + binName + " [options]\n" : usageMsg.Get() + "\n";
  138. // help line of help message, usageLine:message of parametors
  139. std::string helpLine = "";
  140. std::string usageLine = "";
  141. uint32_t i = 0;
  142. for (auto flag = flags.begin(); flag != flags.end(); flag++) {
  143. std::string flagName = flag->second.flagName;
  144. std::string helpInfo = flag->second.helpInfo;
  145. // parameter line
  146. std::string thisLine = flag->second.isBoolean ? " --[no-]" + flagName : " --" + flagName + "=VALUE";
  147. if (++i < flags.size()) {
  148. // add paramter help message of each line
  149. thisLine += " " + helpInfo;
  150. Replaceall(&helpInfo, "\n\r", "\n");
  151. usageLine += thisLine + "\n";
  152. } else {
  153. // brief help message
  154. helpLine = thisLine + " " + helpInfo + "\n";
  155. }
  156. }
  157. // total usage is brief of usage+ brief of bin + help message + brief of
  158. // paramters
  159. return usageString + helpLine + usageLine;
  160. }
  161. } // namespace predict
  162. } // namespace mindspore