{"id":2192,"date":"2025-07-23T15:11:13","date_gmt":"2025-07-23T07:11:13","guid":{"rendered":"https:\/\/www.madbull.site\/?p=2192"},"modified":"2025-07-23T15:11:13","modified_gmt":"2025-07-23T07:11:13","slug":"getopt_long%e5%91%bd%e4%bb%a4%e8%a1%8c%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=2192","title":{"rendered":"getopt_long\u547d\u4ee4\u884c\u89e3\u6790"},"content":{"rendered":"\n<p>\u4e4b\u524d\u4ecb\u7ecd\u8fc7getopt\u51fd\u6570\u89e3\u6790\u547d\u4ee4\u884c\uff0c\u53c2\u770b\u5f80\u671f\u6587\u7ae0\uff1a<a href=\"https:\/\/www.madbull.site\/?p=1606\">https:\/\/www.madbull.site\/?p=1606<\/a><\/p>\n\n\n\n<p>\u4eca\u5929\u505a\u4e2agetopt_long\u7684\u7b14\u8bb0\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u51fd\u6570\u5b9a\u4e49<\/strong><\/h2>\n\n\n\n<p>\u51fd\u6570\u539f\u578b\u5728 getopt_ext.h \u6587\u4ef6\u4e2d\u7684\u5b9a\u4e49\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>extern int getopt_long (int ___argc, char *__getopt_argv_const *___argv,\n\t\t\tconst char *__shortopts,\n\t\t        const struct option *__longopts, int *__longind)<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2d\u53c2\u6570 struct option \u7ed3\u6784\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct option\n{\n  const char *name;\n  \/* has_arg can't be an enum because some compilers complain about\n     type mismatches in all the code that assumes it is an int.  *\/\n  int has_arg;\n  int *flag;\n  int val;\n};<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>name\uff1a\u53c2\u6570\u540d\u79f0\uff0c\u4e0d\u9700\u8981\u52a0 \u201c&#8211;\u201d<\/li>\n\n\n\n<li>has_arg\uff1a\u6709\u4e09\u79cd\u7c7b\u578b\uff0c\u5982\u4e0b\uff1a\n<ul class=\"wp-block-list\">\n<li>no_argument \u00a0 \u00a0 0         \u53c2\u6570\u4e0d\u9700\u8981\u9009\u9879<\/li>\n\n\n\n<li>required_argument \u00a01   \u53c2\u6570\u5fc5\u987b\u8981\u5e26\u9009\u9879<\/li>\n\n\n\n<li>optional_argument \u00a02   \u53c2\u6570\u53ef\u4ee5\u5e26\u9009\u9879\u4e5f\u53ef\u4ee5\u4e0d\u5e26\u9009\u9879<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>flag\uff1a\u628a\u503c\u76f4\u63a5\u590d\u5236\u7ed9*flag\u53d8\u91cf<\/li>\n\n\n\n<li>val\uff1a\u5f53 <code>flag != NULL<\/code> \u65f6\uff0c\u76f4\u63a5\u628a val \u503c\u8d4b\u503c\u7ed9*flag\u53d8\u91cf\uff1b\u5f53 <code>flag == NULL<\/code> \u65f6\uff0c\u5219val\u4f5c\u4e3agetopt_long\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002<\/li>\n\n\n\n<li>\u53e6\u5916\uff1a\u5168\u5c40\u53d8\u91cf optarg \u5728\u5e26\u9009\u9879\u7684\u53c2\u6570\u91cc\uff0c\u4f5c\u4e3a\u9009\u9879\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4f7f\u7528\u4e3e\u4f8b<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ gcc getoptlong.c -o test\n\n#include &lt;stdio.h>\n#include &lt;stdlib.h>\n#include &lt;getopt.h>\n\nint main(int argc, char *argv&#91;]) {\n    int opt;\n    int verbose = 0;\n    char *output_file = NULL;\n    int count = 1;  \/\/ \u9ed8\u8ba4\u503c\n    \n    \/\/ \u5b9a\u4e49\u957f\u9009\u9879\n    struct option long_options&#91;] = {\n        {\"verbose\", no_argument,       &amp;verbose, 1},     \/\/ \u65e0\u53c2\u6570\n        {\"output\",  required_argument, 0,      'o'},     \/\/ \u5fc5\u987b\u53c2\u6570\n        {\"count\",   optional_argument, 0,      'c'},     \/\/ \u53ef\u9009\u53c2\u6570\n        {0, 0, 0, 0}\n    };\n\n    while ((opt = getopt_long(argc, argv, \"o:c::\", long_options, NULL)) != -1) {\n        switch (opt) {\n            case 0:\n                \/\/ \u5df2\u901a\u8fc7 flag \u8bbe\u7f6e verbose\n                break;\n                \n            case 'o':\n                output_file = optarg;\n                printf(\"\u8f93\u51fa\u6587\u4ef6: %s\\n\", output_file);\n                break;\n                \n            case 'c':\n                if (optarg) {\n                    count = atoi(optarg);\n                    printf(\"\u81ea\u5b9a\u4e49\u8ba1\u6570: %d\\n\", count);\n                } else {\n                    printf(\"\u4f7f\u7528\u9ed8\u8ba4\u8ba1\u6570: %d\\n\", count);\n                }\n                break;\n                \n            case '?':\n                printf(\"\u672a\u77e5\u9009\u9879\u6216\u7f3a\u5c11\u53c2\u6570\\n\");\n                exit(EXIT_FAILURE);\n        }\n    }\n\n    \/\/ \u6253\u5370\u6700\u7ec8\u7ed3\u679c\n    printf(\"\\n\u6700\u7ec8\u914d\u7f6e:\\n\");\n    printf(\"\u8be6\u7ec6\u6a21\u5f0f: %s\\n\", verbose ? \"\u5f00\u542f\" : \"\u5173\u95ed\");\n    printf(\"\u8f93\u51fa\u6587\u4ef6: %s\\n\", output_file ? output_file : \"\u65e0\");\n    printf(\"\u8ba1\u6570: %d\\n\", count);\n    \n    return 0;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u7f16\u8bd1\u6d4b\u8bd5<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"629\" height=\"893\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2025\/07\/111-1.png\" alt=\"\" class=\"wp-image-2193\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>getopt_long\u7b14\u8bb0\u3002<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-with-sidebar","format":"standard","meta":{"footnotes":""},"categories":[156,154],"tags":[175,237],"class_list":["post-2192","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-c","category-154","tag-c","tag-linux"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2192","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=2192"}],"version-history":[{"count":1,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2192\/revisions"}],"predecessor-version":[{"id":2194,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2192\/revisions\/2194"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}