netserver/Opts.hs
changeset 1341 86d7d5ab22a2
child 1342 ae6c4f10ace2
equal deleted inserted replaced
1340:430d210d54ae 1341:86d7d5ab22a2
       
     1 module Opts where
       
     2 
       
     3 import System
       
     4 import System.Console.GetOpt
       
     5 import Network
       
     6 import Data.Maybe ( fromMaybe )
       
     7 import Miscutils
       
     8 
       
     9 data Flag = ListenPort PortNumber
       
    10 	deriving Show
       
    11 
       
    12 options :: [OptDescr Flag]
       
    13 options = [
       
    14 	Option ['p'] ["port"] (OptArg defaultPort "PORT") "listen on PORT"
       
    15 	]
       
    16 
       
    17 defaultPort :: Maybe String -> Flag
       
    18 defaultPort str = ListenPort $ fromInteger (fromMaybe 46631 (maybeRead (fromMaybe "46631" str) :: Maybe Integer))
       
    19 
       
    20 opts :: IO [Flag]
       
    21 opts = do
       
    22 	args <- getArgs
       
    23 	case getOpt Permute options args of
       
    24 		(o, [], []) -> return o
       
    25 		(_,_,errs) -> ioError (userError (concat errs ++ usageInfo header options))
       
    26 	where header = "Usage: newhwserv [OPTION...]"
       
    27 
       
    28 getPort :: [Flag] -> PortNumber
       
    29 getPort [] = 46631
       
    30 getPort (ListenPort a:flags) = a