netserver/Opts.hs
author unc0rr
Tue, 17 Feb 2009 12:58:08 +0000
changeset 1801 bc0c5c21376e
parent 1757 3aa7d21baca1
permissions -rw-r--r--
Clouds fixes by nemo
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1473
60e1fad78d58 Cleanup code a bit, some reformatting
unc0rr
parents: 1386
diff changeset
     1
module Opts
60e1fad78d58 Cleanup code a bit, some reformatting
unc0rr
parents: 1386
diff changeset
     2
(
1492
2da1fe033f23 Finish refactoring
unc0rr
parents: 1473
diff changeset
     3
	getOpts,
1473
60e1fad78d58 Cleanup code a bit, some reformatting
unc0rr
parents: 1386
diff changeset
     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
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    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
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    13
1492
2da1fe033f23 Finish refactoring
unc0rr
parents: 1473
diff changeset
    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
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    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
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    22
readListenPort str opts = opts{listenPort = readPort}
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    23
	where
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    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
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    26
readDedicated str opts = opts{isDedicated = readDedicated}
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    27
	where
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    28
		readDedicated = fromMaybe True (maybeRead str :: Maybe Bool)
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    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
2da1fe033f23 Finish refactoring
unc0rr
parents: 1473
diff changeset
    32
getOpts :: ServerInfo -> IO ServerInfo
2da1fe033f23 Finish refactoring
unc0rr
parents: 1473
diff changeset
    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
2da1fe033f23 Finish refactoring
unc0rr
parents: 1473
diff changeset
    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...]"