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 5.9 kB

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