Rebuild subcommands for tabs

This commit is contained in:
Mark McGranaghan 2019-06-04 07:32:51 -07:00
parent 7c160440be
commit c6a401d2a3
2 changed files with 39 additions and 39 deletions

View File

@ -8,50 +8,50 @@
package main package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"os" "os"
) )
func main() { func main() {
// We declare a subcommand using the `NewFlagSet` // We declare a subcommand using the `NewFlagSet`
// function, and proceed to define new flags specific // function, and proceed to define new flags specific
// for this subcommand. // for this subcommand.
fooCmd := flag.NewFlagSet("foo", flag.ExitOnError) fooCmd := flag.NewFlagSet("foo", flag.ExitOnError)
fooEnable := fooCmd.Bool("enable", false, "enable") fooEnable := fooCmd.Bool("enable", false, "enable")
fooName := fooCmd.String("name", "", "name") fooName := fooCmd.String("name", "", "name")
// For a different subcommand we can define different // For a different subcommand we can define different
// supported flags. // supported flags.
barCmd := flag.NewFlagSet("bar", flag.ExitOnError) barCmd := flag.NewFlagSet("bar", flag.ExitOnError)
barLevel := barCmd.Int("level", 0, "level") barLevel := barCmd.Int("level", 0, "level")
// The subcommand is expected as the first argument // The subcommand is expected as the first argument
// to the program. // to the program.
if len(os.Args) < 2 { if len(os.Args) < 2 {
fmt.Println("expected 'foo' or 'bar' subcommands") fmt.Println("expected 'foo' or 'bar' subcommands")
os.Exit(1) os.Exit(1)
} }
// Check which subcommand is invoked. // Check which subcommand is invoked.
switch os.Args[1] { switch os.Args[1] {
// For every subcommand, we parse its own flags and // For every subcommand, we parse its own flags and
// have access to trailing positional arguments. // have access to trailing positional arguments.
case "foo": case "foo":
fooCmd.Parse(os.Args[2:]) fooCmd.Parse(os.Args[2:])
fmt.Println("subcommand 'foo'") fmt.Println("subcommand 'foo'")
fmt.Println(" enable:", *fooEnable) fmt.Println(" enable:", *fooEnable)
fmt.Println(" name:", *fooName) fmt.Println(" name:", *fooName)
fmt.Println(" tail:", fooCmd.Args()) fmt.Println(" tail:", fooCmd.Args())
case "bar": case "bar":
barCmd.Parse(os.Args[2:]) barCmd.Parse(os.Args[2:])
fmt.Println("subcommand 'bar'") fmt.Println("subcommand 'bar'")
fmt.Println(" level:", *barLevel) fmt.Println(" level:", *barLevel)
fmt.Println(" tail:", barCmd.Args()) fmt.Println(" tail:", barCmd.Args())
default: default:
fmt.Println("expected 'foo' or 'bar' subcommands") fmt.Println("expected 'foo' or 'bar' subcommands")
os.Exit(1) os.Exit(1)
} }
} }

View File

@ -218,7 +218,7 @@ have access to trailing positional arguments.</p>
<span class="go">flag provided but not defined: -enable</span> <span class="go">flag provided but not defined: -enable</span>
<span class="go">Usage of bar:</span> <span class="go">Usage of bar:</span>
<span class="go"> -level int</span> <span class="go"> -level int</span>
<span class="go"> level</span> <span class="go"> level</span>
</pre></div> </pre></div>
</td> </td>