Use flag package for mkimage command line parsing

Test Plan: Covered by CI

X-Origin-Diff: phab/D248
GitOrigin-RevId: 3b59e11885c78e8321d8a44b22e67d85268b5765
diff --git a/core/BUILD b/core/BUILD
index edced0a..38160dc 100644
--- a/core/BUILD
+++ b/core/BUILD
@@ -8,7 +8,10 @@
         "smalltown.img",
     ],
     cmd = """
-    $(location @//core/cmd/mkimage) $(location @//core/build/linux_kernel:bzImage) $@ $(location @//core/build/linux_kernel:initramfs)
+    $(location @//core/cmd/mkimage) \
+        -efi $(location @//core/build/linux_kernel:bzImage) \
+        -initramfs $(location @//core/build/linux_kernel:initramfs) \
+        -out $@
     """,
     tools = [
         "@//core/cmd/mkimage",
diff --git a/core/cmd/mkimage/main.go b/core/cmd/mkimage/main.go
index a356ca5..f733e78 100644
--- a/core/cmd/mkimage/main.go
+++ b/core/cmd/mkimage/main.go
@@ -17,6 +17,7 @@
 package main
 
 import (
+	"flag"
 	"fmt"
 	"io/ioutil"
 	"os"
@@ -29,17 +30,25 @@
 
 var SmalltownDataPartition gpt.Type = gpt.Type("9eeec464-6885-414a-b278-4305c51f7966")
 
+var (
+	efiPayloadPath = flag.String("efi", "", "UEFI payload")
+	outputPath     = flag.String("out", "", "Output disk image")
+	initramfsPath  = flag.String("initramfs", "", "External initramfs [optional]")
+)
+
 func mibToSectors(size uint64) uint64 {
 	return (size * 1024 * 1024) / 512
 }
 
 func main() {
-	if len(os.Args) < 3 {
-		fmt.Println("Usage: mkimage <UEFI payload> <output image path> <initramfs (optional)>")
+	flag.Parse()
+	if *efiPayloadPath == "" || *outputPath == "" {
+		flag.PrintDefaults()
 		os.Exit(2)
 	}
-	_ = os.Remove(os.Args[2])
-	diskImg, err := diskfs.Create(os.Args[2], 3*1024*1024*1024, diskfs.Raw)
+
+	_ = os.Remove(*outputPath)
+	diskImg, err := diskfs.Create(*outputPath, 3*1024*1024*1024, diskfs.Raw)
 	if err != nil {
 		fmt.Printf("Failed to create disk: %v", err)
 		os.Exit(1)
@@ -89,7 +98,7 @@
 		fmt.Printf("Failed to open EFI payload for writing: %v", err)
 		os.Exit(1)
 	}
-	efiPayloadSrc, err := os.Open(os.Args[1])
+	efiPayloadSrc, err := os.Open(*efiPayloadPath)
 	if err != nil {
 		fmt.Printf("Failed to open EFI payload for reading: %v", err)
 		os.Exit(1)
@@ -110,8 +119,8 @@
 		os.Exit(1)
 	}
 	// If we have more than two arguments, the second one is the initramfs
-	if len(os.Args) > 3 {
-		initramfsSrc, err := os.Open(os.Args[3])
+	if *initramfsPath != "" {
+		initramfsSrc, err := os.Open(*initramfsPath)
 		if err != nil {
 			fmt.Printf("Failed to open initramfs for reading: %v", err)
 			os.Exit(1)