author | unc0rr |
Sun, 08 Mar 2009 20:22:52 +0000 | |
changeset 1874 | 1b145e099b75 |
parent 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", |
1757
3aa7d21baca1
Add an ability for global messages when server started with password option set
unc0rr
parents:
1492
diff
changeset
|
17 |
Option ['d'] ["dedicated"] (ReqArg readDedicated "BOOL") "start as dedicated (True or False)", |
3aa7d21baca1
Add an ability for global messages when server started with password option set
unc0rr
parents:
1492
diff
changeset
|
18 |
Option [] ["password"] (ReqArg readPassword "STRING") "admin password" |
1341
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
19 |
] |
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
20 |
|
1757
3aa7d21baca1
Add an ability for global messages when server started with password option set
unc0rr
parents:
1492
diff
changeset
|
21 |
readListenPort, readDedicated, readPassword :: String -> ServerInfo -> ServerInfo |
1383 | 22 |
readListenPort str opts = opts{listenPort = readPort} |
23 |
where |
|
24 |
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
|
25 |
|
1383 | 26 |
readDedicated str opts = opts{isDedicated = readDedicated} |
27 |
where |
|
28 |
readDedicated = fromMaybe True (maybeRead str :: Maybe Bool) |
|
29 |
||
1757
3aa7d21baca1
Add an ability for global messages when server started with password option set
unc0rr
parents:
1492
diff
changeset
|
30 |
readPassword str opts = opts{adminPassword = str} |
3aa7d21baca1
Add an ability for global messages when server started with password option set
unc0rr
parents:
1492
diff
changeset
|
31 |
|
1492 | 32 |
getOpts :: ServerInfo -> IO ServerInfo |
33 |
getOpts opts = do |
|
1341
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
34 |
args <- getArgs |
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
35 |
case getOpt Permute options args of |
1492 | 36 |
(o, [], []) -> return $ foldr ($) opts o |
1341
86d7d5ab22a2
Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff
changeset
|
37 |
(_,_,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
|
38 |
where header = "Usage: newhwserv [OPTION...]" |