tc_option.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #ifndef __TC_OPTION_H
  17. #define __TC_OPTION_H
  18. #include <map>
  19. #include <vector>
  20. #include <string>
  21. #include <sstream>
  22. using namespace std;
  23. namespace tars
  24. {
  25. /////////////////////////////////////////////////
  26. /**
  27. * @file tc_option.h
  28. * @brief 命令行参数解析类.
  29. *
  30. */
  31. /////////////////////////////////////////////////
  32. /**
  33. * @brief 命令解析类,通常用于解析命令行参数
  34. *
  35. * 支持以下形式的参数: ./main.exe --name=value --with abc def
  36. */
  37. class TC_Option
  38. {
  39. public:
  40. /**
  41. * @brief 构造函数
  42. */
  43. TC_Option(){};
  44. /**
  45. * @brief 解码.
  46. *
  47. * @param argc 参数个数
  48. * @param argv 参数数组
  49. *
  50. */
  51. void decode(int argc, char *argv[]);
  52. /**
  53. * @brief 解码(和上面decode的区别是: 只有命令, 没有签名的argv[0])
  54. *
  55. * @param command 命令
  56. *
  57. */
  58. void decode(const char *command);
  59. /**
  60. * @brief 是否存在某个--标识的参数.
  61. *
  62. * @param sName 要判断的标识
  63. * @return bool 存在返回true,否则返回false
  64. */
  65. bool hasParam(const string &sName) const;
  66. /**
  67. * @brief 获取某个--表示的参数,如果参数不存在或者参数值为空 ,
  68. * 都返回""
  69. * @param sName 标识
  70. * @return string 标识的参数值
  71. */
  72. string getValue(const string &sName) const;
  73. /**
  74. * @brief 获取所有--标识的参数.
  75. *
  76. * @return map<string,string> map类型的标识和参数值的对应关系
  77. */
  78. const map<string, string>& getMulti() const;
  79. /**
  80. * @brief 获取所有普通的参数, 例子中的abc,
  81. * def,参数按照顺序在vector中
  82. * @return vector<string> 顺序存放参数的vector
  83. */
  84. const vector<string>& getSingle() const;
  85. /**
  86. * @brief 获取所有--标识的参数.
  87. *
  88. * @return map<string,string> map类型的标识和参数值的对应关系
  89. */
  90. map<string, string>& getMulti();
  91. /**
  92. * @brief 获取所有普通的参数, 例子中的abc,
  93. * def,参数按照顺序在vector中
  94. * @return vector<string> 顺序存放参数的vector
  95. */
  96. vector<string>& getSingle();
  97. protected:
  98. /**
  99. * @brief 解析字符串,取出标识和其对应的参数值,
  100. * 对型如--name=value 的字符串进行解析,取出name和vaue
  101. * @param s 要解析的字符串
  102. */
  103. void parse(const string &s);
  104. protected:
  105. /**
  106. *存放标识和其对应参数的对应关系,例如:对于--name=value,存放name和value
  107. */
  108. map<string, string> _mParam;
  109. /**
  110. *存放普通参数的vetor
  111. */
  112. vector<string> _vSingle;
  113. };
  114. }
  115. #endif