on master help sign in
Enable using custom parser for command line arguments
commitefa148c095f2c556016aa5789b995d4c10fb6eb7 authorAlberto <aesadde@users.noreply.github.com> dateSun 21 May 2017, 05:35 (9 years ago) parent7ad569d
1 file changed+56−28
src/Hakyll/Main.hs+56−28
src/Hakyll/Main.hs+56−28
@@ -1,10 +1,11 @@
11--------------------------------------------------------------------------------
22-- | Module providing the main hakyll function and command-line argument parsing
33{-# LANGUAGE CPP #-}
4−{-# LANGUAGE DeriveDataTypeable #-}
4+
55module Hakyll.Main
66 ( hakyll
77 , hakyllWith
8+ , hakyllWithArgs
89 , hakyllWithExitCode
910 ) where
1011
@@ -16,7 +17,6 @@ import System.Exit (ExitCode(ExitSuccess), exitWit
1617
1718
1819--------------------------------------------------------------------------------
19−import Data.Monoid ((<>))
2020import Options.Applicative
2121
2222
@@ -29,7 +29,7 @@ import Hakyll.Core.Rules
2929
3030
3131--------------------------------------------------------------------------------
32−-- | This usualy is the function with which the user runs the hakyll compiler
32+-- | This usually is the function with which the user runs the hakyll compiler
3333hakyll :: Rules a -> IO ()
3434hakyll = hakyllWith Config.defaultConfiguration
3535
@@ -39,26 +39,54 @@ hakyll = hakyllWith Config.defaultConfiguration
3939hakyllWith :: Config.Configuration -> Rules a -> IO ()
4040hakyllWith conf rules = hakyllWithExitCode conf rules >>= exitWith
4141
42+--------------------------------------------------------------------------------
43+-- | A variant of 'hakyll' which returns an 'ExitCode'
4244hakyllWithExitCode :: Config.Configuration -> Rules a -> IO ExitCode
43−hakyllWithExitCode conf rules = do
44− args' <- customExecParser (prefs showHelpOnError) (info (helper <*> optionParser conf) (fullDesc <> progDesc (progName ++ " - Static site compiler created with Hakyll")))
45− let args'' = optCommand args'
45+hakyllWithExitCode conf rules = do
46+ args <- defaultParser conf
47+ hakyllWithExitCodeAndArgs conf args rules
4648
47− let verbosity' = if verbosity args' then Logger.Debug else Logger.Message
48− check' =
49− if internal_links args'' then Check.InternalLinks else Check.All
49+--------------------------------------------------------------------------------
50+-- | A variant of 'hakyll' which expects a 'Configuration' and command-line
51+-- 'Options'. This gives freedom to implement your own parsing.
52+hakyllWithArgs :: Config.Configuration -> Options -> Rules a -> IO ()
53+hakyllWithArgs conf args rules =
54+ hakyllWithExitCodeAndArgs conf args rules >>= exitWith
55+
56+--------------------------------------------------------------------------------
57+hakyllWithExitCodeAndArgs :: Config.Configuration ->
58+ Options -> Rules a -> IO ExitCode
59+hakyllWithExitCodeAndArgs conf args rules = do
60+ let args' = optCommand args
61+ verbosity' = if verbosity args then Logger.Debug else Logger.Message
62+ check =
63+ if internal_links args' then Check.InternalLinks else Check.All
5064
5165 logger <- Logger.new verbosity'
66+ invokeCommands args' conf check logger rules
5267
53− case args'' of
54− Build -> Commands.build conf logger rules
55− Check _ -> Commands.check conf logger check'
56− Clean -> Commands.clean conf logger >> ok
57− Deploy -> Commands.deploy conf
58− Preview p -> Commands.preview conf logger rules p >> ok
59− Rebuild -> Commands.rebuild conf logger rules
60− Server _ _ -> Commands.server conf logger (host args'') (port args'') >> ok
61− Watch _ p s -> Commands.watch conf logger (host args'') p (not s) rules >> ok
68+--------------------------------------------------------------------------------
69+defaultParser :: Config.Configuration -> IO Options
70+defaultParser conf =
71+ customExecParser (prefs showHelpOnError)
72+ (info (helper <*> optionParser conf)
73+ (fullDesc <> progDesc
74+ (progName ++ " - Static site compiler created with Hakyll")))
75+
76+
77+--------------------------------------------------------------------------------
78+invokeCommands :: Command -> Config.Configuration ->
79+ Check.Check -> Logger.Logger -> Rules a -> IO ExitCode
80+invokeCommands args conf check logger rules =
81+ case args of
82+ Build -> Commands.build conf logger rules
83+ Check _ -> Commands.check conf logger check >> ok
84+ Clean -> Commands.clean conf logger >> ok
85+ Deploy -> Commands.deploy conf
86+ Preview p -> Commands.preview conf logger rules p >> ok
87+ Rebuild -> Commands.rebuild conf logger rules
88+ Server _ _ -> Commands.server conf logger (host args) (port args) >> ok
89+ Watch _ p s -> Commands.watch conf logger (host args) p (not s) rules >> ok
6290 where
6391 ok = return ExitSuccess
6492
@@ -80,7 +108,7 @@ data Command
80108 deriving (Show)
81109
82110optionParser :: Config.Configuration -> Parser Options
83−optionParser conf = Options <$> verboseParser <*> (commandParser conf)
111+optionParser conf = Options <$> verboseParser <*> commandParser conf
84112 where
85113 verboseParser = switch (long "verbose" <> short 'v' <> help "Run in verbose mode")
86114
@@ -88,18 +116,18 @@ optionParser conf = Options <$> verboseParser <*> (commandParser conf)
88116commandParser :: Config.Configuration -> Parser Command
89117commandParser conf = subparser $ foldr ((<>) . produceCommand) mempty commands
90118 where
91− produceCommand (a,b) = command a (info (helper <*> (fst b)) (snd b))
119+ produceCommand (a,b) = command a (info (helper <*> fst b) (snd b))
92120 portParser = option auto (long "port" <> help "Port to listen on" <> value (Config.previewPort conf))
93121 hostParser = strOption (long "host" <> help "Host to bind on" <> value (Config.previewHost conf))
94122 commands = [
95− ("build",(pure Build,fullDesc <> progDesc "Generate the site")),
96− ("check",(pure Check <*> switch (long "internal-links" <> help "Check internal links only"), fullDesc <> progDesc "Validate the site output")),
97− ("clean",(pure Clean,fullDesc <> progDesc "Clean up and remove cache")),
98− ("deploy",(pure Deploy,fullDesc <> progDesc "Upload/deploy your site")),
99− ("preview",(pure Preview <*> portParser,fullDesc <> progDesc "[DEPRECATED] Please use the watch command")),
100− ("rebuild",(pure Rebuild,fullDesc <> progDesc "Clean and build again")),
101− ("server",(pure Server <*> hostParser <*> portParser,fullDesc <> progDesc "Start a preview server")),
102− ("watch",(pure Watch <*> hostParser <*> portParser <*> switch (long "no-server" <> help "Disable the built-in web server"),fullDesc <> progDesc "Autocompile on changes and start a preview server. You can watch and recompile without running a server with --no-server."))
123+ ("build", (pure Build,fullDesc <> progDesc "Generate the site")),
124+ ("check", (pure Check <*> switch (long "internal-links" <> help "Check internal links only"), fullDesc <> progDesc "Validate the site output")),
125+ ("clean", (pure Clean,fullDesc <> progDesc "Clean up and remove cache")),
126+ ("deploy", (pure Deploy,fullDesc <> progDesc "Upload/deploy your site")),
127+ ("preview", (pure Preview <*> portParser,fullDesc <> progDesc "[DEPRECATED] Please use the watch command")),
128+ ("rebuild", (pure Rebuild,fullDesc <> progDesc "Clean and build again")),
129+ ("server", (pure Server <*> hostParser <*> portParser,fullDesc <> progDesc "Start a preview server")),
130+ ("watch", (pure Watch <*> hostParser <*> portParser <*> switch (long "no-server" <> help "Disable the built-in web server"),fullDesc <> progDesc "Autocompile on changes and start a preview server. You can watch and recompile without running a server with --no-server."))
103131 ]
104132
105133