author | unc0rr |
Wed, 16 Feb 2011 13:07:00 +0300 | |
changeset 4944 | e43a3da2fc22 |
parent 4932 | f11d80bac7ed |
child 4957 | 3684faf5b3d1 |
permissions | -rw-r--r-- |
4932 | 1 |
{-# LANGUAGE CPP #-} |
1804 | 2 |
module Opts |
3 |
( |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
1964
diff
changeset
|
4 |
getOpts, |
1804 | 5 |
) where |
6 |
||
4905 | 7 |
import System.Environment |
1804 | 8 |
import System.Console.GetOpt |
4932 | 9 |
import Data.Maybe ( fromMaybe ) |
10 |
#if defined(OFFICIAL_SERVER) |
|
11 |
import qualified Data.ByteString.Char8 as B |
|
1804 | 12 |
import Network |
4932 | 13 |
#endif |
14 |
------------------- |
|
1804 | 15 |
import CoreTypes |
16 |
import Utils |
|
17 |
||
18 |
options :: [OptDescr (ServerInfo -> ServerInfo)] |
|
19 |
options = [ |
|
4932 | 20 |
Option "p" ["port"] (ReqArg readListenPort "PORT") "listen on PORT", |
21 |
Option "d" ["dedicated"] (ReqArg readDedicated "BOOL") "start as dedicated (True or False)" |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
1964
diff
changeset
|
22 |
] |
1804 | 23 |
|
4932 | 24 |
readListenPort |
25 |
, readDedicated |
|
26 |
#if defined(OFFICIAL_SERVER) |
|
27 |
, readDbLogin |
|
28 |
, readDbPassword |
|
29 |
readDbHost |
|
30 |
#endif |
|
31 |
:: String -> ServerInfo -> ServerInfo |
|
32 |
||
1832 | 33 |
|
1804 | 34 |
readListenPort str opts = opts{listenPort = readPort} |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
1964
diff
changeset
|
35 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
1964
diff
changeset
|
36 |
readPort = fromInteger $ fromMaybe 46631 (maybeRead str :: Maybe Integer) |
1804 | 37 |
|
4932 | 38 |
readDedicated str opts = opts{isDedicated = readDed} |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
1964
diff
changeset
|
39 |
where |
4932 | 40 |
readDed = fromMaybe True (maybeRead str :: Maybe Bool) |
1804 | 41 |
|
4932 | 42 |
#if defined(OFFICIAL_SERVER) |
4905 | 43 |
readDbLogin str opts = opts{dbLogin = B.pack str} |
44 |
readDbPassword str opts = opts{dbPassword = B.pack str} |
|
45 |
readDbHost str opts = opts{dbHost = B.pack str} |
|
4932 | 46 |
#endif |
1804 | 47 |
|
48 |
getOpts :: ServerInfo -> IO ServerInfo |
|
49 |
getOpts opts = do |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
1964
diff
changeset
|
50 |
args <- getArgs |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
1964
diff
changeset
|
51 |
case getOpt Permute options args of |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
1964
diff
changeset
|
52 |
(o, [], []) -> return $ foldr ($) opts o |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
1964
diff
changeset
|
53 |
(_,_,errs) -> ioError (userError (concat errs ++ usageInfo header options)) |
2966 | 54 |
where header = "Usage: hedgewars-server [OPTION...]" |