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