parseBuildArgs :: GhcMajorVersion -> [String] -> IO BuildArgs
parseBuildArgs version =
    loop BuildArgs
        { sandbox = sandboxRoot $ defaultBuildSettings Nothing version
        , buildPlanSrc = defaultBuildPlan
        , extraArgs' = id
        , noDocs = False
        , buildCores = Nothing
        , testThreads = Nothing
        }
  where
    loop x [] = return x
    loop x ("--sandbox":y:rest) = loop x { sandbox = y } rest
    loop x ("--build-plan":y:rest) = loop x { buildPlanSrc = y } rest
    loop x ("--arg":y:rest) = loop x { extraArgs' = extraArgs' x . (y:) } rest
    loop x ("--no-docs":rest) = loop x { noDocs = True } rest
    loop x ("-j":y:rest) = loop x { buildCores = Just $ read y } rest
    loop x ("--test-threads":y:rest) = loop x { testThreads = Just $ read y } rest
    loop _ (y:_) = error $ "Did not understand argument: " ++ y
parseBuildArgs :: GhcMajorVersion -> [String] -> IO BuildArgs
parseBuildArgs version =
    loop BuildArgs
        { sandbox = sandboxRoot $ defaultBuildSettings Nothing version
        , buildPlanSrc = defaultBuildPlan
        , extraArgs' = id
        , noDocs = False
        , buildCores = Nothing
        , testThreads = Nothing
        }
  where
    loop x [] = return x
    loop x ("--sandbox":y:rest) = loop x { sandbox = y } rest
    loop x ("--build-plan":y:rest) = loop x { buildPlanSrc = y } rest
    loop x ("--arg":y:rest) = loop x { extraArgs' = extraArgs' x . (y:) } rest
    loop x ("--no-docs":rest) = loop x { noDocs = True } rest
    loop x ("-j":y:rest) = loop x { buildCores = Just $ read y } rest
    loop x ("--test-threads":y:rest) = loop x { testThreads = Just $ read y } rest
    loop _ (y:_) = error $ "Did not understand argument: " ++ y
parseBuildArgs version =
    loop BuildArgs
        { sandbox = sandboxRoot $ defaultBuildSettings Nothing version
        , buildPlanSrc = defaultBuildPlan
        , extraArgs' = id
        , noDocs = False
        , buildCores = Nothing
        , testThreads = Nothing
        }
  where
    loop x [] = return x
    loop x ("--sandbox":y:rest) = loop x { sandbox = y } rest
    loop x ("--build-plan":y:rest) = loop x { buildPlanSrc = y } rest
    loop x ("--arg":y:rest) = loop x { extraArgs' = extraArgs' x . (y:) } rest
    loop x ("--no-docs":rest) = loop x { noDocs = True } rest
    loop x ("-j":y:rest) = loop x { buildCores = Just $ read y } rest
    loop x ("--test-threads":y:rest) = loop x { testThreads = Just $ read y } rest
    loop _ (y:_) = error $ "Did not understand argument: " ++ y