Support more ways to pass -j/-k

In preparation to remove Make/makeparallel from soong_ui startup, we
need to preserve compatibility with the different ways that make
supports the -j option.

Nothing changes unless Make/makeparallel is removed from the startup.
Once that is removed, not specifying a -j value will be equivalent to
'-j' instead of '-j1', like Ninja. A value will also be supported when
specifying -k, like Ninja (though specifying it alone will be equivalent
to '-k 0').

Test: m -j blueprint_tools
Change-Id: I9d5d59bedd4f6e5cca76bdb4cd47e0b5b7d523f0
diff --git a/ui/build/config.go b/ui/build/config.go
index 16826f2..925b153 100644
--- a/ui/build/config.go
+++ b/ui/build/config.go
@@ -53,6 +53,12 @@
 		environ: OsEnvironment(),
 	}
 
+	// Sane default matching ninja
+	ret.parallel = runtime.NumCPU() + 2
+	ret.keepGoing = 1
+
+	ret.parseArgs(ctx, args)
+
 	// Make sure OUT_DIR is set appropriately
 	if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
 		ret.environ.Set("OUT_DIR", filepath.Clean(outDir))
@@ -97,10 +103,6 @@
 	// Tell python not to spam the source tree with .pyc files.
 	ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
 
-	// Sane default matching ninja
-	ret.parallel = runtime.NumCPU() + 2
-	ret.keepGoing = 1
-
 	// Precondition: the current directory is the top of the source tree
 	if _, err := os.Stat(srcDirFileCheck); err != nil {
 		if os.IsNotExist(err) {
@@ -135,38 +137,49 @@
 		log.Fatalln("Directory names containing spaces are not supported")
 	}
 
-	for _, arg := range args {
-		arg = strings.TrimSpace(arg)
+	return Config{ret}
+}
+
+func (c *configImpl) parseArgs(ctx Context, args []string) {
+	for i := 0; i < len(args); i++ {
+		arg := strings.TrimSpace(args[i])
 		if arg == "--make-mode" {
 			continue
 		} else if arg == "showcommands" {
-			ret.verbose = true
+			c.verbose = true
 			continue
 		} else if arg == "dist" {
-			ret.dist = true
+			c.dist = true
 		}
 		if arg[0] == '-' {
-			var err error
+			parseArgNum := func(def int) int {
+				if len(arg) > 2 {
+					p, err := strconv.ParseUint(arg[2:], 10, 31)
+					if err != nil {
+						ctx.Fatalf("Failed to parse %q: %v", arg, err)
+					}
+					return int(p)
+				} else if i+1 < len(args) {
+					p, err := strconv.ParseUint(args[i+1], 10, 31)
+					if err == nil {
+						i++
+						return int(p)
+					}
+				}
+				return def
+			}
+
 			if arg[1] == 'j' {
-				// TODO: handle space between j and number
-				// Unnecessary if used with makeparallel
-				ret.parallel, err = strconv.Atoi(arg[2:])
+				c.parallel = parseArgNum(c.parallel)
 			} else if arg[1] == 'k' {
-				// TODO: handle space between k and number
-				// Unnecessary if used with makeparallel
-				ret.keepGoing, err = strconv.Atoi(arg[2:])
+				c.keepGoing = parseArgNum(0)
 			} else {
 				ctx.Fatalln("Unknown option:", arg)
 			}
-			if err != nil {
-				ctx.Fatalln("Argument error:", err, arg)
-			}
 		} else {
-			ret.arguments = append(ret.arguments, arg)
+			c.arguments = append(c.arguments, arg)
 		}
 	}
-
-	return Config{ret}
 }
 
 // Lunch configures the environment for a specific product similarly to the