gameServer/Opts.hs
author sheepluva
Thu, 15 Sep 2011 09:19:56 +0200
changeset 5911 4a28d0ae9624
parent 5210 a5329e52a71b
child 10460 8dcea9087d75
permissions -rw-r--r--
based on the fact that the crash never seemed to have happened with default screen resolutions and also inspired by the only other getmem usage in the code I came up with this simple solution which seems to work just fine. I do not understand why getmem seems to need this, but well - feel free to explain.

{-# LANGUAGE CPP #-}
module Opts
(
    getOpts,
) where

import System.Environment
import System.Console.GetOpt
import Data.Maybe ( fromMaybe )
-------------------
import CoreTypes
import Utils

options :: [OptDescr (ServerInfo -> ServerInfo)]
options = [
    Option "p" ["port"] (ReqArg readListenPort "PORT") "listen on PORT",
    Option "d" ["dedicated"] (ReqArg readDedicated "BOOL") "start as dedicated (True or False)"
    ]

readListenPort
    , readDedicated
    :: String -> ServerInfo -> ServerInfo


readListenPort str opts = opts{listenPort = readPort}
    where
        readPort = fromInteger $ fromMaybe 46631 (maybeRead str :: Maybe Integer)

readDedicated str opts = opts{isDedicated = readDed}
    where
        readDed = fromMaybe True (maybeRead str :: Maybe Bool)

getOpts :: ServerInfo -> IO ServerInfo
getOpts opts = do
    args <- getArgs
    case getOpt Permute options args of
        (o, [], []) -> return $ foldr ($) opts{runArgs = args} o
        (_,_,errs) -> ioError (userError (concat errs ++ usageInfo header options))
    where header = "Usage: hedgewars-server [OPTION...]"