1804
|
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",
|
1964
|
16 |
Option ['d'] ["dedicated"] (ReqArg readDedicated "BOOL") "start as dedicated (True or False)"
|
1804
|
17 |
]
|
|
18 |
|
1832
|
19 |
readListenPort,
|
|
20 |
readDedicated,
|
|
21 |
readDbLogin,
|
|
22 |
readDbPassword,
|
|
23 |
readDbHost :: String -> ServerInfo -> ServerInfo
|
|
24 |
|
1804
|
25 |
readListenPort str opts = opts{listenPort = readPort}
|
|
26 |
where
|
|
27 |
readPort = fromInteger $ fromMaybe 46631 (maybeRead str :: Maybe Integer)
|
|
28 |
|
|
29 |
readDedicated str opts = opts{isDedicated = readDedicated}
|
|
30 |
where
|
|
31 |
readDedicated = fromMaybe True (maybeRead str :: Maybe Bool)
|
|
32 |
|
1832
|
33 |
readDbLogin str opts = opts{dbLogin = str}
|
|
34 |
readDbPassword str opts = opts{dbPassword = str}
|
|
35 |
readDbHost str opts = opts{dbHost = str}
|
1804
|
36 |
|
|
37 |
getOpts :: ServerInfo -> IO ServerInfo
|
|
38 |
getOpts opts = do
|
|
39 |
args <- getArgs
|
|
40 |
case getOpt Permute options args of
|
|
41 |
(o, [], []) -> return $ foldr ($) opts o
|
|
42 |
(_,_,errs) -> ioError (userError (concat errs ++ usageInfo header options))
|
|
43 |
where header = "Usage: newhwserv [OPTION...]"
|