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.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
packagemain
+
+
+
+
+
+
+
+
+
+
+
+
import(
+ "flag"
+ "fmt"
+ "os"
+)
+
+
+
+
+
+
+
+
+
+
+
+
funcmain(){
+
+
+
+
+
+
+
+
We declare a subcommand using the NewFlagSet
+function, and proceed to define new flags specific
+for this subcommand.
From af633e81f3d4b1087d743752fd83d298379b5f32 Mon Sep 17 00:00:00 2001
From: Mark McGranaghan
Date: Tue, 4 Jun 2019 07:25:49 -0700
Subject: [PATCH 2/2] Presentation tweak
---
.../command-line-subcommands/command-line-subcommands.go | 6 +++---
.../command-line-subcommands.hash | 4 ++--
public/command-line-subcommands | 8 ++++----
public/variables | 2 +-
4 files changed, 10 insertions(+), 10 deletions(-)
diff --git a/examples/command-line-subcommands/command-line-subcommands.go b/examples/command-line-subcommands/command-line-subcommands.go
index 132c9ec..af57b21 100644
--- a/examples/command-line-subcommands/command-line-subcommands.go
+++ b/examples/command-line-subcommands/command-line-subcommands.go
@@ -36,10 +36,10 @@ func main() {
// 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.
+ // For every subcommand, we parse its own flags and
+ // have access to trailing positional arguments.
+ case "foo":
fooCmd.Parse(os.Args[2:])
fmt.Println("subcommand 'foo'")
fmt.Println(" enable:", *fooEnable)
diff --git a/examples/command-line-subcommands/command-line-subcommands.hash b/examples/command-line-subcommands/command-line-subcommands.hash
index b26ba5f..f22bcc8 100644
--- a/examples/command-line-subcommands/command-line-subcommands.hash
+++ b/examples/command-line-subcommands/command-line-subcommands.hash
@@ -1,2 +1,2 @@
-673d7811734cfd8f8f5bcb411e12def1e158cf0b
-V7xR3wMkquz
+5a0ec258e4992e9b93b11d48f2f249092ff3db66
+gtgSAg76N4I
diff --git a/public/command-line-subcommands b/public/command-line-subcommands
index 190970c..f53c19f 100644
--- a/public/command-line-subcommands
+++ b/public/command-line-subcommands
@@ -32,7 +32,7 @@ subcommands that have their own flags.
-
+
packagemain
@@ -124,7 +124,6 @@ to the program.
switchos.Args[1]{
- case"foo":
@@ -138,7 +137,8 @@ have access to trailing positional arguments.
-
fooCmd.Parse(os.Args[2:])
+
case"foo":
+ fooCmd.Parse(os.Args[2:])fmt.Println("subcommand 'foo'")fmt.Println(" enable:",*fooEnable)fmt.Println(" name:",*fooName)
@@ -218,7 +218,7 @@ have access to trailing positional arguments.
flag provided but not defined: -enableUsage of bar: -level int
- bar level
+ level
diff --git a/public/variables b/public/variables
index a02f89a..a10e9c0 100644
--- a/public/variables
+++ b/public/variables
@@ -150,7 +150,7 @@ initializing a variable, e.g. for
1 2true0
-short
+apple