netserver/Opts.hs
author unc0rr
Fri, 10 Oct 2008 13:18:45 +0000
changeset 1341 86d7d5ab22a2
child 1342 ae6c4f10ace2
permissions -rw-r--r--
Allow --port=PORT command-line parameter to specify the port to listen on
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 = [
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    14
	Option ['p'] ["port"] (OptArg defaultPort "PORT") "listen on PORT"
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
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    17
defaultPort :: Maybe String -> Flag
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    18
defaultPort str = ListenPort $ fromInteger (fromMaybe 46631 (maybeRead (fromMaybe "46631" str) :: Maybe Integer))
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