1
0
mirror of https://github.com/golang/go synced 2024-11-26 17:07:09 -07:00

change solution to use init() method

This commit is contained in:
Darren 2024-07-11 10:47:29 +01:00 committed by GitHub
parent b4630820d1
commit cf78a8c895
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1196,17 +1196,16 @@ func Parsed() bool {
// CommandLine is the default set of command-line flags, parsed from [os.Args].
// The top-level functions such as [BoolVar], [Arg], and so on are wrappers for the
// methods of CommandLine.
var CommandLine = safeCommandLineFlagSet()
// Handle the scenario where a process run via execl() can have an empty os.Args
func safeCommandLineFlagSet() {
if len(os.Args) == 0 {
return NewFlagSet("", ExitOnError)
}
return NewFlagSet(os.Args[0], ExitOnError)
}
var CommandLine *FlagSet
func init() {
// Handle the scenario where a process run via execl() can have an empty os.Args
if len(os.Args) == 0 {
CommandLine = NewFlagSet("", ExitOnError)
} else {
CommandLine = NewFlagSet(os.Args[0], ExitOnError)
}
// Override generic FlagSet default Usage with call to global Usage.
// Note: This is not CommandLine.Usage = Usage,
// because we want any eventual call to use any updated value of Usage,