|
1 {-# LANGUAGE CPP #-} |
1 module Opts |
2 module Opts |
2 ( |
3 ( |
3 getOpts, |
4 getOpts, |
4 ) where |
5 ) where |
5 |
6 |
6 import System.Environment |
7 import System.Environment |
7 import System.Console.GetOpt |
8 import System.Console.GetOpt |
|
9 import Data.Maybe ( fromMaybe ) |
|
10 #if defined(OFFICIAL_SERVER) |
|
11 import qualified Data.ByteString.Char8 as B |
8 import Network |
12 import Network |
9 import Data.Maybe ( fromMaybe ) |
13 #endif |
10 import qualified Data.ByteString.Char8 as B |
14 ------------------- |
11 |
|
12 import CoreTypes |
15 import CoreTypes |
13 import Utils |
16 import Utils |
14 |
17 |
15 options :: [OptDescr (ServerInfo -> ServerInfo)] |
18 options :: [OptDescr (ServerInfo -> ServerInfo)] |
16 options = [ |
19 options = [ |
17 Option ['p'] ["port"] (ReqArg readListenPort "PORT") "listen on PORT", |
20 Option "p" ["port"] (ReqArg readListenPort "PORT") "listen on PORT", |
18 Option ['d'] ["dedicated"] (ReqArg readDedicated "BOOL") "start as dedicated (True or False)" |
21 Option "d" ["dedicated"] (ReqArg readDedicated "BOOL") "start as dedicated (True or False)" |
19 ] |
22 ] |
20 |
23 |
21 readListenPort, |
24 readListenPort |
22 readDedicated, |
25 , readDedicated |
23 readDbLogin, |
26 #if defined(OFFICIAL_SERVER) |
24 readDbPassword, |
27 , readDbLogin |
25 readDbHost :: String -> ServerInfo -> ServerInfo |
28 , readDbPassword |
|
29 readDbHost |
|
30 #endif |
|
31 :: String -> ServerInfo -> ServerInfo |
|
32 |
26 |
33 |
27 readListenPort str opts = opts{listenPort = readPort} |
34 readListenPort str opts = opts{listenPort = readPort} |
28 where |
35 where |
29 readPort = fromInteger $ fromMaybe 46631 (maybeRead str :: Maybe Integer) |
36 readPort = fromInteger $ fromMaybe 46631 (maybeRead str :: Maybe Integer) |
30 |
37 |
31 readDedicated str opts = opts{isDedicated = readDedicated} |
38 readDedicated str opts = opts{isDedicated = readDed} |
32 where |
39 where |
33 readDedicated = fromMaybe True (maybeRead str :: Maybe Bool) |
40 readDed = fromMaybe True (maybeRead str :: Maybe Bool) |
34 |
41 |
|
42 #if defined(OFFICIAL_SERVER) |
35 readDbLogin str opts = opts{dbLogin = B.pack str} |
43 readDbLogin str opts = opts{dbLogin = B.pack str} |
36 readDbPassword str opts = opts{dbPassword = B.pack str} |
44 readDbPassword str opts = opts{dbPassword = B.pack str} |
37 readDbHost str opts = opts{dbHost = B.pack str} |
45 readDbHost str opts = opts{dbHost = B.pack str} |
|
46 #endif |
38 |
47 |
39 getOpts :: ServerInfo -> IO ServerInfo |
48 getOpts :: ServerInfo -> IO ServerInfo |
40 getOpts opts = do |
49 getOpts opts = do |
41 args <- getArgs |
50 args <- getArgs |
42 case getOpt Permute options args of |
51 case getOpt Permute options args of |