gameServer/Opts.hs
author unc0rr
Thu, 19 Feb 2009 14:52:32 +0000
changeset 1810 4059cafd1da7
parent 1804 4e78ad846fb6
child 1832 1fb61a53a2c2
permissions -rw-r--r--
Frontend look fixes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     1
module Opts
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     2
(
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     3
	getOpts,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     4
) where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     5
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     6
import System
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     7
import System.Console.GetOpt
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     8
import Network
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     9
import Data.Maybe ( fromMaybe )
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    10
import CoreTypes
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    11
import Utils
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    12
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    13
options :: [OptDescr (ServerInfo -> ServerInfo)]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    14
options = [
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    15
	Option ['p'] ["port"] (ReqArg readListenPort "PORT") "listen on PORT",
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    16
	Option ['d'] ["dedicated"] (ReqArg readDedicated "BOOL") "start as dedicated (True or False)",
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    17
	Option []    ["password"] (ReqArg readPassword "STRING") "admin password"
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    18
	]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    19
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    20
readListenPort, readDedicated, readPassword :: String -> ServerInfo -> ServerInfo
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    21
readListenPort str opts = opts{listenPort = readPort}
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    22
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    23
		readPort = fromInteger $ fromMaybe 46631 (maybeRead str :: Maybe Integer)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    24
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    25
readDedicated str opts = opts{isDedicated = readDedicated}
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    26
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    27
		readDedicated = fromMaybe True (maybeRead str :: Maybe Bool)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    28
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    29
readPassword str opts = opts{adminPassword = str}
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    30
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    31
getOpts :: ServerInfo -> IO ServerInfo
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    32
getOpts opts = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    33
	args <- getArgs
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    34
	case getOpt Permute options args of
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    35
		(o, [], []) -> return $ foldr ($) opts o
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    36
		(_,_,errs) -> ioError (userError (concat errs ++ usageInfo header options))
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    37
	where header = "Usage: newhwserv [OPTION...]"