author | unc0rr |
Wed, 14 Jan 2009 19:27:23 +0000 | |
changeset 1673 | 06bff12f8a74 |
parent 1492 | 2da1fe033f23 |
child 1757 | 3aa7d21baca1 |
permissions | -rw-r--r-- |
1473 | 1 |
module Opts |
2 |
( |
|
1492 | 3 |
getOpts, |
1473 | 4 |
) where |
1341
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
5 |
|
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
6 |
import System |
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
7 |
import System.Console.GetOpt |
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
8 |
import Network |
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
9 |
import Data.Maybe ( fromMaybe ) |
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
10 |
import Miscutils |
1383 | 11 |
import System.IO.Unsafe |
1341
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
12 |
|
1383 | 13 |
|
1492 | 14 |
options :: [OptDescr (ServerInfo -> ServerInfo)] |
1341
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
15 |
options = [ |
1383 | 16 |
Option ['p'] ["port"] (ReqArg readListenPort "PORT") "listen on PORT", |
17 |
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
|
18 |
] |
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
19 |
|
1492 | 20 |
readListenPort, readDedicated :: String -> ServerInfo -> ServerInfo |
1383 | 21 |
readListenPort str opts = opts{listenPort = readPort} |
22 |
where |
|
23 |
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
|
24 |
|
1383 | 25 |
readDedicated str opts = opts{isDedicated = readDedicated} |
26 |
where |
|
27 |
readDedicated = fromMaybe True (maybeRead str :: Maybe Bool) |
|
28 |
||
1492 | 29 |
getOpts :: ServerInfo -> IO ServerInfo |
30 |
getOpts opts = do |
|
1341
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
31 |
args <- getArgs |
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
32 |
case getOpt Permute options args of |
1492 | 33 |
(o, [], []) -> return $ foldr ($) opts o |
1341
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
34 |
(_,_,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
|
35 |
where header = "Usage: newhwserv [OPTION...]" |