Subcommands example
This commit is contained in:
parent
f2f5af3690
commit
738fc184ef
@ -59,6 +59,7 @@ Writing Files
|
|||||||
Line Filters
|
Line Filters
|
||||||
Command-Line Arguments
|
Command-Line Arguments
|
||||||
Command-Line Flags
|
Command-Line Flags
|
||||||
|
Command-Line Subcommands
|
||||||
Environment Variables
|
Environment Variables
|
||||||
HTTP Clients
|
HTTP Clients
|
||||||
Spawning Processes
|
Spawning Processes
|
||||||
|
@ -0,0 +1,57 @@
|
|||||||
|
// Some command-line tools, like the `go` tool or `git`
|
||||||
|
// have many *subcommands*, each with its own set of
|
||||||
|
// flags. For example, `go build` and `go get` are two
|
||||||
|
// different subcommands of the `go` tool.
|
||||||
|
// The `flag` package lets us easily define simple
|
||||||
|
// subcommands that have their own flags.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
// We declare a subcommand using the `NewFlagSet`
|
||||||
|
// function, and proceed to define new flags specific
|
||||||
|
// for this subcommand.
|
||||||
|
fooCmd := flag.NewFlagSet("foo", flag.ExitOnError)
|
||||||
|
fooEnable := fooCmd.Bool("enable", false, "enable")
|
||||||
|
fooName := fooCmd.String("name", "", "name")
|
||||||
|
|
||||||
|
// For a different subcommand we can define different
|
||||||
|
// supported flags.
|
||||||
|
barCmd := flag.NewFlagSet("bar", flag.ExitOnError)
|
||||||
|
barLevel := barCmd.Int("level", 0, "level")
|
||||||
|
|
||||||
|
// The subcommand is expected as the first argument
|
||||||
|
// to the program.
|
||||||
|
if len(os.Args) < 2 {
|
||||||
|
fmt.Println("expected 'foo' or 'bar' subcommands")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check which subcommand is invoked.
|
||||||
|
switch os.Args[1] {
|
||||||
|
case "foo":
|
||||||
|
|
||||||
|
// For every subcommand, we parse its own flags and
|
||||||
|
// have access to trailing positional arguments.
|
||||||
|
fooCmd.Parse(os.Args[2:])
|
||||||
|
fmt.Println("subcommand 'foo'")
|
||||||
|
fmt.Println(" enable:", *fooEnable)
|
||||||
|
fmt.Println(" name:", *fooName)
|
||||||
|
fmt.Println(" tail:", fooCmd.Args())
|
||||||
|
case "bar":
|
||||||
|
barCmd.Parse(os.Args[2:])
|
||||||
|
fmt.Println("subcommand 'bar'")
|
||||||
|
fmt.Println(" level:", *barLevel)
|
||||||
|
fmt.Println(" tail:", barCmd.Args())
|
||||||
|
default:
|
||||||
|
fmt.Println("expected 'foo' or 'bar' subcommands")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,2 @@
|
|||||||
|
673d7811734cfd8f8f5bcb411e12def1e158cf0b
|
||||||
|
V7xR3wMkquz
|
@ -0,0 +1,21 @@
|
|||||||
|
$ go build command-line-subcommands.go
|
||||||
|
|
||||||
|
# First invoke the foo subcommand
|
||||||
|
$ ./command-line-subcommands foo -enable -name=joe a1 a2
|
||||||
|
subcommand 'foo'
|
||||||
|
enable: true
|
||||||
|
name: joe
|
||||||
|
tail: [a1 a2]
|
||||||
|
|
||||||
|
# Now try bar
|
||||||
|
$ ./command-line-subcommands bar -level 8 a1
|
||||||
|
subcommand 'bar'
|
||||||
|
level: 8
|
||||||
|
tail: [a1]
|
||||||
|
|
||||||
|
# But bar won't accept foo's flags
|
||||||
|
$ ./command-line-subcommands bar -enable a1
|
||||||
|
flag provided but not defined: -enable
|
||||||
|
Usage of bar:
|
||||||
|
-level int
|
||||||
|
level
|
@ -297,7 +297,7 @@ way to parameterize programs.</p>
|
|||||||
|
|
||||||
|
|
||||||
<p class="next">
|
<p class="next">
|
||||||
Next example: <a href="environment-variables">Environment Variables</a>.
|
Next example: <a href="command-line-subcommands">Command-Line Subcommands</a>.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p class="footer">
|
<p class="footer">
|
||||||
|
239
public/command-line-subcommands
Normal file
239
public/command-line-subcommands
Normal file
@ -0,0 +1,239 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>Go by Example: Command-Line Subcommands</title>
|
||||||
|
<link rel=stylesheet href="site.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="example" id="command-line-subcommands">
|
||||||
|
<h2><a href="./">Go by Example</a>: Command-Line Subcommands</h2>
|
||||||
|
|
||||||
|
<table>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>Some command-line tools, like the <code>go</code> tool or <code>git</code>
|
||||||
|
have many <em>subcommands</em>, each with its own set of
|
||||||
|
flags. For example, <code>go build</code> and <code>go get</code> are two
|
||||||
|
different subcommands of the <code>go</code> tool.
|
||||||
|
The <code>flag</code> package lets us easily define simple
|
||||||
|
subcommands that have their own flags.</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code empty leading">
|
||||||
|
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
<a href="http://play.golang.org/p/V7xR3wMkquz"><img title="Run code" class="run" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAPCAYAAADtc08vAAAABGdBTUEAANbY1E9YMgAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAJ1SURBVCjPY/j//z8DMu7o6GAAgpQgD9tLqcmJH4KDg14aaik/MtdXe2ZjY6OCrh6Fs2jRYmZ9Pd05M9uL/u9dPfU/CLS0dfxvKIz/X5Dg/z8pKdkGqwGpqakMUdExDHJSYqt37tjxf+qUSf9rc2P+79298/+RA3v+H1zV///o6r7/DrbWFQkJiQwxMTGoBjAxMTpKiQmuqMuP/f/xw/v/J0+f/W9tbvTfxVLn/8rJVf+v757z/96hRf8TQtxuCQmLMjk4OKAawMfDVWVvrvd85eTq/7tXTP6/e/XM/22lif9LCnL+b13Q/v/Kzln/L++c/X/7/Jb/VpYWuZFRUagGAAErEBtlxvi+vn944f9L26cDNcz6v21R9/8zm6aC2SBDbu+f/78kK+4/L79AO7oBYCAqxD/57JZp/y/tmPX/wrYZ/6+CbAayD6zs/78daBjIgPayFJAGG6wGAIFAcpjH/dv7F4ANABuya/b/Od3l/ye2V/+/tnv2/7ldxSANmrgMYGBhZg7fuagD7GyYIeeBrrqwdRrQgLn/l02sBGkwwWkAEAjV5EZ/vQV0LswAGAYZsLC3DKTBAJ8BzCkRni/uHFyIYcAtoNc6ypL/ANVIohigrKwMxqqqqgxMzKzM6VHeL+6iGQAKzDtAV5XlJv3n5uFLRTHgzZs3YPzz50+GwqJiPitD9Y8Pjy4BB+CNvfP+3wUmIpAhhckhr3X19LodHZ28UQxQU1MDYw0NDQYBAQEeoBOTK7JjP2xf3Pt/bkfB/4KkoDcKMmIL5OXlFerq6hhu3rzJgC8MwMDYxGSfm5vbVn9/f0cgVxAkpqioyFBfX49iAACbTAK+xT3CzgAAAABJRU5ErkJggg==" /></a>
|
||||||
|
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre><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></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>We declare a subcommand using the <code>NewFlagSet</code>
|
||||||
|
function, and proceed to define new flags specific
|
||||||
|
for this subcommand.</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre> <span class="nx">fooCmd</span> <span class="o">:=</span> <span class="nx">flag</span><span class="p">.</span><span class="nx">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="nx">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="nx">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></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>For a different subcommand we can define different
|
||||||
|
supported flags.</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre> <span class="nx">barCmd</span> <span class="o">:=</span> <span class="nx">flag</span><span class="p">.</span><span class="nx">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="nx">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></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>The subcommand is expected as the first argument
|
||||||
|
to the program.</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre> <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="nx">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="nx">Exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
|
||||||
|
<span class="p">}</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>Check which subcommand is invoked.</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre> <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>
|
||||||
|
<span class="k">case</span> <span class="s">"foo"</span><span class="p">:</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>For every subcommand, we parse its own flags and
|
||||||
|
have access to trailing positional arguments.</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code">
|
||||||
|
|
||||||
|
<div class="highlight"><pre> <span class="nx">fooCmd</span><span class="p">.</span><span class="nx">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="nx">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="nx">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="nx">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="nx">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="nx">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="nx">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="nx">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="nx">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="nx">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="nx">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="nx">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="nx">Exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
|
||||||
|
<span class="p">}</span>
|
||||||
|
<span class="p">}</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<table>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre><span class="gp">$</span> go build command-line-subcommands.go
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>First invoke the foo subcommand</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre><span class="gp">$</span> ./command-line-subcommands foo -enable -name<span class="o">=</span>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></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>Now try bar</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre><span class="gp">$</span> ./command-line-subcommands bar -level <span class="m">8</span> a1
|
||||||
|
<span class="go">subcommand 'bar'</span>
|
||||||
|
<span class="go"> level: 8</span>
|
||||||
|
<span class="go"> tail: [a1]</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>But bar won’t accept foo’s flags</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code">
|
||||||
|
|
||||||
|
<div class="highlight"><pre><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"> bar level</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
</table>
|
||||||
|
|
||||||
|
|
||||||
|
<p class="next">
|
||||||
|
Next example: <a href="environment-variables">Environment Variables</a>.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p class="footer">
|
||||||
|
by <a href="https://markmcgranaghan.com">Mark McGranaghan</a> | <a href="https://github.com/mmcgrana/gobyexample/blob/master/examples/command-line-subcommands">source</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -145,6 +145,8 @@
|
|||||||
|
|
||||||
<li><a href="command-line-flags">Command-Line Flags</a></li>
|
<li><a href="command-line-flags">Command-Line Flags</a></li>
|
||||||
|
|
||||||
|
<li><a href="command-line-subcommands">Command-Line Subcommands</a></li>
|
||||||
|
|
||||||
<li><a href="environment-variables">Environment Variables</a></li>
|
<li><a href="environment-variables">Environment Variables</a></li>
|
||||||
|
|
||||||
<li><a href="http-clients">HTTP Clients</a></li>
|
<li><a href="http-clients">HTTP Clients</a></li>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user