main.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "tars2php.h"
  20. void usage()
  21. {
  22. cout << "Usage : tars2php [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 << " tars2php support type: bool byte short int long float double vector map" << endl;
  27. exit(0);
  28. }
  29. void check(vector<string> &vTars)
  30. {
  31. for(size_t i = 0; i < vTars.size(); i++)
  32. {
  33. string ext = tars::TC_File::extractFileExt(vTars[i]);
  34. if(ext == "tars")
  35. {
  36. if(!tars::TC_File::isFileExist(vTars[i]))
  37. {
  38. cerr << "file '" << vTars[i] << "' not exists" << endl;
  39. usage();
  40. exit(0);
  41. }
  42. }
  43. else
  44. {
  45. cerr << "only support tars file." << endl;
  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. bool bCoder = option.hasParam("coder");
  65. vector<string> vCoder;
  66. if(bCoder)
  67. {
  68. vCoder = tars::TC_Common::sepstr<string>(option.getValue("coder"), ";", false);
  69. if(vCoder.size() == 0)
  70. {
  71. usage();
  72. return 0;
  73. }
  74. }
  75. Tars2Php j2p;
  76. if (option.hasParam("dir"))
  77. {
  78. j2p.setBaseDir(option.getValue("dir"));
  79. }
  80. else
  81. {
  82. j2p.setBaseDir(".");
  83. }
  84. j2p.setCheckDefault(tars::TC_Common::lower(option.getValue("check-default")) == "false"?false:true);
  85. try
  86. {
  87. //�Ƿ������tars��ͷ
  88. g_parse->setTars(option.hasParam("with-tars"));
  89. g_parse->setHeader(option.getValue("header"));
  90. for(size_t i = 0; i < vTars.size(); i++)
  91. {
  92. g_parse->parse(vTars[i]);
  93. j2p.createFile(vTars[i], vCoder);
  94. }
  95. }catch(exception& e)
  96. {
  97. cerr<<e.what()<<endl;
  98. }
  99. return 0;
  100. }