gobyexample/public/command-line-flags
Hana 9e216da9ef go.mod: add go.mod and move pygments to third_party
After go1.16, go will use module mode by default,
even when the repository is checked out under GOPATH
or in a one-off directory. Add go.mod, go.sum to keep
this repo buildable without opting out of the module
mode.

> go mod init github.com/mmcgrana/gobyexample
> go mod tidy
> go mod vendor

In module mode, the 'vendor' directory is special
and its contents will be actively maintained by the
go command. pygments aren't the dependency the go will
know about, so it will delete the contents from vendor
directory. Move it to `third_party` directory now.

And, vendor the blackfriday package.

Note: the tutorial contents are not affected by the
change in go1.16 because all the examples in this
tutorial ask users to run the go command with the
explicit list of files to be compiled (e.g.
`go run hello-world.go` or `go build command-line-arguments.go`).
When the source list is provided, the go command does
not have to compute the build list and whether it's
running in GOPATH mode or module mode becomes irrelevant.
2021-02-15 16:45:26 -05:00

318 lines
12 KiB
Plaintext
Generated

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example: Command-Line Flags</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>: Command-Line Flags</h2>
<table>
<tr>
<td class="docs">
<p><a href="http://en.wikipedia.org/wiki/Command-line_interface#Command-line_option"><em>Command-line flags</em></a>
are a common way to specify options for command-line
programs. For example, in <code>wc -l</code> the <code>-l</code> is a
command-line flag.</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/fD0SmjD4GdZ"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Go provides a <code>flag</code> package supporting basic
command-line flag parsing. We&rsquo;ll use this package to
implement our example command-line program.</p>
</td>
<td class="code leading">
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
<span class="s">&quot;flag&quot;</span>
<span class="s">&quot;fmt&quot;</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>Basic flag declarations are available for string,
integer, and boolean options. Here we declare a
string flag <code>word</code> with a default value <code>&quot;foo&quot;</code>
and a short description. This <code>flag.String</code> function
returns a string pointer (not a string value);
we&rsquo;ll see how to use this pointer below.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nx">wordPtr</span> <span class="o">:=</span> <span class="nx">flag</span><span class="p">.</span><span class="nx">String</span><span class="p">(</span><span class="s">&quot;word&quot;</span><span class="p">,</span> <span class="s">&quot;foo&quot;</span><span class="p">,</span> <span class="s">&quot;a string&quot;</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>This declares <code>numb</code> and <code>fork</code> flags, using a
similar approach to the <code>word</code> flag.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nx">numbPtr</span> <span class="o">:=</span> <span class="nx">flag</span><span class="p">.</span><span class="nx">Int</span><span class="p">(</span><span class="s">&quot;numb&quot;</span><span class="p">,</span> <span class="mi">42</span><span class="p">,</span> <span class="s">&quot;an int&quot;</span><span class="p">)</span>
<span class="nx">boolPtr</span> <span class="o">:=</span> <span class="nx">flag</span><span class="p">.</span><span class="nx">Bool</span><span class="p">(</span><span class="s">&quot;fork&quot;</span><span class="p">,</span> <span class="kc">false</span><span class="p">,</span> <span class="s">&quot;a bool&quot;</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>It&rsquo;s also possible to declare an option that uses an
existing var declared elsewhere in the program.
Note that we need to pass in a pointer to the flag
declaration function.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <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="nx">StringVar</span><span class="p">(</span><span class="o">&amp;</span><span class="nx">svar</span><span class="p">,</span> <span class="s">&quot;svar&quot;</span><span class="p">,</span> <span class="s">&quot;bar&quot;</span><span class="p">,</span> <span class="s">&quot;a string var&quot;</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Once all flags are declared, call <code>flag.Parse()</code>
to execute the command-line parsing.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nx">flag</span><span class="p">.</span><span class="nx">Parse</span><span class="p">()</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Here we&rsquo;ll just dump out the parsed options and
any trailing positional arguments. Note that we
need to dereference the pointers with e.g. <code>*wordPtr</code>
to get the actual option values.</p>
</td>
<td class="code">
<div class="highlight"><pre> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;word:&quot;</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="nx">Println</span><span class="p">(</span><span class="s">&quot;numb:&quot;</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="nx">Println</span><span class="p">(</span><span class="s">&quot;fork:&quot;</span><span class="p">,</span> <span class="o">*</span><span class="nx">boolPtr</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">&quot;svar:&quot;</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="nx">Println</span><span class="p">(</span><span class="s">&quot;tail:&quot;</span><span class="p">,</span> <span class="nx">flag</span><span class="p">.</span><span class="nx">Args</span><span class="p">())</span>
<span class="p">}</span>
</pre></div>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
<p>To experiment with the command-line flags program it&rsquo;s
best to first compile it and then run the resulting
binary directly.</p>
</td>
<td class="code leading">
<div class="highlight"><pre><span class="gp">$</span> go build command-line-flags.go
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Try out the built program by first giving it values for
all flags.</p>
</td>
<td class="code leading">
<div class="highlight"><pre><span class="gp">$</span> ./command-line-flags -word<span class="o">=</span>opt -numb<span class="o">=</span><span class="m">7</span> -fork -svar<span class="o">=</span>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></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Note that if you omit flags they automatically take
their default values.</p>
</td>
<td class="code leading">
<div class="highlight"><pre><span class="gp">$</span> ./command-line-flags -word<span class="o">=</span>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></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Trailing positional arguments can be provided after
any flags.</p>
</td>
<td class="code leading">
<div class="highlight"><pre><span class="gp">$</span> ./command-line-flags -word<span class="o">=</span>opt a1 a2 a3
<span class="go">word: opt</span>
<span class="go">...</span>
<span class="go">tail: [a1 a2 a3]</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Note that the <code>flag</code> package requires all flags to
appear before positional arguments (otherwise the flags
will be interpreted as positional arguments).</p>
</td>
<td class="code leading">
<div class="highlight"><pre><span class="gp">$</span> ./command-line-flags -word<span class="o">=</span>opt a1 a2 a3 -numb<span class="o">=</span>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></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Use <code>-h</code> or <code>--help</code> flags to get automatically
generated help text for the command-line program.</p>
</td>
<td class="code leading">
<div class="highlight"><pre><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=&quot;bar&quot;: a string var</span>
<span class="go"> -word=&quot;foo&quot;: a string</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>If you provide a flag that wasn&rsquo;t specified to the
<code>flag</code> package, the program will print an error message
and show the help text again.</p>
</td>
<td class="code">
<div class="highlight"><pre><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></div>
</td>
</tr>
</table>
<p class="next">
Next example: <a href="command-line-subcommands">Command-Line Subcommands</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-flags">source</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>
</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 boolPtr :\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:\", *boolPtr)\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>