1{-
2 * Hedgewars, a free turn based strategy game
3 * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 \-}
18
19{-# LANGUAGE CPP #-}
20module Opts
21(
22    getOpts,
23) where
24
25import System.Environment
26import System.Console.GetOpt
27import Data.Maybe ( fromMaybe )
28-------------------
29import CoreTypes
30import Utils
31
32options :: [OptDescr (ServerInfo -> ServerInfo)]
33options = [
34    Option "p" ["port"] (ReqArg readListenPort "PORT") "listen on PORT",
35    Option "d" ["dedicated"] (ReqArg readDedicated "BOOL") "start as dedicated (True or False)"
36    ]
37
38readListenPort
39    , readDedicated
40    :: String -> ServerInfo -> ServerInfo
41
42
43readListenPort str opts = opts{listenPort = readPort}
44    where
45        readPort = fromInteger $ fromMaybe 46631 (maybeRead str :: Maybe Integer)
46
47readDedicated str opts = opts{isDedicated = readDed}
48    where
49        readDed = fromMaybe True (maybeRead str :: Maybe Bool)
50
51getOpts :: ServerInfo -> IO ServerInfo
52getOpts opts = do
53    args <- getArgs
54    case getOpt Permute options args of
55        (o, [], []) -> return $ foldr ($) opts{runArgs = args} o
56        (_,_,errs) -> ioError (userError (concat errs ++ usageInfo header options))
57    where header = "Usage: hedgewars-server [OPTION...]"
58