main.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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_file.h"
  17. #include "util/tc_option.h"
  18. #include "tars2case.h"
  19. void usage()
  20. {
  21. cout << "Usage : tars2case [OPTION] tarsfile" << endl;
  22. cout << " --json generate json casefile" << endl;
  23. cout << " --web generate webadmin casefile" << endl;
  24. cout << " --dir=DIRECTORY generate casefile to DIRECTORY" << endl;
  25. cout << endl;
  26. exit(100);
  27. }
  28. void check(vector<string> &vTars)
  29. {
  30. for(size_t i = 0; i < vTars.size(); i++)
  31. {
  32. string ext = TC_File::extractFileExt(vTars[i]);
  33. if(ext == "tars")
  34. {
  35. if(!TC_File::isFileExist(vTars[i]))
  36. {
  37. cerr << "file '" << vTars[i] << "' not exists" << endl;
  38. usage();
  39. }
  40. }
  41. else
  42. {
  43. cerr << "only support tars file." << endl;
  44. exit(100);
  45. }
  46. }
  47. }
  48. void doTars2Test(TC_Option& option, const vector<string>& vTars)
  49. {
  50. Tars2Case j2t;
  51. j2t.setJsonCase(option.hasParam("json") ? true : false);
  52. j2t.setWebSupport(option.hasParam("web") ? true : false);
  53. j2t.setBaseDir(option.getValue("dir").empty() ? "" : option.getValue("dir"));
  54. g_parse->setUseCurrentPath(true);
  55. for(size_t i = 0; i < vTars.size(); i++)
  56. {
  57. g_parse->parse(vTars[i]);
  58. j2t.createFile(vTars[i], option.getValue("out"));
  59. }
  60. }
  61. int main(int argc, char* argv[]){
  62. if (argc < 2)
  63. {
  64. usage();
  65. return 100;
  66. }
  67. try
  68. {
  69. TC_Option option;
  70. option.decode(argc, argv);
  71. vector<string> vTars = option.getSingle();
  72. check(vTars);
  73. ::chdir(TC_File::extractFilePath(vTars[0]).c_str());
  74. if (option.hasParam("help"))
  75. {
  76. usage();
  77. }
  78. doTars2Test(option, vTars);
  79. }
  80. catch(exception& e)
  81. {
  82. cerr<<e.what()<<endl;
  83. }
  84. return 0;
  85. }