Skip to content

Commit 602bf11

Browse files
committed
Split argument parsing into a dedicated function
Even though this was all main had to do, it still feels better having it split.
1 parent 8cf99a5 commit 602bf11

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

cmd/multibuild/main.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,13 @@ type cliArgs struct {
6161
// (e.g. multibuild foo/main.go)
6262
sources []string
6363

64+
displayUsage bool
6465
displayConfig bool
6566
displayTargets bool
6667
verbose bool
6768
}
6869

69-
func main() {
70+
func buildArgs() (cliArgs, error) {
7071
args := cliArgs{}
7172
args.self = filepath.Base(os.Args[0])
7273
args.goBuildArgs = os.Args[1:]
@@ -83,15 +84,16 @@ func main() {
8384
args.output = strings.TrimPrefix(arg, "-o=")
8485

8586
case arg == "-h" || arg == "--help":
86-
displayUsageAndExit(args.self)
87+
args.displayUsage = true
88+
8789
case arg == "-v":
8890
args.verbose = true
8991
case arg == "--multibuild-configuration":
9092
args.displayConfig = true
9193
case arg == "--multibuild-targets":
9294
args.displayTargets = true
9395
case strings.HasPrefix(arg, "--multibuild"):
94-
fatal("multibuild: unrecognized argument %q", arg)
96+
return cliArgs{}, fmt.Errorf("multibuild: unrecognized argument %q", arg)
9597
case !strings.HasPrefix(arg, "-"):
9698
if args.packagePath != "" {
9799
// For now, I'm cowardly refusing to handle this.
@@ -105,7 +107,7 @@ func main() {
105107
//
106108
// We will need to discover sources for each package, scan independently,
107109
// and build independently.
108-
fatal("multibuild: cannot build multiple packages")
110+
return cliArgs{}, fmt.Errorf("multibuild: cannot build multiple packages")
109111
}
110112
args.packagePath = arg
111113
}
@@ -139,5 +141,18 @@ func main() {
139141
}
140142
}
141143

144+
return args, nil
145+
}
146+
147+
func main() {
148+
args, err := buildArgs()
149+
if err != nil {
150+
fatal(err.Error())
151+
}
152+
153+
if args.displayUsage {
154+
displayUsageAndExit(args.self)
155+
}
156+
142157
doMultibuild(args)
143158
}

0 commit comments

Comments
 (0)