parseSelectArgs :: [String] -> IO SelectArgs
parseSelectArgs =
    loop SelectArgs
        { excluded = []
        , noPlatform = True
        , ignoreUpgradeable = False
        , onlyPermissive = False
        , allowed = []
        , buildPlanDest = defaultBuildPlan
        , globalDB = False
        }
  where
    loop x [] = return x
    loop x ("--exclude":y:rest) = loop x { excluded = y : excluded x } rest
    loop x ("--no-platform":rest) = loop x { noPlatform = True } rest
    loop x ("--platform":rest) = loop x { noPlatform = False } rest
    loop x ("--ignore-upgradeable":rest) = loop x { ignoreUpgradeable = True } rest
    loop x ("--only-permissive":rest) = loop x { onlyPermissive = True } rest
    loop x ("--allow":y:rest) = loop x { allowed = y : allowed x } rest
    loop x ("--build-plan":y:rest) = loop x { buildPlanDest = y } rest
    loop x ("--use-global-db":rest) = loop x { globalDB = True } rest
    loop _ (y:_) = error $ "Did not understand argument: " ++ y
parseSelectArgs :: [String] -> IO SelectArgs
parseSelectArgs =
    loop SelectArgs
        { excluded = []
        , noPlatform = True
        , ignoreUpgradeable = False
        , onlyPermissive = False
        , allowed = []
        , buildPlanDest = defaultBuildPlan
        , globalDB = False
        }
  where
    loop x [] = return x
    loop x ("--exclude":y:rest) = loop x { excluded = y : excluded x } rest
    loop x ("--no-platform":rest) = loop x { noPlatform = True } rest
    loop x ("--platform":rest) = loop x { noPlatform = False } rest
    loop x ("--ignore-upgradeable":rest) = loop x { ignoreUpgradeable = True } rest
    loop x ("--only-permissive":rest) = loop x { onlyPermissive = True } rest
    loop x ("--allow":y:rest) = loop x { allowed = y : allowed x } rest
    loop x ("--build-plan":y:rest) = loop x { buildPlanDest = y } rest
    loop x ("--use-global-db":rest) = loop x { globalDB = True } rest
    loop _ (y:_) = error $ "Did not understand argument: " ++ y
parseSelectArgs =
    loop SelectArgs
        { excluded = []
        , noPlatform = True
        , ignoreUpgradeable = False
        , onlyPermissive = False
        , allowed = []
        , buildPlanDest = defaultBuildPlan
        , globalDB = False
        }
  where
    loop x [] = return x
    loop x ("--exclude":y:rest) = loop x { excluded = y : excluded x } rest
    loop x ("--no-platform":rest) = loop x { noPlatform = True } rest
    loop x ("--platform":rest) = loop x { noPlatform = False } rest
    loop x ("--ignore-upgradeable":rest) = loop x { ignoreUpgradeable = True } rest
    loop x ("--only-permissive":rest) = loop x { onlyPermissive = True } rest
    loop x ("--allow":y:rest) = loop x { allowed = y : allowed x } rest
    loop x ("--build-plan":y:rest) = loop x { buildPlanDest = y } rest
    loop x ("--use-global-db":rest) = loop x { globalDB = True } rest
    loop _ (y:_) = error $ "Did not understand argument: " ++ y