on master help sign in
Enable using custom parser for command line arguments
efa148cAlberto
src/Hakyll/Main.hs+56−28
@@ -1,10 +1,11 @@
1--------------------------------------------------------------------------------
1--------------------------------------------------------------------------------
2-- | Module providing the main hakyll function and command-line argument parsing
2-- | Module providing the main hakyll function and command-line argument parsing
3{-# LANGUAGE CPP #-}
3{-# LANGUAGE CPP #-}
4−{-# LANGUAGE DeriveDataTypeable #-}
4+
5module Hakyll.Main
5module Hakyll.Main
6 ( hakyll
6 ( hakyll
7 , hakyllWith
7 , hakyllWith
8+ , hakyllWithArgs
8 , hakyllWithExitCode
9 , hakyllWithExitCode
9 ) where
10 ) where
10
11
@@ -16,7 +17,6 @@ import System.Exit (ExitCode(ExitSuccess), exitWit
16
17
17
18
18--------------------------------------------------------------------------------
19--------------------------------------------------------------------------------
19−import Data.Monoid ((<>))
20import Options.Applicative
20import Options.Applicative
21
21
22
22
@@ -29,7 +29,7 @@ import Hakyll.Core.Rules
29
29
30
30
31--------------------------------------------------------------------------------
31--------------------------------------------------------------------------------
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
33hakyll :: Rules a -> IO ()
33hakyll :: Rules a -> IO ()
34hakyll = hakyllWith Config.defaultConfiguration
34hakyll = hakyllWith Config.defaultConfiguration
35
35
@@ -39,26 +39,54 @@ hakyll = hakyllWith Config.defaultConfiguration
39hakyllWith :: Config.Configuration -> Rules a -> IO ()
39hakyllWith :: Config.Configuration -> Rules a -> IO ()
40hakyllWith conf rules = hakyllWithExitCode conf rules >>= exitWith
40hakyllWith conf rules = hakyllWithExitCode conf rules >>= exitWith
41
41
42+--------------------------------------------------------------------------------
43+-- | A variant of 'hakyll' which returns an 'ExitCode'
42hakyllWithExitCode :: Config.Configuration -> Rules a -> IO ExitCode
44hakyllWithExitCode :: Config.Configuration -> Rules a -> IO ExitCode
43−hakyllWithExitCode conf rules = do
45+hakyllWithExitCode conf rules = do
44− args' <- customExecParser (prefs showHelpOnError) (info (helper <*> optionParser conf) (fullDesc <> progDesc (progName ++ " - Static site compiler created with Hakyll")))
46+ args <- defaultParser conf
45− let args'' = optCommand args'
47+ hakyllWithExitCodeAndArgs conf args rules
46
48
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
47− let verbosity' = if verbosity args' then Logger.Debug else Logger.Message
61+ verbosity' = if verbosity args then Logger.Debug else Logger.Message
48− check' =
62+ check =
49− if internal_links args'' then Check.InternalLinks else Check.All
63+ if internal_links args' then Check.InternalLinks else Check.All
50
64
51 logger <- Logger.new verbosity'
65 logger <- Logger.new verbosity'
66+ invokeCommands args' conf check logger rules
52
67
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 =
53− case args'' of
81+ case args of
54− Build -> Commands.build conf logger rules
82+ Build -> Commands.build conf logger rules
55− Check _ -> Commands.check conf logger check'
83+ Check _ -> Commands.check conf logger check >> ok
56− Clean -> Commands.clean conf logger >> ok
84+ Clean -> Commands.clean conf logger >> ok
57− Deploy -> Commands.deploy conf
85+ Deploy -> Commands.deploy conf
58− Preview p -> Commands.preview conf logger rules p >> ok
86+ Preview p -> Commands.preview conf logger rules p >> ok
59− Rebuild -> Commands.rebuild conf logger rules
87+ Rebuild -> Commands.rebuild conf logger rules
60− Server _ _ -> Commands.server conf logger (host args'') (port args'') >> ok
88+ 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
89+ Watch _ p s -> Commands.watch conf logger (host args) p (not s) rules >> ok
62 where
90 where
63 ok = return ExitSuccess
91 ok = return ExitSuccess
64
92
@@ -80,7 +108,7 @@ data Command
80 deriving (Show)
108 deriving (Show)
81
109
82optionParser :: Config.Configuration -> Parser Options
110optionParser :: Config.Configuration -> Parser Options
83−optionParser conf = Options <$> verboseParser <*> (commandParser conf)
111+optionParser conf = Options <$> verboseParser <*> commandParser conf
84 where
112 where
85 verboseParser = switch (long "verbose" <> short 'v' <> help "Run in verbose mode")
113 verboseParser = switch (long "verbose" <> short 'v' <> help "Run in verbose mode")
86
114
@@ -88,18 +116,18 @@ optionParser conf = Options <$> verboseParser <*> (commandParser conf)
88commandParser :: Config.Configuration -> Parser Command
116commandParser :: Config.Configuration -> Parser Command
89commandParser conf = subparser $ foldr ((<>) . produceCommand) mempty commands
117commandParser conf = subparser $ foldr ((<>) . produceCommand) mempty commands
90 where
118 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))
92 portParser = option auto (long "port" <> help "Port to listen on" <> value (Config.previewPort conf))
120 portParser = option auto (long "port" <> help "Port to listen on" <> value (Config.previewPort conf))
93 hostParser = strOption (long "host" <> help "Host to bind on" <> value (Config.previewHost conf))
121 hostParser = strOption (long "host" <> help "Host to bind on" <> value (Config.previewHost conf))
94 commands = [
122 commands = [
95− ("build",(pure Build,fullDesc <> progDesc "Generate the site")),
123+ ("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")),
124+ ("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")),
125+ ("clean", (pure Clean,fullDesc <> progDesc "Clean up and remove cache")),
98− ("deploy",(pure Deploy,fullDesc <> progDesc "Upload/deploy your site")),
126+ ("deploy", (pure Deploy,fullDesc <> progDesc "Upload/deploy your site")),
99− ("preview",(pure Preview <*> portParser,fullDesc <> progDesc "[DEPRECATED] Please use the watch command")),
127+ ("preview", (pure Preview <*> portParser,fullDesc <> progDesc "[DEPRECATED] Please use the watch command")),
100− ("rebuild",(pure Rebuild,fullDesc <> progDesc "Clean and build again")),
128+ ("rebuild", (pure Rebuild,fullDesc <> progDesc "Clean and build again")),
101− ("server",(pure Server <*> hostParser <*> portParser,fullDesc <> progDesc "Start a preview server")),
129+ ("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."))
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."))
103 ]
131 ]
104
132
105
133