netserver/Opts.hs
author unc0rr
Wed, 15 Oct 2008 15:02:39 +0000
changeset 1356 77ab8735401d
parent 1342 ae6c4f10ace2
child 1383 d20e6e8928e3
permissions -rw-r--r--
Share chat between frontend and engine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
     8
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
     9
data Flag = ListenPort PortNumber
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    10
	deriving Show
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    11
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    12
options :: [OptDescr Flag]
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    13
options = [
1342
ae6c4f10ace2 Rename function
unc0rr
parents: 1341
diff changeset
    14
	Option ['p'] ["port"] (OptArg readPort "PORT") "listen on PORT"
1341
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    15
	]
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    16
1342
ae6c4f10ace2 Rename function
unc0rr
parents: 1341
diff changeset
    17
readPort :: Maybe String -> Flag
ae6c4f10ace2 Rename function
unc0rr
parents: 1341
diff changeset
    18
readPort str = ListenPort $ fromInteger (fromMaybe 46631 (maybeRead (fromMaybe "46631" str) :: Maybe Integer))
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
opts :: IO [Flag]
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    21
opts = do
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    22
	args <- getArgs
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    23
	case getOpt Permute options args of
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    24
		(o, [], []) -> return o
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    25
		(_,_,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
    26
	where header = "Usage: newhwserv [OPTION...]"
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    27
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    28
getPort :: [Flag] -> PortNumber
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    29
getPort [] = 46631
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    30
getPort (ListenPort a:flags) = a