main.cpp 2.5 KB

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