From 6e368bc36c603a23e57a5baa14556446a8603a8e Mon Sep 17 00:00:00 2001 From: defanor Date: Mon, 18 Dec 2017 08:21:09 +0300 Subject: Read first command from command-line options So that e.g. `pancake ddg something` can be invoked for a quick search. Also print option parsing error messages. --- Pancake.hs | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'Pancake.hs') diff --git a/Pancake.hs b/Pancake.hs index e854d13..d99a411 100644 --- a/Pancake.hs +++ b/Pancake.hs @@ -348,7 +348,8 @@ data Option = OVersion | OHelp | OEmbedded deriving (Show) -- | Command-line option descriptions for 'getOpt'. options :: [OptDescr Option] -options = [ Option [] ["version"] (NoArg OVersion) "show version number and exit" +options = [ Option [] ["version"] (NoArg OVersion) + "show version number and exit" , Option [] ["help"] (NoArg OHelp) "show help message and exit" , Option ['e'] ["embedded"] (NoArg OEmbedded) "run in the embedded mode" ] @@ -360,13 +361,22 @@ main = do -- A hack to receive SIGINT reliably. tid <- myThreadId _ <- installHandler sigINT (Catch (throwTo tid UserInterrupt)) Nothing - let run e = runStateT (updateConfig >> eventLoop) - (LS ([],[]) 0 [] def e False [] Nothing) - >> pure () - case getOpt Permute options args of - ([], [], []) -> run False - ([OEmbedded], [], []) -> run True - ([OVersion], [], []) -> putStrLn $ "pancake " ++ showVersion version + let run cmd e = do + let maybeCommand = + if null cmd + then pure () + else get >>= \st -> command (parseCommand (conf st) (unwords cmd)) + _ <- runStateT (updateConfig >> maybeCommand >> eventLoop) + (LS ([],[]) 0 [] def e False [] Nothing) + pure () + let opt = getOpt Permute options args + case opt of + (_, _, errors) -> mapM_ putErrLn errors + case opt of + ([], cmd, _) -> run cmd False + ([OEmbedded], cmd, _) -> run cmd True + ([OVersion], [], _) -> putStrLn $ "pancake " ++ showVersion version _ -> do p <- getProgName - putStrLn $ usageInfo ("Usage: " ++ p ++ " [option ...]") options + putStrLn $ + usageInfo ("Usage: " ++ p ++ " [option ...] [command ...]") options -- cgit v1.2.3