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
// If the first arg is this registered commmand in the loop, then try and run it, then exit
118
94
95
+
// Set up the usage function for this subcommand
96
+
// If the subcommand has a usageFunc defined, then use it
97
+
ifcmd.usageFunc!=nil {
98
+
cmd.flagSet.Usage=cmd.usageFunc
99
+
} else {
100
+
// If the subcommand does not have a usageFunc defined,
101
+
// then define a simple default one,
102
+
// using the list of flags defined in the subcommand, and their description strings
103
+
cmd.flagSet.Usage=func() {
104
+
_, _=fmt.Fprintf(flag.CommandLine.Output(), "Usage of '%s %s':\n", cmdName, cmd.flagSet.Name())
105
+
cmd.flagSet.PrintDefaults()
106
+
}
107
+
}
108
+
119
109
// Read global configuration
120
110
varerrerror
121
111
cfg, err=readConfig()
122
112
iferr!=nil {
123
113
log.Fatal("reading config: ", err)
124
114
}
125
115
126
-
// Get the remaining args, to pass to the subcommand, as an unparsed array of previously parsed args
116
+
// Get the remainder of the args, excluding the first arg / this command name
127
117
args:=flagSet.Args()[1:]
128
118
129
-
// Set output to stdout for help (flag package defaults to stderr)
119
+
// Set output to stdout, for usage / helper text printed for the --help flag (flag package defaults to stderr)
130
120
cmd.flagSet.SetOutput(os.Stdout)
131
121
flag.CommandLine.SetOutput(os.Stdout)
132
122
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