summaryrefslogtreecommitdiff
path: root/HsbotIrcBot/Main.hs
diff options
context:
space:
mode:
authorJulien Dessaux2010-08-01 00:22:58 +0200
committerJulien Dessaux2010-08-01 00:22:58 +0200
commit6888950b606430f3779507bf8106df168a52d0f2 (patch)
treef99b81ef1ede28027a3aba588d10aca80d6129e9 /HsbotIrcBot/Main.hs
parentGot rid of the pseudo dynamic starting stuff, wrote a decent CLI parameters h... (diff)
downloadhsbot-6888950b606430f3779507bf8106df168a52d0f2.tar.gz
hsbot-6888950b606430f3779507bf8106df168a52d0f2.tar.bz2
hsbot-6888950b606430f3779507bf8106df168a52d0f2.zip
Added the cli parser and a config file handler for hsbot-irc.
Diffstat (limited to '')
-rw-r--r--HsbotIrcBot/Main.hs83
1 files changed, 83 insertions, 0 deletions
diff --git a/HsbotIrcBot/Main.hs b/HsbotIrcBot/Main.hs
new file mode 100644
index 0000000..070b7b6
--- /dev/null
+++ b/HsbotIrcBot/Main.hs
@@ -0,0 +1,83 @@
+module Main (main) where
+
+import Control.Monad (when)
+import Prelude hiding (catch)
+import System.Console.GetOpt
+import System.Environment
+import System.Exit
+import System.IO
+
+import Hsbot.Irc.Config
+
+-- | Main function
+main :: IO ()
+main = do
+ args <- getArgs
+ -- Parse options, getting a list of option actions
+ let (actions, nonOptions, errors) = getOpt RequireOrder options args
+ -- Here we thread startOptions through all supplied option actions
+ opts <- case (nonOptions, errors) of
+ ([], []) -> foldl (>>=) (return defaultOptions) actions
+ (_, _) -> do
+ hPutStrLn stderr $ concat errors ++ usageInfo header options
+ exitWith $ ExitFailure 1
+ -- From there the initialization code truly begins
+ when (optDebug opts) . putStrLn $ "Got options : " ++ (show opts)
+ -- We find and parse the config file
+ ircConfig <- getIrcConfig $ optConfigFile opts
+ print $ ircConfigChannels ircConfig
+ print $ ircConfigPlugins ircConfig
+
+-- CLI argument parting stuff {{{
+-- | CLI options
+data Options = Options
+ { optDebug :: Bool
+ , optConfigFile :: Maybe String
+ , optGroup :: Maybe String
+ , optUser :: Maybe String
+ , optVerbose :: Bool
+ } deriving (Show)
+
+-- | CLI default options
+defaultOptions :: Options
+defaultOptions = Options { optDebug = False
+ , optConfigFile = Nothing
+ , optGroup = Nothing
+ , optUser = Nothing
+ , optVerbose = False }
+
+-- | CLI options logic
+options :: [ OptDescr (Options -> IO Options) ]
+options =
+ [ Option "d" ["debug"]
+ (NoArg (\opt -> return opt { optDebug = True, optVerbose = True }))
+ "Enter verbose debug mode and prevents Hsbot from forking in background"
+ , Option "f" ["file"]
+ (ReqArg (\arg opt -> return opt { optConfigFile = return arg }) "<config_file>")
+ "The config file to use"
+ , Option "g" ["group"]
+ (ReqArg (\arg opt -> return opt { optGroup = return arg }) "<group>")
+ "The group hsbot will run as"
+ , Option "h" ["help"]
+ (NoArg (\_ -> do
+ putStrLn $ usageInfo header options
+ exitWith ExitSuccess))
+ "Print this help message"
+ , Option "u" ["user"]
+ (ReqArg (\arg opt -> return opt { optUser = return arg }) "<user>")
+ "The user hsbot will run as"
+ , Option "v" ["verbose"]
+ (NoArg (\opt -> return opt { optVerbose = True }))
+ "Enable verbose messages"
+ , Option "V" ["version"]
+ (NoArg (\_ -> do
+ putStrLn "hsbot-irc version 0.3"
+ exitWith ExitSuccess))
+ "Show version"
+ ]
+
+-- | Usage header
+header :: String
+header = "Usage: hsbot-irc [-dhvV] [-f config_file] [-u user] [-g group]"
+-- }}}
+