main.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "mainwindow.h"
  2. #include <QApplication>
  3. #include "ivlog.h"
  4. iv::Ivlog * givlog;
  5. #include <getopt.h>
  6. #include <iostream>
  7. #include <QTranslator>
  8. char gstr_memname[256];
  9. void print_useage()
  10. {
  11. std::cout<<" -m --memname $mappath : share memory name. eq. -m lidar_pc"<<std::endl;
  12. std::cout<<" -h --help print help"<<std::endl;
  13. }
  14. int GetOptLong(int argc, char *argv[]) {
  15. int nRtn = 0;
  16. int opt; // getopt_long() 的返回值
  17. int digit_optind = 0; // 设置短参数类型及是否需要参数
  18. (void)digit_optind;
  19. // 如果option_index非空,它指向的变量将记录当前找到参数符合long_opts里的
  20. // 第几个元素的描述,即是long_opts的下标值
  21. int option_index = 0;
  22. // 设置短参数类型及是否需要参数
  23. const char *optstring = "m:h";
  24. // 设置长参数类型及其简写,比如 --reqarg <==>-r
  25. /*
  26. struct option {
  27. const char * name; // 参数的名称
  28. int has_arg; // 是否带参数值,有三种:no_argument, required_argument,optional_argument
  29. int * flag; // 为空时,函数直接将 val 的数值从getopt_long的返回值返回出去,
  30. // 当非空时,val的值会被赋到 flag 指向的整型数中,而函数返回值为0
  31. int val; // 用于指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值
  32. };
  33. 其中:
  34. no_argument(即0),表明这个长参数不带参数(即不带数值,如:--name)
  35. required_argument(即1),表明这个长参数必须带参数(即必须带数值,如:--name Bob)
  36. optional_argument(即2),表明这个长参数后面带的参数是可选的,(即--name和--name Bob均可)
  37. */
  38. static struct option long_options[] = {
  39. {"memname", required_argument, NULL, 'm'},
  40. {"help", no_argument, NULL, 'h'},
  41. // {"optarg", optional_argument, NULL, 'o'},
  42. {0, 0, 0, 0} // 添加 {0, 0, 0, 0} 是为了防止输入空值
  43. };
  44. while ( (opt = getopt_long(argc,
  45. argv,
  46. optstring,
  47. long_options,
  48. &option_index)) != -1) {
  49. // printf("opt = %c\n", opt); // 命令参数,亦即 -a -b -n -r
  50. // printf("optarg = %s\n", optarg); // 参数内容
  51. // printf("optind = %d\n", optind); // 下一个被处理的下标值
  52. // printf("argv[optind - 1] = %s\n", argv[optind - 1]); // 参数内容
  53. // printf("option_index = %d\n", option_index); // 当前打印参数的下标值
  54. // printf("\n");
  55. switch(opt)
  56. {
  57. case 'm':
  58. strncpy(gstr_memname,optarg,255);
  59. break;
  60. case 'h':
  61. print_useage();
  62. nRtn = 1; //because use -h
  63. break;
  64. default:
  65. break;
  66. }
  67. }
  68. return nRtn;
  69. }
  70. int main(int argc, char *argv[])
  71. {
  72. givlog = new iv::Ivlog("map_collectfromveh");
  73. QApplication a(argc, argv);
  74. QTranslator ts;
  75. if(ts.load("./map_collectfromveh_language.qm"))
  76. {
  77. a.installTranslator(&ts);
  78. }
  79. snprintf(gstr_memname,255,"hcp2_gpsimu");
  80. int nRtn = GetOptLong(argc,argv);
  81. if(nRtn == 1) //show help,so exit.
  82. {
  83. return 0;
  84. }
  85. MainWindow w;
  86. w.show();
  87. return a.exec();
  88. }