summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordefanor <defanor@uberspace.net>2017-12-18 08:21:09 +0300
committerdefanor <defanor@uberspace.net>2017-12-18 08:23:43 +0300
commit6e368bc36c603a23e57a5baa14556446a8603a8e (patch)
tree2e742e400df1e84cb7197014045ab35194c3cd37
parentbe0fce32fc99b26a3ae4e208fb2a244f441f3212 (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.
-rw-r--r--Pancake.hs28
-rw-r--r--pancake.12
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