main.cpp 3.3 KB

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