GO标准库—命令行参数解析FLAG

论坛 期权论坛     
选择匿名的用户   2021-5-23 02:01   24   0
<p>评论有人提到没有例子,不知道讲的是什么。因此,为了大家能够更好地理解,特意加了一个示例。其实本文更多讲解的是 flag 的实现原理,加上示例之后,就更好地知道怎么使用了。建议阅读 《Go语言标准库》一书的对应章节:<a href="http://books.studygolang.com/The-Golang-Standard-Library-by-Example/chapter13/13.1.html">flag – 命令行参数解析</a>。</p>
<p>在写命令行程序(工具、server)时,对命令参数进行解析是常见的需求。各种语言一般都会提供解析命令行参数的方法或库,以方便程序员使用。如果命令行参数纯粹自己写代码解析,对于比较复杂的,还是挺费劲的。在go标准库中提供了一个包:flag,方便进行命令行解析。</p>
<p>注:区分几个概念<br> 1)命令行参数(或参数):是指运行程序提供的参数<br> 2)已定义命令行参数:是指程序中通过flag.Xxx等这种形式定义了的参数<br> 3)非flag(non-flag)命令行参数(或保留的命令行参数):后文解释</p>
<h2>一、使用示例</h2>
<p>我们以 nginx 为例,执行 nginx -h,输出如下:</p>
<pre class="blockcode"><code class="language-bash">nginx version: nginx/1.10.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file
</code></pre>
<p>  </p>
<p>  </p>
<p>我们通过 &#96;flag&#96; 实现类似 nginx 的这个输出,创建文件 nginx.go,内容如下:</p>
<pre class="blockcode"><code class="language-Go">package main

import (
&#34;flag&#34;
&#34;fmt&#34;
&#34;os&#34;
)

// 实际中应该用更好的变量名
var (
h bool

v, V bool
t, T bool
q    *bool

s string
p string
c string
g string
)

func init() {
flag.BoolVar(&amp;h, &#34;h&#34;, false, &#34;this help&#34;)

flag.BoolVar(&amp;v, &#34;v&#34;, false, &#34;show version and exit&#34;)
flag.BoolVar(&amp;V, &#34;V&#34;, false, &#34;show version and configure options then exit&#34;)

flag.BoolVar(&amp;t, &#34;t&#34;, false, &#34;test configuration and exit&#34;)
flag.BoolVar(&amp;T, &#34;T&#34;, false, &#34;test configuration, dump it and exit&#34;)

// 另一种绑定方式
q &#61; flag.Bool(&#34;q&#34;, false, &#34;suppress non-error messages during configuration testing&#34;)

// 注意 &#96;signal&#96;。默认是 -s string,有了 &#96;signal&#96; 之后,变为 -s signal
flag.StringVar(&amp;s, &#34;s&#34;, &#34;&#34;, &#34;send &#96;signal&#96; to a master process: stop, quit, reopen, reload&#34;)
flag.StringVar(&amp;p, &#34;p&#34;, &#34;/usr/local/nginx/&#34;, &#34;set &#96;prefix&#96; path&#34;)
flag.StringVar(&amp;c, &#34;c&#34;, &#34;conf/nginx.conf&#34;, &#34;set configuration &#96;file&#96;&#34;)
flag.StringVar(&amp;g, &#34;g&#34;, &#34;conf/nginx.conf&#34;, &#34;set global &#96;directives&#96; out of configuration file&#34;)

// 改变默认的 Usage
flag.Usage &#61; usage
}

func main() {
flag.Parse()

if h {
  flag.Usage()
}
}

func usage() {
fmt.Fprintf(os.Stderr, &#96;nginx version: nginx/1.10.0
Usage: nginx [-hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
&#96;)
flag.PrintDefaults()
}</code></pre>
<p><br> 在flag包中,进行了进一步封装:将FlagSet的方法都重新定义了一遍,也就是提供了一序列函数,而函数中只是简单的调用已经实例化好了的FlagSet实例:commandLine 的方法,这样commandLine实例便不需要export。这样,使用者是这么调用:flag.Parse()而不是flag.commandLine.Parse()。(Go 1.2 版本起改为了 CommandLine)执行:go run nginx.go -h,(或 go build -o nginx &amp;&amp; ./nginx -h)输出如下:</p>
<pre class="blockcode"><code class="language-bash">nginx version: nginx/1.10.0
Usage: nginx [-hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -T test configuration, dump it and exit
  -V show version and configure options then exit
  -c file
     set configuration file (default &#34;conf/nginx.conf&#34;)
  -g directives
     set global directives out of configuration file (default &#34;conf/nginx.conf&#34;)
  -h this help
  -p prefix
     set prefix path (default &#34;/usr/local/nginx/&#34;)
  -q suppress non-error messages during configuration testing
  -s signal
     send signal to a master process: stop, quit, reopen, reload
  -t test configuration and exit
  -v show version and exit</code></pre>
<p>仔细理解以上例子,如果有不理解的,看完下文的讲解再回过头来看。</p>
<h2>二、flag包概述</h2>
<p>flag包实现了命令行参数的解析
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP