{"id":1632,"date":"2024-10-31T17:00:13","date_gmt":"2024-10-31T09:00:13","guid":{"rendered":"https:\/\/www.madbull.site\/?p=1632"},"modified":"2024-11-21T17:00:44","modified_gmt":"2024-11-21T09:00:44","slug":"glib%e8%a7%a3%e6%9e%90%e5%91%bd%e4%bb%a4%e8%a1%8c%e5%8f%82%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=1632","title":{"rendered":"GLIB\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570"},"content":{"rendered":"\n<p>glib\u5b98\u65b9\u6587\u6863\u53c2\u770b\uff1a<a href=\"https:\/\/docs.gtk.org\/glib\/struct.OptionContext.html\">https:\/\/docs.gtk.org\/glib\/struct.OptionContext.html<\/a><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u9996\u5148\uff0c\u5728C\u4ee3\u7801\u4e2d\u5b9a\u4e49\u4e00\u4e2a GOptionEntry \u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u4f7f\u7528 g_option_context_add_main_entries \u548c g_option_context_parse \u7b49\u51fd\u6570\uff0c\u8ba9\u547d\u4ee4\u884c\u6309\u7167\u6b64\u6570\u7ec4\u8fdb\u884c\u89e3\u6790\u3002GOptionEntry \u7684\u5b9a\u4e49\u548c\u8bf4\u660e\u53c2\u8003\u5b98\u65b9\u6587\u6863\uff1a<a href=\"https:\/\/docs.gtk.org\/glib\/struct.OptionEntry.html\">https:\/\/docs.gtk.org\/glib\/struct.OptionEntry.html<\/a><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u53c2\u6570\u89e3\u6790\u7c7b\u578b\u5b9a\u4e49\u4e3a GOptionArg \uff0c\u5b98\u65b9\u6587\u6863\uff1a<a href=\"https:\/\/docs.gtk.org\/glib\/enum.OptionArg.html\">https:\/\/docs.gtk.org\/glib\/enum.OptionArg.html<\/a>\u3002\u6839\u636e\u4e0d\u540c\u7c7b\u578b\uff0c\u9700\u8981\u63d0\u4f9b\u4e0d\u540c\u7684\u53d8\u91cf\uff0c\u6216\u8005\u5904\u7406\u51fd\u6570\u8fdb\u884c\u5904\u7406\u3002<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\"><strong>\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ gcc -o test gargs.c -I\/usr\/include\/glib-2.0 -I\/usr\/lib\/x86_64-linux-gnu\/glib-2.0\/include -I\/usr\/include\/sysprof-6 -pthread -lglib-2.0 \n\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;glib.h&gt;\n\nstatic gboolean boolarg = FALSE;\nstatic gchar **cfg_names = NULL;\nstatic gchar *run_user = NULL;\nstatic gint num = 0 ;\n\nstatic GOptionEntry entries&#91;] =\n{\n    { \"bool\",   'f', 0, G_OPTION_ARG_NONE,           &amp;boolarg,   \"bool arg default false\", NULL },\n    { \"config\", 'c', 0, G_OPTION_ARG_FILENAME_ARRAY, &amp;cfg_names, \"Specify config file(s)\", NULL },\n    { \"user\",   'u', 0, G_OPTION_ARG_STRING,         &amp;run_user,  \"User to run test\", NULL },\n    { \"num\",    'n', 0, G_OPTION_ARG_INT,            &amp;num, \"get args num\", NULL},\n    \/\/ \u6700\u540e\u4e00\u4e2a\u662f\u7ed3\u675f\u6807\u5fd7\n    { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }\n};\n\nstatic void\nread_cmd_line (gint *argc, gchar ***argv)\n{\n    GError *error = NULL;\n    GOptionContext *context;\n    guint cfg_num;\n\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u53c2\u6570\u89e3\u6790\u4e0a\u4e0b\u6587\n    context = g_option_context_new (\"- run test\");\n    \/\/ \u6dfb\u52a0\u4e00\u4e2a\u8bf4\u660e\uff0c--help\u6253\u5370\u4e2d\uff0c\u5728\u53c2\u6570\u5217\u8868\u4e4b\u524d\u6253\u5370\u3002\n    g_option_context_set_summary (context, \"Summary:\\n  A g_option test \u3002\");\n\n    \/\/ \u5982\u679c\u4e3b\u7ec4\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u4e3b\u7ec4\uff0c\u5e76\u5411\u5176\u6dfb\u52a0\u53c2\u6570\u89e3\u6790\u6761\u76ee\n    g_option_context_add_main_entries (context, entries, NULL);\n\n    \/\/ \u89e3\u6790\n    if (!g_option_context_parse (context, argc, argv, &amp;error)) {\n        fprintf (stderr, \"option parsing failed: %s\\n\", error-&gt;message);\n        g_option_context_free (context);\n        exit (EXIT_FAILURE);\n    }\n\n    \/\/ \u91ca\u653e\u53c2\u6570\u89e3\u6790\u4e0a\u4e0b\u6587\u7a7a\u95f4\n    g_option_context_free (context);\n}\n\n\nint main(int argc, char *argv&#91;]){\n\n    read_cmd_line (&amp;argc, &amp;argv) ;\n\n    g_print(\"bool&#91;%s] run_user&#91;%s] num&#91;%d]\", boolarg?\"TRUE\":\"FALSE\", run_user, num ) ;\n    if( cfg_names != NULL ) {\n        for( gint i=0; i&lt;g_strv_length(cfg_names) ; ++i ) {\n            g_print(\" config&#91;%s]\", cfg_names&#91;i] ) ;\n        }\n    }\n    g_print(\"\\n\") ;\n    return 0 ;\n}<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\"><strong>\u7f16\u8bd1\u548c\u6d4b\u8bd5\uff1a<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u7f16\u8bd1\u6307\u4ee4\uff1a<code>gcc -o test gargs.c `pkg-config --cflags --libs glib-2.0`<\/code><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"612\" height=\"398\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-22.png\" alt=\"\" class=\"wp-image-1637\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-22.png 612w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/10\/\u56fe\u7247-22-300x195.png 300w\" sizes=\"auto, (max-width: 612px) 100vw, 612px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u521b\u5efa\u4e00\u4e2a\u53c2\u6570\u89e3\u6790\u4e0a\u4e0b\u6587<br \/>\n\u6dfb\u52a0\u4e00\u4e2a\u8bf4\u660e\uff0c&#8211;help\u6253\u5370\u4e2d\uff0c\u5728\u53c2\u6570\u5217\u8868\u4e4b\u524d\u6253\u5370\u3002<br \/>\n\u5982\u679c\u4e3b\u7ec4\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u4e3b\u7ec4\uff0c\u5e76\u5411\u5176\u6dfb\u52a0\u53c2\u6570\u89e3\u6790\u6761\u76ee<br \/>\n\u89e3\u6790<\/p>\n","protected":false},"author":1,"featured_media":1676,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-with-sidebar","format":"standard","meta":{"footnotes":""},"categories":[156,154],"tags":[525,524,178,177,517],"class_list":["post-1632","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-c","category-154","tag-g_option_context_parse","tag-glib","tag-178","tag-177","tag-517"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/1632","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1632"}],"version-history":[{"count":11,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/1632\/revisions"}],"predecessor-version":[{"id":1749,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/1632\/revisions\/1749"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/1676"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}