forked from gobyexample-cn/gobyexample-cn.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command-line-subcommands.html
258 lines (201 loc) · 13.2 KB
/
command-line-subcommands.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
<!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-flags';
}
if (e.key == "ArrowRight") {
window.location.href = 'environment-variables';
}
}
</script>
<body>
<div class="example" id="command-line-subcommands">
<h2><a href="./">Go by Example 中文版</a>: 命令行子命令</h2>
<table>
<tr>
<td class="docs">
<p><code>go</code> 和 <code>git</code> 这种命令行工具,都有很多的 <em>子命令</em> 。
并且每个工具都有一套自己的 flag,比如:
<code>go build</code> 和 <code>go get</code> 是 <code>go</code> 里面的两个不同的子命令。
<code>flag</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/Eo69vIgCwLp"><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">
</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="s">"os"</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>NewFlagSet</code> 函数声明一个子命令,
然后为这个子命令定义一个专用的 flag。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fooCmd</span> <span class="o">:=</span> <span class="nx">flag</span><span class="p">.</span><span class="nf">NewFlagSet</span><span class="p">(</span><span class="s">"foo"</span><span class="p">,</span> <span class="nx">flag</span><span class="p">.</span><span class="nx">ExitOnError</span><span class="p">)</span>
<span class="nx">fooEnable</span> <span class="o">:=</span> <span class="nx">fooCmd</span><span class="p">.</span><span class="nf">Bool</span><span class="p">(</span><span class="s">"enable"</span><span class="p">,</span> <span class="kc">false</span><span class="p">,</span> <span class="s">"enable"</span><span class="p">)</span>
<span class="nx">fooName</span> <span class="o">:=</span> <span class="nx">fooCmd</span><span class="p">.</span><span class="nf">String</span><span class="p">(</span><span class="s">"name"</span><span class="p">,</span> <span class="s">""</span><span class="p">,</span> <span class="s">"name"</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>对于不同的子命令,我们可以定义不同的 flag。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">barCmd</span> <span class="o">:=</span> <span class="nx">flag</span><span class="p">.</span><span class="nf">NewFlagSet</span><span class="p">(</span><span class="s">"bar"</span><span class="p">,</span> <span class="nx">flag</span><span class="p">.</span><span class="nx">ExitOnError</span><span class="p">)</span>
<span class="nx">barLevel</span> <span class="o">:=</span> <span class="nx">barCmd</span><span class="p">.</span><span class="nf">Int</span><span class="p">(</span><span class="s">"level"</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="s">"level"</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>子命令应作为程序的第一个参数传入。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="nx">os</span><span class="p">.</span><span class="nx">Args</span><span class="p">)</span> <span class="p"><</span> <span class="mi">2</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">"expected 'foo' or 'bar' subcommands"</span><span class="p">)</span>
<span class="nx">os</span><span class="p">.</span><span class="nf">Exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>检查哪一个子命令被调用了。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="k">switch</span> <span class="nx">os</span><span class="p">.</span><span class="nx">Args</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="p">{</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>每个子命令,都会解析自己的 flag 并允许它访问后续的位置参数。</p>
</td>
<td class="code">
<pre class="chroma">
<span class="k">case</span> <span class="s">"foo"</span><span class="p">:</span>
<span class="nx">fooCmd</span><span class="p">.</span><span class="nf">Parse</span><span class="p">(</span><span class="nx">os</span><span class="p">.</span><span class="nx">Args</span><span class="p">[</span><span class="mi">2</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">"subcommand 'foo'"</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">" enable:"</span><span class="p">,</span> <span class="o">*</span><span class="nx">fooEnable</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">" name:"</span><span class="p">,</span> <span class="o">*</span><span class="nx">fooName</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">fooCmd</span><span class="p">.</span><span class="nf">Args</span><span class="p">())</span>
<span class="k">case</span> <span class="s">"bar"</span><span class="p">:</span>
<span class="nx">barCmd</span><span class="p">.</span><span class="nf">Parse</span><span class="p">(</span><span class="nx">os</span><span class="p">.</span><span class="nx">Args</span><span class="p">[</span><span class="mi">2</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">"subcommand 'bar'"</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">" level:"</span><span class="p">,</span> <span class="o">*</span><span class="nx">barLevel</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">barCmd</span><span class="p">.</span><span class="nf">Args</span><span class="p">())</span>
<span class="k">default</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">"expected 'foo' or 'bar' subcommands"</span><span class="p">)</span>
<span class="nx">os</span><span class="p">.</span><span class="nf">Exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</pre>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="gp">$</span> go build command-line-subcommands.go </pre>
</td>
</tr>
<tr>
<td class="docs">
<p>首先调用 foo 子命令。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="gp">$</span> ./command-line-subcommands foo -enable -name=joe a1 a2
<span class="go">subcommand 'foo'
</span><span class="go"> enable: true
</span><span class="go"> name: joe
</span><span class="go"> tail: [a1 a2]</span></pre>
</td>
</tr>
<tr>
<td class="docs">
<p>然后试一下 bar 子命令。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="gp">$</span> ./command-line-subcommands bar -level 8 a1
<span class="go">subcommand 'bar'
</span><span class="go"> level: 8
</span><span class="go"> tail: [a1]</span></pre>
</td>
</tr>
<tr>
<td class="docs">
<p>但是 bar 不接受 foo 的 flag(enable)。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="gp">$</span> ./command-line-subcommands bar -enable a1
<span class="go">flag provided but not defined: -enable
</span><span class="go">Usage of bar:
</span><span class="go"> -level int
</span><span class="go"> level</span></pre>
</td>
</tr>
<tr>
<td class="docs">
<p>接下来我们会学习程序获取参数的另一种常见方式——环境变量。</p>
</td>
<td class="code empty">
</td>
</tr>
</table>
<p class="next">
下一个例子: <a href="environment-variables.html">环境变量</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 \"os\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' fooCmd :\u003D flag.NewFlagSet(\"foo\", flag.ExitOnError)\u000A fooEnable :\u003D fooCmd.Bool(\"enable\", false, \"enable\")\u000A fooName :\u003D fooCmd.String(\"name\", \"\", \"name\")\u000A');codeLines.push(' barCmd :\u003D flag.NewFlagSet(\"bar\", flag.ExitOnError)\u000A barLevel :\u003D barCmd.Int(\"level\", 0, \"level\")\u000A');codeLines.push(' if len(os.Args) \u003C 2 {\u000A fmt.Println(\"expected \'foo\' or \'bar\' subcommands\")\u000A os.Exit(1)\u000A }\u000A');codeLines.push(' switch os.Args[1] {\u000A');codeLines.push(' case \"foo\":\u000A fooCmd.Parse(os.Args[2:])\u000A fmt.Println(\"subcommand \'foo\'\")\u000A fmt.Println(\" enable:\", *fooEnable)\u000A fmt.Println(\" name:\", *fooName)\u000A fmt.Println(\" tail:\", fooCmd.Args())\u000A case \"bar\":\u000A barCmd.Parse(os.Args[2:])\u000A fmt.Println(\"subcommand \'bar\'\")\u000A fmt.Println(\" level:\", *barLevel)\u000A fmt.Println(\" tail:\", barCmd.Args())\u000A default:\u000A fmt.Println(\"expected \'foo\' or \'bar\' subcommands\")\u000A os.Exit(1)\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>