netserver/Opts.hs
author unc0rr
Thu, 16 Oct 2008 16:29:47 +0000
changeset 1365 2ffe20744a4a
parent 1342 ae6c4f10ace2
child 1383 d20e6e8928e3
permissions -rw-r--r--
- Fix crash when changing maps - Fix message not sent when switching to generated map

module Opts where

import System
import System.Console.GetOpt
import Network
import Data.Maybe ( fromMaybe )
import Miscutils

data Flag = ListenPort PortNumber
	deriving Show

options :: [OptDescr Flag]
options = [
	Option ['p'] ["port"] (OptArg readPort "PORT") "listen on PORT"
	]

readPort :: Maybe String -> Flag
readPort str = ListenPort $ fromInteger (fromMaybe 46631 (maybeRead (fromMaybe "46631" str) :: Maybe Integer))

opts :: IO [Flag]
opts = do
	args <- getArgs
	case getOpt Permute options args of
		(o, [], []) -> return o
		(_,_,errs) -> ioError (userError (concat errs ++ usageInfo header options))
	where header = "Usage: newhwserv [OPTION...]"

getPort :: [Flag] -> PortNumber
getPort [] = 46631
getPort (ListenPort a:flags) = a