{"id":1970,"date":"2016-05-06T16:32:43","date_gmt":"2016-05-06T08:32:43","guid":{"rendered":"http:\/\/www.gigiwangs.com\/?p=1970"},"modified":"2016-05-06T16:32:43","modified_gmt":"2016-05-06T08:32:43","slug":"%e5%91%bd%e4%bb%a4%e8%a1%8c%e5%8f%82%e6%95%b0-command-line-flags","status":"publish","type":"post","link":"https:\/\/www.mujins.com\/?p=1970","title":{"rendered":"\u547d\u4ee4\u884c\u53c2\u6570-Command-Line Flags"},"content":{"rendered":"<p>\u539f\u6587: [<a href=\"https:\/\/gobyexample.com\/\" target=\"_blank\">Go by Example:Command-Line Flags<\/a>] \u00a0&#8211;\u6ce8\u610f\u6b63\u786e\u7684\u4e0a\u7f51\u59ff\u52bf..<\/p>\n<p>\u547d\u4ee4\u884c\u6807\u8bc6\uff08\u53c2\u6570\uff09\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6307\u5b9a\u547d\u4ee4\u884c\u7a0b\u5e8f\u9009\u9879\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982\uff0c\u5728`wc -l`\u4e2d \u201c-l\u201d \u5c31\u662f\u4e00\u4e2a\u547d\u4ee4\u7684\u53c2\u6570\u3002<\/p>\n<p><a href=\"https:\/\/www.gigiwangs.com\/wp-content\/uploads\/2016\/05\/golang.png\" data-lightbox=\"gal[1970]\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-1976\" src=\"https:\/\/www.gigiwangs.com\/wp-content\/uploads\/2016\/05\/golang.png\" alt=\"golang\" width=\"225\" height=\"225\" \/><\/a><br \/>\nGo\u63d0\u4f9b\u4e86\u4e00\u4e2a\u652f\u6301\u57fa\u672c\u7684\u547d\u4ee4\u884c\u53c2\u6570\u89e3\u6790\u7684flag package\uff08\u5305\uff09\u3002\u6211\u4eec\u5c06\u4f7f\u7528\u8fd9\u4e2apackage\u6765\u5b9e\u73b0\u6211\u4eec\u7684\u547d\u4ee4\u884c\u7a0b\u5e8f\u793a\u4f8b\u3002<\/p>\n<pre class=\"brush:c\">package main\n\nimport \"flag\"\nimport \"fmt\"\n\nfunc main() {\n\n    \/\/ \u57fa\u672c\u7684\u6807\u8bc6\u53ef\u4ee5\u58f0\u660e\u4e3astring,integer\u4ee5\u53cabool\u7c7b\u578b\u3002\n    \/\/ \u8fd9\u91cc\u6211\u4eec\u4ec0\u4e48\u4e86\u4e00\u4e2a\u5b57\u7b26\u4e32\u53c2\u6570word\uff0c\u5b83\u6709\u4e00\u4e2a\u9ed8\u8ba4\u503c\u201cfoo\u201d\uff0c\n    \/\/ \u8fd8\u6709\u4e00\u4e2a\u7b80\u77ed\u7684\u63cf\u8ff0\u3002\n    \/\/ flag.String\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u6307\u9488\uff08\u4e0d\u662f\u5b57\u7b26\u4e32\u503c\uff09\uff1b\n    \/\/ \u4e0b\u9762\u5c06\u770b\u5230\u5982\u4f55\u6765\u4f7f\u7528\u8fd9\u4e2a\u6307\u9488\u3002\n    \n    wordPtr := flag.String(\"word\", \"foo\", \"a string\")\n\n    \/\/ \u7c7b\u4f3c\u4e8e\u524d\u8fb9\u7684'word' flag\uff0c\u8fd9\u91cc\u58f0\u660e\u4e86numb\u548cfork\u6807\u8bc6.\n \n    numbPtr := flag.Int(\"numb\", 42, \"an int\")\n    boolPtr := flag.Bool(\"fork\", false, \"a bool\")\n\n    \/\/ \u8fd8\u53ef\u4ee5\u4f7f\u7528\u5728\u7a0b\u5e8f\u6210\u4efb\u4f55\u5730\u65b9\u58f0\u660e\u8fc7\u7684var\u53d8\u91cf\u6765\u58f0\u660e\u4e00\u4e2a\u9009\u9879\u3002 \n    \/\/ \u6ce8\u610f\uff0c\u6211\u4eec\u9700\u8981\u4e3a\u51fd\u6570\u4f20\u9012\u6307\u5411\u8fd9\u4e2aflag\u7684\u6307\u9488\u3002\n    \n    var svar string\n    flag.StringVar(&amp;svar, \"svar\", \"bar\", \"a string var\")\n\n    \/\/ \u4e00\u65e6\u6240\u6709\u7684\u58f0\u660e\u5b8c\u6210\uff0c\u8fd8\u9700\u8981\u8c03\u7528flag.Parse()\u6765\u6267\u884c\u547d\u4ee4\u884c\u89e3\u6790\u3002\n    \n    flag.Parse()\n\n    \/\/ \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u6253\u5370\u8f93\u51fa\u89e3\u6790\u7684\u9009\u9879\u8fd8\u6709\u5176\u5b83\u7d27\u63a5\u4f4d\u7f6e\u7684\u53c2\u6570\u3002\n    \/\/ (\u6ce8:Trailing positional arguments\u600e\u4e48\u7ffb\u8bd1\u5462?)\n    \/\/ \u8fd8\u8981\u6ce8\u610f\uff0c\u6211\u4eec\u9700\u8981\u7528\u5f62\u5982*wordPtr\u7684\u6307\u9488\u6307\u5411\u503c\uff0c\u6765\u83b7\u53d6\u53c2\u6570\u5b9e\u9645\u503c\u3002\n    \n    fmt.Println(\"word:\", *wordPtr)\n    fmt.Println(\"numb:\", *numbPtr)\n    fmt.Println(\"fork:\", *boolPtr)\n    fmt.Println(\"svar:\", svar)\n    fmt.Println(\"tail:\", flag.Args())\n}\n<\/pre>\n<p>\u6700\u597d\u5148\u7f16\u8bd1\u4e0b\uff0c\u7136\u540e\u76f4\u63a5\u8fd0\u884c\u751f\u6210\u4e8c\u8fdb\u5236\u7a0b\u5e8f\u6765\u6d4b\u8bd5\u8fd9\u4e2a\u5e26\u53c2\u6570\u7684\u547d\u4ee4\u884c\u7a0b\u5e8f\u3002<\/p>\n<pre class=\"&quot;brush:bash\">$ go build command-line-flags.go\n<\/pre>\n<p>\u9996\u5148\u7528\u6240\u6709flag\u90fd\u6709\u503c\u6765\u6d4b\u8bd5\u4e0b\u8fd9\u4e2a\u7f16\u8bd1\u597d\u7684\u7a0b\u5e8f\u3002<\/p>\n<pre class=\"&quot;brush:bash\">$ .\/command-line-flags -word=opt -numb=7 -fork -svar=flag\nword: opt\nnumb: 7\nfork: true\nsvar: flag\ntail: []\n<\/pre>\n<p>\u6ce8\u610f\uff0c\u5ffd\u7565\u7684flag\u53c2\u6570\u5c06\u88ab\u81ea\u52a8\u8d4b\u4e88\u4ed6\u4eec\u7684\u9ed8\u8ba4\u503c\u3002<\/p>\n<pre class=\"&quot;brush:bash\">$ .\/command-line-flags -word=opt\nword: opt\nnumb: 42\nfork: false\nsvar: bar\ntail: []\n<\/pre>\n<p>\u7d27\u968f\u53c2\u6570\u53ef\u4ee5\u4e3a\u63d0\u4f9b\u7ed9\u4efb\u610fflag<\/p>\n<pre class=\"&quot;brush:bash\">$ .\/command-line-flags -word=opt a1 a2 a3\nword: opt\n...\ntail: [a1 a2 a3]\n<\/pre>\n<p>\u6ce8\u610f\uff0cflag package\u9700\u8981\u6240\u6709\u7684flag\u90fd\u8981\u5728\u4f4d\u7f6e\u53c2\u6570\u4e4b\u524d\u51fa\u73b0\uff08\u5426\u5219\u8fd9\u4e9bflag\u5c06\u4f1a\u88ab\u4f4d\u7f6e\u53c2\u6570\u4e2d\u65ad\u8c03\u6389\uff09<\/p>\n<pre class=\"&quot;brush:bash\">$ .\/command-line-flags -word=opt a1 a2 a3 -numb=7\nword: opt\nnumb: 42\nfork: false\nsvar: bar\ntrailing: [a1 a2 a3 -numb=7]\n<\/pre>\n<p>\u4f7f\u7528 -h \u6216\u8005 &#8211;help \u53c2\u6570\u53ef\u4ee5\u81ea\u52a8\u4e3a\u547d\u4ee4\u884c\u7a0b\u5e8f\u751f\u6210\u5e2e\u52a9\u4fe1\u606f<\/p>\n<pre class=\"&quot;brush:bash\">$ .\/command-line-flags -h\nUsage of .\/command-line-flags:\n  -fork=false: a bool\n  -numb=42: an int\n  -svar=\"bar\": a string var\n  -word=\"foo\": a string\n<\/pre>\n<p>\u5982\u679c\u63d0\u4f9b\u7684flag\u4e0d\u662ffalg package\u4e2d\u6307\u5b9a\u3002\u7a0b\u5e8f\u5c06\u6253\u5370\u51fa\u9519\u4fe1\u606f\u5e76\u663e\u793a\u5e2e\u52a9\u6587\u672c\u3002<\/p>\n<pre class=\"&quot;brush:bash\">$ .\/command-line-flags -wat\nflag provided but not defined: -wat\nUsage of .\/command-line-flags:\n...\n<\/pre>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u770b\u5230\u73af\u5883\u53d8\u91cf-\u53e6\u4e00\u79cd\u5e38\u7528\u53c2\u6570\u5316\u7a0b\u5e8f\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587: [Go by Example:Command-Line Flags] \u00a0&#8211;\u6ce8\u610f\u6b63\u786e\u7684\u4e0a\u7f51\u59ff\u52bf.. \u547d\u4ee4\u884c\u6807\u8bc6\uff08\u53c2\u6570\uff09\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6307\u5b9a\u547d\u4ee4\u884c\u7a0b\u5e8f\u9009\u9879\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982\uff0c\u5728`wc -l`\u4e2d \u201c-l\u201d \u5c31\u662f\u4e00\u4e2a\u547d\u4ee4\u7684\u53c2\u6570\u3002 Go\u63d0\u4f9b\u4e86\u4e00\u4e2a\u652f\u6301\u57fa\u672c\u7684\u547d\u4ee4\u884c\u53c2\u6570\u89e3\u6790\u7684flag package\uff08\u5305\uff09\u3002\u6211\u4eec\u5c06\u4f7f\u7528\u8fd9\u4e2apackage\u6765\u5b9e\u73b0\u6211\u4eec\u7684\u547d\u4ee4\u884c\u7a0b\u5e8f\u793a\u4f8b\u3002 package main import &#8220;flag&#8221; import &#8220;fmt&#8221; func main() { \/\/ \u57fa\u672c\u7684\u6807\u8bc6\u53ef\u4ee5\u58f0\u660e\u4e3astring,integer\u4ee5\u53cabool\u7c7b\u578b\u3002 \/\/ \u8fd9\u91cc\u6211\u4eec\u4ec0\u4e48\u4e86\u4e00\u4e2a\u5b57\u7b26\u4e32\u53c2\u6570word\uff0c\u5b83\u6709\u4e00\u4e2a\u9ed8\u8ba4\u503c\u201cfoo\u201d\uff0c \/\/ \u8fd8\u6709\u4e00\u4e2a\u7b80\u77ed\u7684\u63cf\u8ff0\u3002 \/\/ flag.String\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u6307\u9488\uff08\u4e0d\u662f\u5b57\u7b26\u4e32\u503c\uff09\uff1b \/\/ \u4e0b\u9762\u5c06\u770b\u5230\u5982\u4f55\u6765\u4f7f\u7528\u8fd9\u4e2a\u6307\u9488\u3002 wordPtr := flag.String(&#8220;word&#8221;, &#8220;foo&#8221;, &#8220;a string&#8221;) \/\/ \u7c7b\u4f3c\u4e8e\u524d\u8fb9\u7684&#8217;word&#8217; flag\uff0c\u8fd9\u91cc\u58f0\u660e\u4e86numb\u548cfork\u6807\u8bc6. numbPtr := flag.Int(&#8220;numb&#8221;, 42, &#8220;an int&#8221;) boolPtr := flag.Bool(&#8220;fork&#8221;, false, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1976,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[31,4,93],"tags":[94],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.mujins.com\/index.php?rest_route=\/wp\/v2\/posts\/1970"}],"collection":[{"href":"https:\/\/www.mujins.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mujins.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mujins.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mujins.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1970"}],"version-history":[{"count":0,"href":"https:\/\/www.mujins.com\/index.php?rest_route=\/wp\/v2\/posts\/1970\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mujins.com\/index.php?rest_route=\/"}],"wp:attachment":[{"href":"https:\/\/www.mujins.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mujins.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mujins.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}