argument.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. Args args = {.out_file=NULL, .error_file=NULL, .in_file=NULL, .run_commandLine=true};
  18. /**
  19. * 参数设置, args是全局结构体, 保存全局的参数设置
  20. * @param argc
  21. * @param argv
  22. * @return
  23. */
  24. int getArgs(const int argc, char **argv)
  25. {
  26. int opt;
  27. opterr = true;
  28. while((opt=getopt_long(argc, argv, short_option ,long_option,NULL))!=-1)
  29. {
  30. switch(opt)
  31. {
  32. case 0:
  33. break;
  34. case 'o':
  35. args.out_file = memStrcpy(optarg);
  36. break;
  37. case 'e':
  38. args.error_file = memStrcpy(optarg);
  39. break;
  40. case 'i':
  41. args.in_file = memStrcpy(optarg);
  42. break;
  43. case 'n':
  44. args.run_commandLine = false;
  45. break;
  46. case '?':
  47. fprintf(stderr, "[Error]: get not success args : -%c\n", (char)optopt);
  48. return -1;
  49. default:
  50. break;
  51. }
  52. }
  53. return 0;
  54. }
  55. /**
  56. * 释放args的成员而不包括其本身
  57. */
  58. void freeArgs(void){
  59. memFree(args.out_file);
  60. memFree(args.error_file);
  61. memFree(args.in_file);
  62. }