main.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 << " tars2cpp support type: bool byte short int long float double vector map" << endl;
  33. exit(0);
  34. }
  35. void check(vector<string> &vTars)
  36. {
  37. for(size_t i = 0; i < vTars.size(); i++)
  38. {
  39. string ext = tars::TC_File::extractFileExt(vTars[i]);
  40. if(ext == "tars")
  41. {
  42. if(!tars::TC_File::isFileExist(vTars[i]))
  43. {
  44. cerr << "file '" << vTars[i] << "' not exists" << endl;
  45. usage();
  46. exit(0);
  47. }
  48. }
  49. else
  50. {
  51. cerr << "only support tars file." << endl;
  52. exit(0);
  53. }
  54. }
  55. }
  56. int main(int argc, char* argv[])
  57. {
  58. if(argc < 2)
  59. {
  60. usage();
  61. }
  62. tars::TC_Option option;
  63. option.decode(argc, argv);
  64. vector<string> vTars = option.getSingle();
  65. check(vTars);
  66. if(option.hasParam("help"))
  67. {
  68. usage();
  69. }
  70. // bool bCoder = option.hasParam("coder");
  71. // vector<string> vCoder;
  72. // if(bCoder)
  73. // {
  74. // vCoder = tars::TC_Common::sepstr<string>(option.getValue("coder"), ";", false);
  75. // if(vCoder.size() == 0)
  76. // {
  77. // usage();
  78. // return 0;
  79. // }
  80. // }
  81. Tars2Cpp t2c;
  82. if (option.hasParam("dir"))
  83. {
  84. t2c.setBaseDir(option.getValue("dir"));
  85. }
  86. else
  87. {
  88. t2c.setBaseDir(".");
  89. }
  90. t2c.setCheckDefault(tars::TC_Common::lower(option.getValue("check-default")) == "false"?false:true);
  91. t2c.setOnlyStruct(option.hasParam("os"));
  92. //默认支持json
  93. t2c.setJsonSupport(true);
  94. if (option.hasParam("unjson"))
  95. {
  96. t2c.setJsonSupport(false);
  97. }
  98. if (option.hasParam("sql"))
  99. {
  100. t2c.setSqlSupport(true);
  101. t2c.setJsonSupport(true);
  102. }
  103. if (option.hasParam("xml"))
  104. {
  105. vector<string> vXmlIntf;
  106. string sXml = tars::TC_Common::trim(option.getValue("xml"));
  107. sXml = tars::TC_Common::trimleft(tars::TC_Common::trimright(sXml, "]"), "[");
  108. if (!sXml.empty())
  109. {
  110. vXmlIntf = tars::TC_Common::sepstr<string>(sXml, ",", false);
  111. }
  112. t2c.setXmlSupport(true, vXmlIntf);
  113. }
  114. // if (option.hasParam("json"))
  115. // {
  116. // t2c.setJsonSupport(true);
  117. // string sJson = tars::TC_Common::trim(option.getValue("json"));
  118. // sJson = tars::TC_Common::trimleft(tars::TC_Common::trimright(sJson, "]"), "[");
  119. // if (!sJson.empty())
  120. // {
  121. // t2c.setJsonSupport(tars::TC_Common::sepstr<string>(sJson, ",", false));
  122. // }
  123. // }
  124. t2c.setTarsMaster(option.hasParam("tarsMaster"));
  125. try
  126. {
  127. //增加include搜索路径
  128. g_parse->addIncludePath(option.getValue("include"));
  129. //是否可以以tars开头
  130. g_parse->setTars(option.hasParam("with-tars"));
  131. g_parse->setHeader(option.getValue("header"));
  132. g_parse->setCurrentPriority(option.hasParam("currentPriority"));
  133. // t2c.setUnknownField(option.hasParam("unknown"));
  134. for(size_t i = 0; i < vTars.size(); i++)
  135. {
  136. g_parse->parse(vTars[i]);
  137. t2c.createFile(vTars[i]);//, vCoder);
  138. }
  139. }
  140. catch(exception& e)
  141. {
  142. cerr<<e.what()<<endl;
  143. }
  144. return 0;
  145. }