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 +++++++++++++++++++--------- pancake.1 | 2 +- 2 files changed, 20 insertions(+), 10 deletions(-) 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 diff --git a/pancake.1 b/pancake.1 index 3467e09..6d64617 100644 --- a/pancake.1 +++ b/pancake.1 @@ -4,7 +4,7 @@ pancake - a CLI/Emacs web/gopher/file browser .SH SYNOPSIS -pancake [\fIoption ...\fR] +pancake [\fIoption ...\fR] [\fcommand\fR ...] .SH DESCRIPTION Pancake utilizes pandoc and external downloaders such as curl, adding -- cgit v1.2.3