main.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_file.h"
  18. #include "util/tc_common.h"
  19. #include "tars2cpp.h"
  20. void usage()
  21. {
  22. cout << "Usage : tars2cpp [OPTION] tarsfile" << endl;
  23. // cout << " --coder=Demo::interface1;Demo::interface2 create interface encode and decode api" << endl;
  24. cout << " --dir=DIRECTORY generate source file to DIRECTORY(create tars protocol file to DIRECTORY, default is current directory)" << endl;
  25. cout << " --check-default=<true,false> optional field with default value not do package(default: true)" << endl;
  26. cout << " --unjson not json interface" << endl;
  27. cout << " --os only create struct(not create interface) " << endl;
  28. cout << " --include=\"dir1;dir2;dir3\" set search path of tars protocol" << endl;
  29. // cout << " --unknown create unkown field" << endl;
  30. cout << " --tarsMaster create get registry info interface" << endl;
  31. cout << " --currentPriority use current path first." << endl;
  32. cout << " --without-trace 不需要调用链追踪逻辑" << endl;
  33. cout << " tars2cpp support type: bool byte short int long float double vector map" << endl;
  34. exit(0);
  35. }
  36. void check(vector<string> &vTars)
  37. {
  38. for(size_t i = 0; i < vTars.size(); i++)
  39. {
  40. string ext = tars::TC_File::extractFileExt(vTars[i]);
  41. if(ext == "tars")
  42. {
  43. if(!tars::TC_File::isFileExist(vTars[i]))
  44. {
  45. cerr << "file '" << vTars[i] << "' not exists" << endl;
  46. usage();
  47. exit(0);
  48. }
  49. }
  50. else
  51. {
  52. cerr << "only support tars file." << endl;
  53. exit(0);
  54. }
  55. }
  56. }
  57. int main(int argc, char* argv[])
  58. {
  59. if(argc < 2)
  60. {
  61. usage();
  62. }
  63. tars::TC_Option option;
  64. option.decode(argc, argv);
  65. vector<string> vTars = option.getSingle();
  66. check(vTars);
  67. if(option.hasParam("help"))
  68. {
  69. usage();
  70. }
  71. // bool bCoder = option.hasParam("coder");
  72. // vector<string> vCoder;
  73. // if(bCoder)
  74. // {
  75. // vCoder = tars::TC_Common::sepstr<string>(option.getValue("coder"), ";", false);
  76. // if(vCoder.size() == 0)
  77. // {
  78. // usage();
  79. // return 0;
  80. // }
  81. // }
  82. Tars2Cpp t2c;
  83. if (option.hasParam("dir"))
  84. {
  85. t2c.setBaseDir(option.getValue("dir"));
  86. }
  87. else
  88. {
  89. t2c.setBaseDir(".");
  90. }
  91. t2c.setCheckDefault(tars::TC_Common::lower(option.getValue("check-default")) == "false"?false:true);
  92. t2c.setOnlyStruct(option.hasParam("os"));
  93. //默认支持json
  94. t2c.setJsonSupport(true);
  95. if (option.hasParam("unjson"))
  96. {
  97. t2c.setJsonSupport(false);
  98. }
  99. if (option.hasParam("sql"))
  100. {
  101. t2c.setSqlSupport(true);
  102. t2c.setJsonSupport(true);
  103. }
  104. // 调用链追踪
  105. if (option.hasParam("with-trace"))
  106. {
  107. t2c.setTrace(true);
  108. }
  109. else
  110. {
  111. t2c.setTrace(false);
  112. }
  113. if (option.hasParam("xml"))
  114. {
  115. vector<string> vXmlIntf;
  116. string sXml = tars::TC_Common::trim(option.getValue("xml"));
  117. sXml = tars::TC_Common::trimleft(tars::TC_Common::trimright(sXml, "]"), "[");
  118. if (!sXml.empty())
  119. {
  120. vXmlIntf = tars::TC_Common::sepstr<string>(sXml, ",", false);
  121. }
  122. t2c.setXmlSupport(true, vXmlIntf);
  123. }
  124. // if (option.hasParam("json"))
  125. // {
  126. // t2c.setJsonSupport(true);
  127. // string sJson = tars::TC_Common::trim(option.getValue("json"));
  128. // sJson = tars::TC_Common::trimleft(tars::TC_Common::trimright(sJson, "]"), "[");
  129. // if (!sJson.empty())
  130. // {
  131. // t2c.setJsonSupport(tars::TC_Common::sepstr<string>(sJson, ",", false));
  132. // }
  133. // }
  134. t2c.setTarsMaster(option.hasParam("tarsMaster"));
  135. try
  136. {
  137. //增加include搜索路径
  138. g_parse->addIncludePath(option.getValue("include"));
  139. //是否可以以tars开头
  140. g_parse->setTars(option.hasParam("with-tars"));
  141. g_parse->setHeader(option.getValue("header"));
  142. g_parse->setCurrentPriority(option.hasParam("currentPriority"));
  143. // t2c.setUnknownField(option.hasParam("unknown"));
  144. for(size_t i = 0; i < vTars.size(); i++)
  145. {
  146. g_parse->parse(vTars[i]);
  147. t2c.createFile(vTars[i]);//, vCoder);
  148. }
  149. }
  150. catch(exception& e)
  151. {
  152. cerr<<e.what()<<endl;
  153. }
  154. return 0;
  155. }