cmd域名解析命令,Linux glib命令行解析GOptionEntry使用

 2023-11-30 阅读 27 评论 0

摘要:1.安裝glib //安裝依賴庫 sudo apt-get install libffi-dev -y//安裝glib # sudo apt-get install libglib2.0-dev -y//64位ubuntu安裝32位glib2.0, 僅作參考 # sudo apt-get install libglib2.0-dev:i3862.test.c #include <glib.h> #include <locale.h> #define
1.安裝glib
//安裝依賴庫
sudo apt-get install libffi-dev -y//安裝glib
# sudo apt-get install libglib2.0-dev -y//64位ubuntu安裝32位glib2.0, 僅作參考
# sudo apt-get install libglib2.0-dev:i3862.test.c
#include <glib.h>
#include <locale.h>
#define ERR_MSG_V(msg, ...) \g_print("** ERROR: <%s:%d>: " msg "\n", __func__, __LINE__, ##__VA_ARGS__)static  gint repeats = 2;
static  gint max_size = 8;
static  gboolean verbose = FALSE;
static  gboolean beep = FALSE;
static  gboolean op_rand = FALSE;
static  gchar arg_data[32] = { "arg data" };static  GOptionEntry entries[] =
{{"long name" ,  's' /*short-name*/ , 0 /*flags*/ , G_OPTION_ARG_STRING /*arg*/ ,arg_data, "description" ,  "arg_description" },{"repeats" ,  'r' , 0, G_OPTION_ARG_INT, &repeats, "Average over N repetitions" ,  "N" },{"max-size" ,  'm' , 0, G_OPTION_ARG_INT, &max_size, "Test up to 2^M items" ,  "M" },{"verbose" ,  'v' , 0, G_OPTION_ARG_NONE, &verbose, "Be verbose" , NULL},{"beep" ,  'b' , 0, G_OPTION_ARG_NONE, &beep, "Beep when done" , NULL},{"rand" , 0, 0, G_OPTION_ARG_NONE, &op_rand, "Randomize the data" , NULL},{NULL}
};int main (int  argc,  char  *argv[])
{GError *error = NULL;GOptionContext *context = NULL;GOptionGroup *group = NULL;// 創建一個新的選項上下文context = g_option_context_new("- test tree model performance" );
#if 1// 如果主要組不存在則創建主要組,向組添加entries并設置轉換域g_option_context_add_main_entries(context, entries, NULL);//添加要在選項列表之前的--help輸出中顯示的字符串。 這通常是程序功能的摘要g_option_context_set_summary(context, "This is a glib demo" );
#elsegroup = g_option_group_new ("abc", NULL, NULL, NULL, NULL);g_option_group_add_entries (group, entries);g_option_context_set_main_group (context, group);//  g_option_context_add_group(context, gtk_get_option_group(TRUE));
#endif// 解析命令行參數,識別已添加到上下文的選項if  (!g_option_context_parse(context, &argc, &argv, &error)){ERR_MSG_V("%s", error->message);exit (1);}// 釋放被解析的參數g_option_context_free(context);g_print("Now value is: repeats=%d, max_size=%d, verbose=%d, beep=%d\n",repeats, max_size, verbose, beep);return  0;
}3.編譯
# gcc test.c -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include \
-L/usr/lib/x86_64-linux-gnu -lglib-2.0 -o test4.run測試
# ./test -h
Usage:test [OPTION?] - test tree model performanceThis is a glib demoHelp Options:-h, --help                          Show help optionsApplication Options:-s, --long name=arg_description     description-r, --repeats=N                     Average over N repetitions-m, --max-size=M                    Test up to 2^M items-v, --verbose                       Be verbose-b, --beep                          Beep when done--rand                              Randomize the data# ./test -r 66 -m 6
Now value is: repeats=66, max_size=6, verbose=0, beep=05.GOptionEntry結構體定義:
typedef struct GOptionEntry {const gchar *long_name;		// 參數的完整名 gchar        short_name;		// 簡寫名   gint         flags;			// 參數選項標準,如果不關心可直接賦0 GOptionArg   arg;				// 參數類型,int,string... gpointer     arg_data;		// 默 認參數值  const gchar *description;		// 參數意義說明  const gchar *arg_description;	// 參數占位符說明   
}GOptionEntry;

glib命令行解參考

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/185455.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息