gameServer/Opts.hs
author unc0rr
Fri, 03 Apr 2009 16:35:59 +0000
changeset 1940 bbdca883b5f9
parent 1832 1fb61a53a2c2
child 1964 dc9ea05c9d2f
permissions -rw-r--r--
- Enable back border of drop-down list (by nemo) - Move const visibility to public... probably I'll make use of it in future (by me)

module Opts
(
	getOpts,
) where

import System
import System.Console.GetOpt
import Network
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)",
	Option []    ["db-login"] (ReqArg readDbLogin "STRING") "database access login",
	Option []    ["db-password"] (ReqArg readDbPassword "STRING") "database access password",
	Option []    ["db-host"] (ReqArg readDbHost "STRING") "database host"
	]

readListenPort,
	readDedicated,
	readDbLogin,
	readDbPassword,
	readDbHost :: String -> ServerInfo -> ServerInfo

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

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

readDbLogin str opts = opts{dbLogin = str}
readDbPassword str opts = opts{dbPassword = str}
readDbHost str opts = opts{dbHost = str}

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