Skip to content

Commit a0db2ef

Browse files
committed
cmds: don't call flag.Parse in init
When flag.Parse is in main, it is _only_ called in the command binary. When flag.Parse is in init, it is _also_ called in the test binary. The test binary, however, likes to register its own flags, which due to init ordering usually happens after the command's inits. In Go 1.13, flag registration was delayed to be later in the init process, which is how this issue came up. See also golang/go#31859 Signed-off-by: Chris Koch <chrisko@google.com>
1 parent e4d9040 commit a0db2ef

File tree

3 files changed

+4
-3
lines changed

3 files changed

+4
-3
lines changed

cmds/core/date/date.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ func init() {
5959
}
6060
flag.BoolVar(&flags.universal, "u", false, "Coordinated Universal Time (UTC)")
6161
flag.StringVar(&flags.reference, "r", "", "Display the last midification time of FILE")
62-
flag.Parse()
6362
}
6463

6564
// regex search for +format POSIX patterns
@@ -212,6 +211,8 @@ func date(t time.Time, z *time.Location) string {
212211
}
213212

214213
func main() {
214+
flag.Parse()
215+
215216
t := time.Now()
216217
z := time.Local
217218
if flags.universal {

cmds/core/mkfifo/mkfifo.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ func init() {
3333
os.Args[0] = cmd
3434
defUsage()
3535
}
36-
flag.Parse()
3736
}
3837

3938
func main() {

cmds/core/readlink/readlink.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ func init() {
3535
os.Args[0] = cmd
3636
defUsage()
3737
}
38-
flag.Parse()
3938
}
4039

4140
func readLink(file string) error {
@@ -57,6 +56,8 @@ func readLink(file string) error {
5756
}
5857

5958
func main() {
59+
flag.Parse()
60+
6061
var exitStatus int
6162

6263
for _, file := range flag.Args() {

0 commit comments

Comments
 (0)