You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Assume the subcommand is the first arg in the flagSet
105
108
name:=flagSet.Arg(0)
109
+
110
+
// Loop through the list of all registered subcommands
106
111
for_, cmd:=rangec {
112
+
113
+
// If the first arg is not this registered commmand in the loop, try the next registered command
107
114
if!cmd.matches(name) {
108
115
continue
109
116
}
117
+
// If the first arg is this registered commmand in the loop, then try and run it, then exit
110
118
111
-
// Read global configuration now.
119
+
// Read global configuration
112
120
varerrerror
113
121
cfg, err=readConfig()
114
122
iferr!=nil {
115
123
log.Fatal("reading config: ", err)
116
124
}
117
125
118
-
// Get subcommand args, and re-add help flag if it was requested
126
+
// Get the remaining args, to pass to the subcommand, as an unparsed array of previously parsed args
119
127
args:=flagSet.Args()[1:]
120
-
ifhelpRequested {
121
-
args=append(args, "-h")
122
-
}
123
128
124
129
// Set output to stdout for help (flag package defaults to stderr)
125
130
cmd.flagSet.SetOutput(os.Stdout)
126
131
flag.CommandLine.SetOutput(os.Stdout)
127
132
133
+
// Note: We can't parse flags here because commanders need to pass unparsed args to subcommand handlers
134
+
// Each handler is responsible for parsing its own flags
135
+
// All commands must use `flagSet := flag.NewFlagSet("<name>", flag.ExitOnError)` to ensure usage helper text is printed automatically on arg parse errors
136
+
// Parse the subcommand's args, on its behalf, to test if flag.ExitOnError is not set
137
+
// if err := cmd.flagSet.Parse(args); err != nil {
0 commit comments