netserver/Opts.hs
author unc0rr
Sun, 09 Nov 2008 09:47:10 +0000
changeset 1487 b4cc59a6d50a
parent 1473 60e1fad78d58
child 1492 2da1fe033f23
permissions -rw-r--r--
Add an option to name records with current date and time
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
(
60e1fad78d58 Cleanup code a bit, some reformatting
unc0rr
parents: 1386
diff changeset
     3
	GlobalOptions(..),
60e1fad78d58 Cleanup code a bit, some reformatting
unc0rr
parents: 1386
diff changeset
     4
	globalOptions
60e1fad78d58 Cleanup code a bit, some reformatting
unc0rr
parents: 1386
diff changeset
     5
) where
1341
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
     6
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
     7
import System
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
     8
import System.Console.GetOpt
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
     9
import Network
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    10
import Data.Maybe ( fromMaybe )
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    11
import Miscutils
1383
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    12
import System.IO.Unsafe
1341
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    13
1383
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    14
data GlobalOptions =
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    15
	GlobalOptions
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    16
	{
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    17
		isDedicated :: Bool,
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    18
		serverMessage :: String,
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    19
		listenPort :: PortNumber
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    20
	}
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    21
defaultMessage = "<h2><p align=center><a href=\"http://www.hedgewars.org/\">http://www.hedgewars.org/</a></p></h2>"
1386
674429128152 Oops... start a dedicated server by default
unc0rr
parents: 1383
diff changeset
    22
defaultOptions = (GlobalOptions True defaultMessage 46631)
1383
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    23
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    24
options :: [OptDescr (GlobalOptions -> GlobalOptions)]
1341
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    25
options = [
1383
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    26
	Option ['p'] ["port"] (ReqArg readListenPort "PORT") "listen on PORT",
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    27
	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
    28
	]
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    29
1383
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    30
readListenPort, readDedicated :: String -> GlobalOptions -> GlobalOptions
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    31
readListenPort str opts = opts{listenPort = readPort}
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    32
	where
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    33
		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
    34
1383
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    35
readDedicated str opts = opts{isDedicated = readDedicated}
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    36
	where
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    37
		readDedicated = fromMaybe True (maybeRead str :: Maybe Bool)
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    38
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    39
opts :: IO GlobalOptions
1341
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    40
opts = do
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    41
	args <- getArgs
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    42
	case getOpt Permute options args of
1383
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    43
		(o, [], []) -> return $ foldr ($) defaultOptions o
1341
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    44
		(_,_,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
    45
	where header = "Usage: newhwserv [OPTION...]"
86d7d5ab22a2 Allow --port=PORT command-line parameter to specify the port to listen on
unc0rr
parents:
diff changeset
    46
1383
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    47
{-# NOINLINE globalOptions #-}
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1342
diff changeset
    48
globalOptions = unsafePerformIO opts