gameServer/Opts.hs
changeset 1804 4e78ad846fb6
child 1832 1fb61a53a2c2
equal deleted inserted replaced
1803:95efe37482e3 1804:4e78ad846fb6
       
     1 module Opts
       
     2 (
       
     3 	getOpts,
       
     4 ) where
       
     5 
       
     6 import System
       
     7 import System.Console.GetOpt
       
     8 import Network
       
     9 import Data.Maybe ( fromMaybe )
       
    10 import CoreTypes
       
    11 import Utils
       
    12 
       
    13 options :: [OptDescr (ServerInfo -> ServerInfo)]
       
    14 options = [
       
    15 	Option ['p'] ["port"] (ReqArg readListenPort "PORT") "listen on PORT",
       
    16 	Option ['d'] ["dedicated"] (ReqArg readDedicated "BOOL") "start as dedicated (True or False)",
       
    17 	Option []    ["password"] (ReqArg readPassword "STRING") "admin password"
       
    18 	]
       
    19 
       
    20 readListenPort, readDedicated, readPassword :: String -> ServerInfo -> ServerInfo
       
    21 readListenPort str opts = opts{listenPort = readPort}
       
    22 	where
       
    23 		readPort = fromInteger $ fromMaybe 46631 (maybeRead str :: Maybe Integer)
       
    24 
       
    25 readDedicated str opts = opts{isDedicated = readDedicated}
       
    26 	where
       
    27 		readDedicated = fromMaybe True (maybeRead str :: Maybe Bool)
       
    28 
       
    29 readPassword str opts = opts{adminPassword = str}
       
    30 
       
    31 getOpts :: ServerInfo -> IO ServerInfo
       
    32 getOpts opts = do
       
    33 	args <- getArgs
       
    34 	case getOpt Permute options args of
       
    35 		(o, [], []) -> return $ foldr ($) opts o
       
    36 		(_,_,errs) -> ioError (userError (concat errs ++ usageInfo header options))
       
    37 	where header = "Usage: newhwserv [OPTION...]"