argument.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "hellovm.h"
  2. char *HelloString = "Welcome To VirtualMath ("__TIME__", "__DATE__") \n"
  3. #ifdef __linux__
  4. "On Linux\n"
  5. #else
  6. "On windows"
  7. #endif
  8. "VirtualMath Command Line Mode\n";
  9. static const struct option long_option[]={
  10. {"stderr",required_argument,NULL,'o'},
  11. {"stdout",required_argument,NULL,'e'},
  12. {"stdin",required_argument,NULL,'i'},
  13. {"not-run-cl",required_argument,NULL,'n'},
  14. {NULL,0,NULL,0}
  15. };
  16. static const char *short_option = "o:e:i:n";
  17. /**
  18. * 参数设置, args是全局结构体, 保存全局的参数设置
  19. * @param argc
  20. * @param argv
  21. * @return
  22. */
  23. int getArgs(const int argc, char **argv)
  24. {
  25. args.out_file = NULL;
  26. args.error_file = NULL;
  27. args.in_file = NULL;
  28. args.run_commandLine = true;
  29. opterr = true;
  30. int opt;
  31. while((opt=getopt_long(argc, argv, short_option ,long_option,NULL))!=-1)
  32. {
  33. switch(opt)
  34. {
  35. case 0:
  36. break;
  37. case 'o':
  38. args.out_file = memStrcpy(optarg);
  39. break;
  40. case 'e':
  41. args.error_file = memStrcpy(optarg);
  42. break;
  43. case 'i':
  44. args.in_file = memStrcpy(optarg);
  45. break;
  46. case 'n':
  47. args.run_commandLine = false;
  48. break;
  49. case '?':
  50. fprintf(stderr, "[Error]: get not success args : -%c\n", (char)optopt);
  51. return -1;
  52. default:
  53. break;
  54. }
  55. }
  56. return 0;
  57. }
  58. /**
  59. * 释放args的成员而不包括其本身
  60. */
  61. void freeArgs(void){
  62. memFree(args.out_file);
  63. memFree(args.error_file);
  64. memFree(args.in_file);
  65. }