1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
   | static gboolean opt_hugepage = FALSE; static gint opt_size = 128;
  /*  * GOptionEntry定义一个输入参数项。每项的定义依次是:长参数名字,短参数名字,flag,  * 参数类型,参数存放地址,参数项描述,参数描述。  *  * 简单使用时,flag用G_OPTION_FLAG_NONE就好,最后一个参数描述会在长参数后输出,  * 可以参看最下面的--help输出。  */ static GOptionEntry entries[] = { 	{ "size", 's', G_OPTION_FLAG_NONE, G_OPTION_ARG_INT, &opt_size, 	  "size of dma copy in normal page case", "size" }, 	{ "hugepage", 'h', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &opt_hugepage, 	  "use hugepage(one 2M page for src, one for dts)", NULL }, 	{ NULL } };
  static void handle_options(int argc, char *argv[]) { 	GError *err = NULL; 	/* 参数项描述的上下文 */ 	GOptionContext *context;
  	/* 创建参数项描述的上下文 */ 	context = g_option_context_new("- test devmmu pasid");
  	/* 把如上定义的各个参数项放到context中 */ 	g_option_context_add_main_entries(context, entries, NULL);
  	/* 调用这个函数解析输入值 */ 	g_option_context_parse(context, &argc, &argv, &err);
  	/* 释放context */ 	g_option_context_free(context); }
  int main(int argc, char *argv[]) { 	handle_options(argc, argv);
  	/* ... */ }
   |