tc_option.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "util/tc_option.h"
  17. #include "util/tc_common.h"
  18. namespace tars
  19. {
  20. void TC_Option::decode(int argc, char *argv[])
  21. {
  22. _mParam.clear();
  23. vector<string> v;
  24. for(int i = 1; i < argc; i++)
  25. {
  26. v.push_back(argv[i]);
  27. }
  28. for(size_t i = 0; i < v.size(); i++)
  29. {
  30. if(v[i].length() > 2 && v[i].substr(0,2) == "--")
  31. {
  32. parse(v[i]);
  33. }
  34. else
  35. {
  36. _vSingle.push_back(v[i]);
  37. }
  38. }
  39. }
  40. void TC_Option::decode(const char *command)
  41. {
  42. _mParam.clear();
  43. if(command == NULL)
  44. return;
  45. vector<string> v = TC_Common::sepstr<string>(command, " \t");
  46. for(size_t i = 0; i < v.size(); i++)
  47. {
  48. if(v[i].length() > 2 && v[i].substr(0,2) == "--")
  49. {
  50. parse(v[i]);
  51. }
  52. else
  53. {
  54. _vSingle.push_back(v[i]);
  55. }
  56. }
  57. }
  58. void TC_Option::parse(const string &s)
  59. {
  60. string::size_type pos = s.find('=');
  61. if( pos != string::npos)
  62. {
  63. _mParam[s.substr(2, pos-2)] = s.substr(pos+1);
  64. }
  65. else
  66. {
  67. _mParam[s.substr(2, pos-2)] = "";
  68. }
  69. }
  70. string TC_Option::getValue(const string &sName, const string &def) const
  71. {
  72. auto it = _mParam.find(sName);
  73. if( it != _mParam.end())
  74. {
  75. return it->second;
  76. }
  77. return def;
  78. }
  79. bool TC_Option::hasParam(const string &sName) const
  80. {
  81. return _mParam.find(sName) != _mParam.end();
  82. }
  83. const vector<string>& TC_Option::getSingle() const
  84. {
  85. return _vSingle;
  86. }
  87. const map<string, string>& TC_Option::getMulti() const
  88. {
  89. return _mParam;
  90. }
  91. vector<string>& TC_Option::getSingle()
  92. {
  93. return _vSingle;
  94. }
  95. map<string, string>& TC_Option::getMulti()
  96. {
  97. return _mParam;
  98. }
  99. }