main.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "tars2cs.h"
  19. void usage()
  20. {
  21. cout << "Usage : tars2cs [OPTION] tarsfile" << endl;
  22. cout << " tars2cs support type: bool byte short int long float double vector map" << endl;
  23. cout << "supported [OPTION]:" << endl;
  24. cout << " --help help,print this" << endl;
  25. cout << " --dir=DIRECTORY generate source file to DIRECTORY(create tars protocol file to DIRECTORY, default is current directory)" << endl;
  26. cout << " --base-package=NAME package prefix, default 'com.qq.'" << endl;
  27. cout << " --with-servant also generate servant class" << endl;
  28. cout << endl;
  29. exit(0);
  30. }
  31. void check(vector<string> &vTars)
  32. {
  33. for(size_t i = 0; i < vTars.size(); i++)
  34. {
  35. string ext = tars::TC_File::extractFileExt(vTars[i]);
  36. if(ext == "tars")
  37. {
  38. if(!tars::TC_File::isFileExist(vTars[i]))
  39. {
  40. cerr << "file '" << vTars[i] << "' not exists" << endl;
  41. usage();
  42. exit(0);
  43. }
  44. }
  45. else
  46. {
  47. cerr << "only support tars file." << endl;
  48. exit(0);
  49. }
  50. }
  51. }
  52. int main(int argc, char* argv[])
  53. {
  54. if(argc < 2)
  55. {
  56. usage();
  57. }
  58. tars::TC_Option option;
  59. option.decode(argc, argv);
  60. vector<string> vTars = option.getSingle();
  61. check(vTars);
  62. if(option.hasParam("help"))
  63. {
  64. usage();
  65. }
  66. Tars2Cs t2cs;
  67. if(option.getValue("dir") != "")
  68. {
  69. t2cs.setBaseDir(option.getValue("dir"));
  70. }
  71. else
  72. {
  73. t2cs.setBaseDir(".");
  74. }
  75. //����ǰ׺
  76. if(option.hasParam("base-package"))
  77. {
  78. t2cs.setBasePackage(option.getValue("base-package"));
  79. }
  80. else
  81. {
  82. t2cs.setBasePackage("Com.QQ.");
  83. }
  84. if(option.hasParam("with-servant"))
  85. {
  86. t2cs.setWithServant(true);
  87. }
  88. else
  89. {
  90. t2cs.setWithServant(false);
  91. }
  92. try
  93. {
  94. g_parse->setTars(option.hasParam("with-tars"));
  95. for(size_t i = 0; i < vTars.size(); i++)
  96. {
  97. g_parse->parse(vTars[i]);
  98. t2cs.createFile(vTars[i]);
  99. }
  100. }catch(exception& e)
  101. {
  102. cerr<<e.what()<<endl;
  103. }
  104. return 0;
  105. }