diff options
author | defanor <defanor@uberspace.net> | 2017-12-18 08:21:09 +0300 |
---|---|---|
committer | defanor <defanor@uberspace.net> | 2017-12-18 08:23:43 +0300 |
commit | 6e368bc36c603a23e57a5baa14556446a8603a8e (patch) | |
tree | 2e742e400df1e84cb7197014045ab35194c3cd37 /Pancake.hs | |
parent | be0fce32fc99b26a3ae4e208fb2a244f441f3212 (diff) |
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.
Diffstat (limited to 'Pancake.hs')
-rw-r--r-- | Pancake.hs | 28 |
1 files changed, 19 insertions, 9 deletions
@@ -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 |