{"id":75,"date":"2025-11-01T09:45:08","date_gmt":"2025-11-01T01:45:08","guid":{"rendered":"http:\/\/118.139.165.189:3001\/?p=75"},"modified":"2025-11-01T10:18:06","modified_gmt":"2025-11-01T02:18:06","slug":"2023-08-26-go%e9%a1%b9%e7%9b%ae%e4%bc%a0%e9%80%92bool%e7%b1%bb%e5%9e%8b%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c%e5%8f%82%e6%95%b0flag%e6%97%b6%e9%9c%80%e8%a6%81%e7%95%99%e6%84%8f%e7%9a%84","status":"publish","type":"post","link":"https:\/\/blog.poker-x-studio.com\/index.php\/2025\/11\/01\/2023-08-26-go%e9%a1%b9%e7%9b%ae%e4%bc%a0%e9%80%92bool%e7%b1%bb%e5%9e%8b%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c%e5%8f%82%e6%95%b0flag%e6%97%b6%e9%9c%80%e8%a6%81%e7%95%99%e6%84%8f%e7%9a%84\/","title":{"rendered":"2023-08-26-go\u9879\u76ee\u4f20\u9012bool\u7c7b\u578b\u7684\u547d\u4ee4\u884c\u53c2\u6570flag\u65f6\u9700\u8981\u7559\u610f\u7684"},"content":{"rendered":"<h3>\u4e00 \u8fd9\u91cc\u547d\u4ee4\u884c\u53c2\u6570\u89e3\u6790\u4f7f\u7528\u7684\u662f\u7b2c\u4e09\u65b9\u5305 go-flags<\/h3>\n<h4>github\u5730\u5740 <a href=\"https:\/\/github.com\/jessevdk\/go-flags\">https:\/\/github.com\/jessevdk\/go-flags<\/a><\/h4>\n<p>\u5728README.md\u6587\u4ef6\u4e2d\uff0c\u6709\u90e8\u5206\u5173\u4e8ebool\u7c7b\u578b\u7684\u8bf4\u660e<\/p>\n<pre><code>var opts struct {\n    \/\/ Slice of bool will append 'true' each time the option\n    \/\/ is encountered (can be set multiple times, like -vvv)\n    Verbose []bool `short:&quot;v&quot; long:&quot;verbose&quot; description:&quot;Show verbose debug information&quot;`\n}<\/code><\/pre>\n<p>This specifies one option with a short name -v and a long name &#8211;verbose. When either -v or &#8211;verbose is found on the command line, a &#8216;true&#8217; value will be appended to the Verbose field. e.g. when specifying -vvv, the resulting value of Verbose will be {[true, true, true]}.<\/p>\n<h4>\u3010\u53ea\u8981\u5728\u547d\u4ee4\u884c\u4e2d\u627e\u5230 -v \u6216&#8211;verbose\uff0c\u90a3\u4e48\uff0c\u53c2\u6570\u5bf9\u5e94\u7684\u503c\u5c31\u662f true\u3011<\/h4>\n<h4>\u3010\u5982\u679c\u9700\u8981\u8bbe\u7f6efalse\uff0c\u5219 \u53bb\u6389 -v \u6216&#8211;verbose \u5c31\u597d\u3011<\/h4>\n<pre><code>\/\/ Make some fake arguments to parse.\nargs := []string{\n    &quot;-vv&quot;,\n}\n\nfmt.Printf(&quot;Verbosity: %vn&quot;, opts.Verbose)\n\n\/\/ Output: Verbosity: [true true]<\/code><\/pre>\n<h3>\u4e8c \u5728 vscode\u4e2d\u7684\u5177\u4f53\u4f7f\u7528<\/h3>\n<ul>\n<li>\n<p>launch.json \u4e2d\u8bbe\u7f6e<\/p>\n<pre><code>&quot;configurations&quot;: [\n    {\n        &quot;name&quot;: &quot;lobby_server&quot;,\n        &quot;type&quot;: &quot;go&quot;,\n        &quot;request&quot;: &quot;launch&quot;,\n        &quot;mode&quot;: &quot;auto&quot;,\n        &quot;program&quot;: &quot;${workspaceFolder}\/cmd\/lobby_server&quot;,\n        &quot;args&quot;: [\n            &quot;--debug&quot;,\/\/bool flag\u53ea\u8981\u8bbe\u7f6e short\u6216long,\u503c\u5c31\u662ftrue\n            &quot;--lobby_config_file&quot;,&quot;lobby_8101.toml&quot;,\n        ]\n    },<\/code><\/pre>\n<\/li>\n<\/ul>\n<p>main.go \u6587\u4ef6<\/p>\n<pre><code>import(\n&quot;github.com\/jessevdk\/go-flags&quot;\n)\n\n\/\/ \u547d\u4ee4\u884c\u53c2\u6570\ntype CmdParams struct {\n    Debug             bool   `short:&quot;d&quot; long:&quot;debug&quot; description:&quot;\u662f\u5426debug&quot;`\n    Lobby_config_file string `short:&quot;l&quot; long:&quot;lobby_config_file&quot; description:&quot;\u5927\u5385\u914d\u7f6e\u6587\u4ef6&quot;`\n}\n\nfunc main() {\n    defer func() {\n        if err := recover(); err != nil {\n            fmt.Println(err)\n        }\n    }()\n\n    \/\/\u89e3\u6790\u53c2\u6570\n    var cmd_params CmdParams\n    if _, err := flags.Parse(&amp;cmd_params); err != nil {\n        panic(&quot;Parse err:&quot; + err.Error())\n    }\n\n    if !cmd_params.Debug {\n        xdebug.Set_release()\n    }\n\n    \/\/...\n}<\/code><\/pre>\n<h3>\u4e09 \u8c03\u8bd5\u8ddf\u8e2a\u4ee3\u7801<\/h3>\n<p>\u5177\u4f53\u53ef\u8be6\u7ec6\u67e5\u770b \u51fd\u6570 parseOption<\/p>\n<pre><code>func (p *Parser) parseOption(s *parseState, name string, option *Option, canarg bool, argument *string) (err error) {\n    if !option.canArgument() {\/\/bool\u7c7b\u578b \u4e0d\u80fd\u5e26\u53c2\u6570\n        if argument != nil {\n            return newErrorf(ErrNoArgumentForBool, &quot;bool flag `%s' cannot have an argument&quot;, option)\n        }\n\n        err = option.set(nil)\n    } else if argument != nil || (canarg &amp;&amp; !s.eof()) {\n    \/\/...\n    }\n    \/\/....\n}   \n\nfunc (option *Option) canArgument() bool {\n    if u := option.isUnmarshaler(); u != nil {\n        return true\n    }\n\n    return !option.isBool()\n}\n\n\/\/\u8fd0\u884c\u53c2\u6570\u662f\u5426\u662fbool\u7c7b\u578b\nfunc (option *Option) isBool() bool {\n    tp := option.value.Type()\n\n    for {\n        switch tp.Kind() {\n        case reflect.Slice, reflect.Ptr:\n            tp = tp.Elem()\n        case reflect.Bool:\n            return true\n        case reflect.Func:\n            return tp.NumIn() == 0\n        default:\n            return false\n        }\n    }\n}<\/code><\/pre>\n<p>&#8211;the end<\/p>","protected":false},"excerpt":{"rendered":"<p>\u4e00 \u8fd9\u91cc\u547d\u4ee4\u884c\u53c2\u6570\u89e3\u6790\u4f7f\u7528\u7684\u662f\u7b2c\u4e09\u65b9\u5305 go-flags github\u5730\u5740 <a href=\"https:\/\/github.co\">https:\/\/github.co<\/a> &hellip; <a href=\"https:\/\/blog.poker-x-studio.com\/index.php\/2025\/11\/01\/2023-08-26-go%e9%a1%b9%e7%9b%ae%e4%bc%a0%e9%80%92bool%e7%b1%bb%e5%9e%8b%e7%9a%84%e5%91%bd%e4%bb%a4%e8%a1%8c%e5%8f%82%e6%95%b0flag%e6%97%b6%e9%9c%80%e8%a6%81%e7%95%99%e6%84%8f%e7%9a%84\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">2023-08-26-go\u9879\u76ee\u4f20\u9012bool\u7c7b\u578b\u7684\u547d\u4ee4\u884c\u53c2\u6570flag\u65f6\u9700\u8981\u7559\u610f\u7684<\/span><\/a><\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[4],"tags":[],"class_list":["post-75","post","type-post","status-publish","format-standard","hentry","category-go"],"_links":{"self":[{"href":"https:\/\/blog.poker-x-studio.com\/index.php\/wp-json\/wp\/v2\/posts\/75","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.poker-x-studio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.poker-x-studio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.poker-x-studio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.poker-x-studio.com\/index.php\/wp-json\/wp\/v2\/comments?post=75"}],"version-history":[{"count":2,"href":"https:\/\/blog.poker-x-studio.com\/index.php\/wp-json\/wp\/v2\/posts\/75\/revisions"}],"predecessor-version":[{"id":139,"href":"https:\/\/blog.poker-x-studio.com\/index.php\/wp-json\/wp\/v2\/posts\/75\/revisions\/139"}],"wp:attachment":[{"href":"https:\/\/blog.poker-x-studio.com\/index.php\/wp-json\/wp\/v2\/media?parent=75"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.poker-x-studio.com\/index.php\/wp-json\/wp\/v2\/categories?post=75"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.poker-x-studio.com\/index.php\/wp-json\/wp\/v2\/tags?post=75"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}