main.cpp 3.2 KB

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