main.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "tars2c.h"
  20. void usage()
  21. {
  22. cout << "Usage : tars2c [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 << " --check-default<true, false> optional field with default value not do package(default: true)" << endl;
  25. cout << " tars2c 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. exit(0);
  46. }
  47. }
  48. }
  49. int main(int argc, char* argv[])
  50. {
  51. if(argc < 2)
  52. {
  53. usage();
  54. }
  55. tars::TC_Option option;
  56. option.decode(argc, argv);
  57. vector<string> vTars = option.getSingle();
  58. check(vTars);
  59. if(option.hasParam("help"))
  60. {
  61. usage();
  62. }
  63. Tars2C j2c;
  64. if (option.hasParam("dir"))
  65. {
  66. j2c.setBaseDir(option.getValue("dir"));
  67. }
  68. else
  69. {
  70. j2c.setBaseDir(".");
  71. }
  72. j2c.setCheckDefault(tars::TC_Common::lower(option.getValue("check-default")) == "false"?false:true);
  73. try
  74. {
  75. // 是否可以以tars开头
  76. g_parse->setTars(option.hasParam("with-tars"));
  77. g_parse->setHeader(option.getValue("header"));
  78. for(size_t i = 0; i < vTars.size(); i++)
  79. {
  80. g_parse->parse(vTars[i]);
  81. j2c.createFile(vTars[i]);
  82. }
  83. }catch(exception& e)
  84. {
  85. cerr<<e.what()<<endl;
  86. }
  87. return 0;
  88. }