-
Notifications
You must be signed in to change notification settings - Fork 8
/
command-line-flags.html
293 lines (231 loc) · 12.2 KB
/
command-line-flags.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example 中文版: 命令行标志</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'command-line-arguments';
}
if (e.key == "ArrowRight") {
window.location.href = 'command-line-subcommands';
}
}
</script>
<body>
<div class="example" id="command-line-flags">
<h2><a href="./">Go by Example 中文版</a>: 命令行标志</h2>
<table>
<tr>
<td class="docs">
<p><a href="http://en.wikipedia.org/wiki/Command-line_interface#Command-line_option"><em>命令行标志</em></a>
是命令行程序指定选项的常用方式。例如,在 <code>wc -l</code> 中,
这个 <code>-l</code> 就是一个命令行标志。</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<a href="https://play.studygolang.com/p/IpbanTVOJ4d"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<pre class="chroma"><span class="kn">package</span> <span class="nx">main</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>Go 提供了一个 <code>flag</code> 包,支持基本的命令行标志解析。
我们将用这个包来实现我们的命令行程序示例。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kn">import</span> <span class="p">(</span>
<span class="s">"flag"</span>
<span class="s">"fmt"</span>
<span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>基本的标记声明仅支持字符串、整数和布尔值选项。
这里我们声明一个默认值为 <code>"foo"</code> 的字符串标志 <code>word</code> 并带有一个简短的描述。
这里的 <code>flag.String</code> 函数返回一个字符串指针(不是一个字符串值),
在下面我们会看到是如何使用这个指针的。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">wordPtr</span> <span class="o">:=</span> <span class="nx">flag</span><span class="p">.</span><span class="nf">String</span><span class="p">(</span><span class="s">"word"</span><span class="p">,</span> <span class="s">"foo"</span><span class="p">,</span> <span class="s">"a string"</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>使用和声明 <code>word</code> 标志相同的方法来声明 <code>numb</code> 和 <code>fork</code> 标志。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">numbPtr</span> <span class="o">:=</span> <span class="nx">flag</span><span class="p">.</span><span class="nf">Int</span><span class="p">(</span><span class="s">"numb"</span><span class="p">,</span> <span class="mi">42</span><span class="p">,</span> <span class="s">"an int"</span><span class="p">)</span>
<span class="nx">forkPtr</span> <span class="o">:=</span> <span class="nx">flag</span><span class="p">.</span><span class="nf">Bool</span><span class="p">(</span><span class="s">"fork"</span><span class="p">,</span> <span class="kc">false</span><span class="p">,</span> <span class="s">"a bool"</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>用程序中已有的参数来声明一个标志也是可以的。
注意在标志声明函数中需要使用该参数的指针。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kd">var</span> <span class="nx">svar</span> <span class="kt">string</span>
<span class="nx">flag</span><span class="p">.</span><span class="nf">StringVar</span><span class="p">(</span><span class="o">&</span><span class="nx">svar</span><span class="p">,</span> <span class="s">"svar"</span><span class="p">,</span> <span class="s">"bar"</span><span class="p">,</span> <span class="s">"a string var"</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>所有标志都声明完成以后,调用 <code>flag.Parse()</code> 来执行命令行解析。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">flag</span><span class="p">.</span><span class="nf">Parse</span><span class="p">()</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>这里我们将仅输出解析的选项以及后面的位置参数。
注意,我们需要使用类似 <code>*wordPtr</code> 这样的语法来对指针解引用,
从而得到选项真正的值。</p>
</td>
<td class="code">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"word:"</span><span class="p">,</span> <span class="o">*</span><span class="nx">wordPtr</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"numb:"</span><span class="p">,</span> <span class="o">*</span><span class="nx">numbPtr</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"fork:"</span><span class="p">,</span> <span class="o">*</span><span class="nx">forkPtr</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"svar:"</span><span class="p">,</span> <span class="nx">svar</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"tail:"</span><span class="p">,</span> <span class="nx">flag</span><span class="p">.</span><span class="nf">Args</span><span class="p">())</span>
<span class="p">}</span>
</pre>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
<p>测试这个程序前,最好将这个程序编译成二进制文件,然后再运行这个程序。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="gp">$</span> go build command-line-flags.go</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>首先以给所有标志赋值的方式,尝试运行构建的程序。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="gp">$</span> ./command-line-flags -word=opt -numb=7 -fork -svar=flag
<span class="go">word: opt
</span><span class="go">numb: 7
</span><span class="go">fork: true
</span><span class="go">svar: flag
</span><span class="go">tail: []</span></pre>
</td>
</tr>
<tr>
<td class="docs">
<p>注意,如果你省略一个标志,那么这个标志的值自动的设定为他的默认值。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="gp">$</span> ./command-line-flags -word=opt
<span class="go">word: opt
</span><span class="go">numb: 42
</span><span class="go">fork: false
</span><span class="go">svar: bar
</span><span class="go">tail: []</span></pre>
</td>
</tr>
<tr>
<td class="docs">
<p>尾随的位置参数可以出现在任何标志后面。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="gp">$</span> ./command-line-flags -word=opt a1 a2 a3
<span class="go">word: opt
</span><span class="go">...
</span><span class="go">tail: [a1 a2 a3]</span></pre>
</td>
</tr>
<tr>
<td class="docs">
<p>注意,<code>flag</code> 包需要所有的标志出现位置参数之前(否则,这个标志将会被解析为位置参数)。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="gp">$</span> ./command-line-flags -word=opt a1 a2 a3 -numb=7
<span class="go">word: opt
</span><span class="go">numb: 42
</span><span class="go">fork: false
</span><span class="go">svar: bar
</span><span class="go">tail: [a1 a2 a3 -numb=7]</span></pre>
</td>
</tr>
<tr>
<td class="docs">
<p>使用 <code>-h</code> 或者 <code>--help</code> 标志来得到自动生成的这个命令行程序的帮助文本。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="gp">$</span> ./command-line-flags -h
<span class="go">Usage of ./command-line-flags:
</span><span class="go"> -fork=false: a bool
</span><span class="go"> -numb=42: an int
</span><span class="go"> -svar="bar": a string var
</span><span class="go"> -word="foo": a string</span></pre>
</td>
</tr>
<tr>
<td class="docs">
<p>如果你提供了一个没有使用 <code>flag</code> 包声明的标志,
程序会输出一个错误信息,并再次显示帮助文本。</p>
</td>
<td class="code">
<pre class="chroma">
<span class="gp">$</span> ./command-line-flags -wat
<span class="go">flag provided but not defined: -wat
</span><span class="go">Usage of ./command-line-flags:
</span><span class="go">...</span></pre>
</td>
</tr>
</table>
<p class="next">
下一个例子: <a href="command-line-subcommands">命令行子命令</a>
</p>
<p class="footer">
<a href="https://twitter.com/mmcgrana">@mmcgrana</a> 和<a href="https://eli.thegreenplace.net">Eli Bendersky</a>编写 | <a href="https://github.com/gobyexample-cn">gobyexample-cn</a> 翻译 | <a href="https://github.com/gobyexample-cn/gobyexample/issues">反馈</a> | <a href="https://github.com/gobyexample-cn/gobyexample">源码</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a> </p>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"flag\"\u000A \"fmt\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' wordPtr :\u003D flag.String(\"word\", \"foo\", \"a string\")\u000A');codeLines.push(' numbPtr :\u003D flag.Int(\"numb\", 42, \"an int\")\u000A forkPtr :\u003D flag.Bool(\"fork\", false, \"a bool\")\u000A');codeLines.push(' var svar string\u000A flag.StringVar(\u0026svar, \"svar\", \"bar\", \"a string var\")\u000A');codeLines.push(' flag.Parse()\u000A');codeLines.push(' fmt.Println(\"word:\", *wordPtr)\u000A fmt.Println(\"numb:\", *numbPtr)\u000A fmt.Println(\"fork:\", *forkPtr)\u000A fmt.Println(\"svar:\", svar)\u000A fmt.Println(\"tail:\", flag.Args())\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>