netserver/Opts.hs
changeset 1965 340bfd438ca5
parent 1964 dc9ea05c9d2f
child 1966 31e449e1d9dd
equal deleted inserted replaced
1964:dc9ea05c9d2f 1965:340bfd438ca5
     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 Miscutils
       
    11 import System.IO.Unsafe
       
    12 
       
    13 
       
    14 options :: [OptDescr (ServerInfo -> ServerInfo)]
       
    15 options = [
       
    16 	Option ['p'] ["port"] (ReqArg readListenPort "PORT") "listen on PORT",
       
    17 	Option ['d'] ["dedicated"] (ReqArg readDedicated "BOOL") "start as dedicated (True or False)",
       
    18 	Option []    ["password"] (ReqArg readPassword "STRING") "admin password"
       
    19 	]
       
    20 
       
    21 readListenPort, readDedicated, readPassword :: String -> ServerInfo -> ServerInfo
       
    22 readListenPort str opts = opts{listenPort = readPort}
       
    23 	where
       
    24 		readPort = fromInteger $ fromMaybe 46631 (maybeRead str :: Maybe Integer)
       
    25 
       
    26 readDedicated str opts = opts{isDedicated = readDedicated}
       
    27 	where
       
    28 		readDedicated = fromMaybe True (maybeRead str :: Maybe Bool)
       
    29 
       
    30 readPassword str opts = opts{adminPassword = str}
       
    31 
       
    32 getOpts :: ServerInfo -> IO ServerInfo
       
    33 getOpts opts = do
       
    34 	args <- getArgs
       
    35 	case getOpt Permute options args of
       
    36 		(o, [], []) -> return $ foldr ($) opts o
       
    37 		(_,_,errs) -> ioError (userError (concat errs ++ usageInfo header options))
       
    38 	where header = "Usage: newhwserv [OPTION...]"