author | unc0rr |
Fri, 07 Nov 2008 22:03:43 +0000 | |
changeset 1483 | 89e24edb6020 |
parent 1473 | 60e1fad78d58 |
child 1492 | 2da1fe033f23 |
permissions | -rw-r--r-- |
1473 | 1 |
module Opts |
2 |
( |
|
3 |
GlobalOptions(..), |
|
4 |
globalOptions |
|
5 |
) where |
|
1341
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
6 |
|
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
7 |
import System |
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
8 |
import System.Console.GetOpt |
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
9 |
import Network |
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
10 |
import Data.Maybe ( fromMaybe ) |
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
11 |
import Miscutils |
1383 | 12 |
import System.IO.Unsafe |
1341
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
13 |
|
1383 | 14 |
data GlobalOptions = |
15 |
GlobalOptions |
|
16 |
{ |
|
17 |
isDedicated :: Bool, |
|
18 |
serverMessage :: String, |
|
19 |
listenPort :: PortNumber |
|
20 |
} |
|
21 |
defaultMessage = "<h2><p align=center><a href=\"http://www.hedgewars.org/\">http://www.hedgewars.org/</a></p></h2>" |
|
1386 | 22 |
defaultOptions = (GlobalOptions True defaultMessage 46631) |
1383 | 23 |
|
24 |
options :: [OptDescr (GlobalOptions -> GlobalOptions)] |
|
1341
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
25 |
options = [ |
1383 | 26 |
Option ['p'] ["port"] (ReqArg readListenPort "PORT") "listen on PORT", |
27 |
Option ['d'] ["dedicated"] (ReqArg readDedicated "BOOL") "start as dedicated (True or False)" |
|
1341
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
28 |
] |
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
29 |
|
1383 | 30 |
readListenPort, readDedicated :: String -> GlobalOptions -> GlobalOptions |
31 |
readListenPort str opts = opts{listenPort = readPort} |
|
32 |
where |
|
33 |
readPort = fromInteger $ fromMaybe 46631 (maybeRead str :: Maybe Integer) |
|
1341
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
34 |
|
1383 | 35 |
readDedicated str opts = opts{isDedicated = readDedicated} |
36 |
where |
|
37 |
readDedicated = fromMaybe True (maybeRead str :: Maybe Bool) |
|
38 |
||
39 |
opts :: IO GlobalOptions |
|
1341
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
40 |
opts = do |
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
41 |
args <- getArgs |
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
42 |
case getOpt Permute options args of |
1383 | 43 |
(o, [], []) -> return $ foldr ($) defaultOptions o |
1341
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
44 |
(_,_,errs) -> ioError (userError (concat errs ++ usageInfo header options)) |
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
45 |
where header = "Usage: newhwserv [OPTION...]" |
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
46 |
|
1383 | 47 |
{-# NOINLINE globalOptions #-} |
48 |
globalOptions = unsafePerformIO opts |