netserver/Opts.hs
author unc0rr
Mon, 17 Nov 2008 20:35:11 +0000
changeset 1501 a0e56fdf10cd
parent 1492 2da1fe033f23
child 1757 3aa7d21baca1
permissions -rw-r--r--
Tune blowtorch a bit
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",
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    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
2da1fe033f23 Finish refactoring
unc0rr
parents: 1473
diff changeset
    20
readListenPort, readDedicated :: String -> ServerInfo -> ServerInfo
1383
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    21
readListenPort str opts = opts{listenPort = readPort}
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    22
	where
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    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
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    25
readDedicated str opts = opts{isDedicated = readDedicated}
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    26
	where
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    27
		readDedicated = fromMaybe True (maybeRead str :: Maybe Bool)
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    28
1492
2da1fe033f23 Finish refactoring
unc0rr
parents: 1473
diff changeset
    29
getOpts :: ServerInfo -> IO ServerInfo
2da1fe033f23 Finish refactoring
unc0rr
parents: 1473
diff changeset
    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
2da1fe033f23 Finish refactoring
unc0rr
parents: 1473
diff changeset
    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...]"