main.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "tars2oc.h"
  20. void usage()
  21. {
  22. cout << "Usage : tars2oc [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 << " --arc create arc" << endl;
  25. cout << " --nonamespace no namespace" << endl;
  26. cout << " --with-namespace=NAME set namespace" << endl;
  27. cout << " tars2oc support type: bool byte short int long float double vector map" << endl;
  28. exit(0);
  29. }
  30. void check(vector<string> &vTars)
  31. {
  32. for(size_t i = 0; i < vTars.size(); i++)
  33. {
  34. string ext = tars::TC_File::extractFileExt(vTars[i]);
  35. if(ext == "tars")
  36. {
  37. if(!tars::TC_File::isFileExist(vTars[i]))
  38. {
  39. cerr << "file '" << vTars[i] << "' not exists" << endl;
  40. usage();
  41. exit(0);
  42. }
  43. }
  44. else
  45. {
  46. cerr << "only support tars file." << endl;
  47. exit(0);
  48. }
  49. }
  50. }
  51. int main(int argc, char* argv[])
  52. {
  53. if(argc < 2)
  54. {
  55. usage();
  56. }
  57. tars::TC_Option option;
  58. option.decode(argc, argv);
  59. vector<string> vTars = option.getSingle();
  60. check(vTars);
  61. if(option.hasParam("help"))
  62. {
  63. usage();
  64. }
  65. //????????tars???
  66. g_parse->setTars(option.hasParam("with-tars"));
  67. g_parse->setHeader(option.getValue("header"));
  68. Tars2OC j2c;
  69. if (option.hasParam("dir"))
  70. {
  71. j2c.setBaseDir(option.getValue("dir"));
  72. }
  73. else
  74. {
  75. j2c.setBaseDir(".");
  76. }
  77. if (option.hasParam("arc"))
  78. {
  79. j2c.setArc(true);
  80. }
  81. else
  82. {
  83. j2c.setArc(false);
  84. }
  85. if(option.hasParam("nonamespace"))
  86. {
  87. j2c.setNeedNS(false);
  88. }
  89. else
  90. {
  91. j2c.setNeedNS(true);
  92. j2c.setNS(option.getValue("with-namespace"));
  93. }
  94. try
  95. {
  96. for(size_t i = 0; i < vTars.size(); i++)
  97. {
  98. g_parse->parse(vTars[i]);
  99. j2c.createFile(vTars[i]);
  100. }
  101. }catch(exception& e)
  102. {
  103. cerr<<e.what()<<endl;
  104. }
  105. return 0;
  106. }