on master help sign in
cmdArgs to Options.Applicative

changed shell options parser from cmdAgrs to Options.Applicative

commit1acebf2699ecac86a2c82445eaeb11eec176be79 authorsk3r <skyer.sma@gmail.com> dateSun 24 Jul 2016, 15:47 (10 years ago) parent68e9c77
2 files changed+56−74
hakyll.cabal+4−3 src/Hakyll/Main.hs+52−71
hakyll.cabal+4−3
@@ -147,7 +147,6 @@ Library
147147 blaze-html >= 0.5 && < 0.9,
148148 blaze-markup >= 0.5.1 && < 0.8,
149149 bytestring >= 0.9 && < 0.11,
150− cmdargs >= 0.10 && < 0.11,
151150 containers >= 0.3 && < 0.6,
152151 cryptohash >= 0.7 && < 0.12,
153152 data-default >= 0.4 && < 0.8,
@@ -173,7 +172,8 @@ Library
173172 time-locale-compat >= 0.1 && < 0.2,
174173 unordered-containers >= 0.2 && < 0.3,
175174 vector >= 0.11 && < 0.12,
176− yaml >= 0.8 && < 0.9
175+ yaml >= 0.8 && < 0.9,
176+ optparse-applicative >= 0.12 && < 0.13
177177
178178 If flag(previewServer)
179179 Build-depends:
@@ -266,7 +266,8 @@ Test-suite hakyll-tests
266266 time-locale-compat >= 0.1 && < 0.2,
267267 unordered-containers >= 0.2 && < 0.3,
268268 vector >= 0.11 && < 0.12,
269− yaml >= 0.8 && < 0.9
269+ yaml >= 0.8 && < 0.9,
270+ optparse-applicative >= 0.12 && < 0.13
270271
271272 If flag(previewServer)
272273 Build-depends:
src/Hakyll/Main.hs+52−71
@@ -10,12 +10,15 @@ module Hakyll.Main
1010
1111
1212--------------------------------------------------------------------------------
13−import System.Console.CmdArgs
14−import qualified System.Console.CmdArgs.Explicit as CA
1513import System.Environment (getProgName)
1614import System.IO.Unsafe (unsafePerformIO)
1715import System.Exit (ExitCode(ExitSuccess), exitWith)
1816
17+
18+--------------------------------------------------------------------------------
19+import Options.Applicative
20+
21+
1922--------------------------------------------------------------------------------
2023import qualified Hakyll.Check as Check
2124import qualified Hakyll.Commands as Commands
@@ -37,88 +40,66 @@ hakyllWith conf rules = hakyllWithExitCode conf rules >>= exitWith
3740
3841hakyllWithExitCode :: Config.Configuration -> Rules a -> IO ExitCode
3942hakyllWithExitCode conf rules = do
40− args' <- cmdArgs (hakyllArgs conf)
43+ args' <- customExecParser (prefs showHelpOnError) (info (helper <*> optionParser conf) (fullDesc <> progDesc (progName ++ " - Static site compiler created with Hakyll")))
44+ let args'' = optCommand args'
4145
42− let verbosity' = if verbose args' then Logger.Debug else Logger.Message
46+ let verbosity' = if verbosity args' then Logger.Debug else Logger.Message
4347 check' =
44− if internal_links args' then Check.InternalLinks else Check.All
48+ if internal_links args'' then Check.InternalLinks else Check.All
4549
4650 logger <- Logger.new verbosity'
47− case args' of
48− Build _ -> Commands.build conf logger rules
49− Check _ _ -> Commands.check conf logger check' >> ok
50− Clean _ -> Commands.clean conf logger >> ok
51− Deploy _ -> Commands.deploy conf
52− Help _ -> showHelp >> ok
53− Preview _ p -> Commands.preview conf logger rules p >> ok
54− Rebuild _ -> Commands.rebuild conf logger rules
55− Server _ _ _ -> Commands.server conf logger (host args') (port args') >> ok
56− Watch _ _ p s -> Commands.watch conf logger (host args') p (not s) rules >> ok
51+
52+ case args'' of
53+ Build -> Commands.build conf logger rules
54+ Check _ -> Commands.check conf logger check' >> ok
55+ Clean -> Commands.clean conf logger >> ok
56+ Deploy -> Commands.deploy conf
57+ Preview p -> Commands.preview conf logger rules p >> ok
58+ Rebuild -> Commands.rebuild conf logger rules
59+ Server _ _ -> Commands.server conf logger (host args'') (port args'') >> ok
60+ Watch _ p s -> Commands.watch conf logger (host args'') p (not s) rules >> ok
5761 where
5862 ok = return ExitSuccess
5963
6064
6165--------------------------------------------------------------------------------
62−-- | Show usage information.
63−showHelp :: IO ()
64−showHelp = print $ CA.helpText [] CA.HelpFormatOne $ cmdArgsMode (hakyllArgs Config.defaultConfiguration)
65−
66−
67−--------------------------------------------------------------------------------
68−data HakyllArgs
69− = Build {verbose :: Bool}
70− | Check {verbose :: Bool, internal_links :: Bool}
71− | Clean {verbose :: Bool}
72− | Deploy {verbose :: Bool}
73− | Help {verbose :: Bool}
74− | Preview {verbose :: Bool, port :: Int}
75− | Rebuild {verbose :: Bool}
76− | Server {verbose :: Bool, host :: String, port :: Int}
77− | Watch {verbose :: Bool, host :: String, port :: Int, no_server :: Bool }
78− deriving (Data, Typeable, Show)
79−
8066
81−--------------------------------------------------------------------------------
82−hakyllArgs :: Config.Configuration -> HakyllArgs
83−hakyllArgs conf = modes
84− [ (Build $ verboseFlag def) &= help "Generate the site"
85− , (Check (verboseFlag def) (False &= help "Check internal links only")) &=
86− help "Validate the site output"
87− , (Clean $ verboseFlag def) &= help "Clean up and remove cache"
88− , (Deploy $ verboseFlag def) &= help "Upload/deploy your site"
89− , (Help $ verboseFlag def) &= help "Show this message" &= auto
90− , (Preview (verboseFlag def) (portFlag defaultPort)) &=
91− help "[Deprecated] Please use the watch command"
92− , (Rebuild $ verboseFlag def) &= help "Clean and build again"
93− , (Server (verboseFlag def) (hostFlag defaultHost) (portFlag defaultPort)) &=
94− help "Start a preview server"
95− , (Watch (verboseFlag def) (hostFlag defaultHost) (portFlag defaultPort) (noServerFlag False) &=
96− help "Autocompile on changes and start a preview server. You can watch and recompile without running a server with --no-server.")
97− ] &= help "Hakyll static site compiler" &= program progName
67+data Options = Options {verbosity :: Bool, optCommand :: Command}
68+ deriving (Show)
69+
70+data Command
71+ = Build
72+ | Check {internal_links :: Bool}
73+ | Clean
74+ | Deploy
75+ | Preview {port :: Int}
76+ | Rebuild
77+ | Server {host :: String, port :: Int}
78+ | Watch {host :: String, port :: Int, no_server :: Bool }
79+ deriving (Show)
80+
81+optionParser :: Config.Configuration -> Parser Options
82+optionParser conf = Options <$> verboseParser <*> (commandParser conf)
9883 where
99− defaultHost = Config.previewHost conf
100− defaultPort = Config.previewPort conf
101−
102−--------------------------------------------------------------------------------
103−verboseFlag :: Data a => a -> a
104−verboseFlag x = x &= help "Run in verbose mode"
105−{-# INLINE verboseFlag #-}
84+ verboseParser = switch (long "verbose" <> short 'v' <> help "Run in verbose mode")
10685
10786
108−--------------------------------------------------------------------------------
109−noServerFlag :: Data a => a -> a
110−noServerFlag x = x &= help "Disable the built-in web server"
111−{-# INLINE noServerFlag #-}
112−
113−--------------------------------------------------------------------------------
114−hostFlag :: Data a => a -> a
115−hostFlag x = x &= help "Host to bind on"
116−{-# INLINE hostFlag #-}
117−
118−--------------------------------------------------------------------------------
119−portFlag :: Data a => a -> a
120−portFlag x = x &= help "Port to listen on"
121−{-# INLINE portFlag #-}
87+commandParser :: Config.Configuration -> Parser Command
88+commandParser conf = subparser $ foldr ((<>) . produceCommand) mempty commands
89+ where
90+ produceCommand (a,b) = command a (info (helper <*> (fst b)) (snd b))
91+ portParser = option auto (long "port" <> help "Port to listen on" <> value (Config.previewPort conf))
92+ hostParser = strOption (long "host" <> help "Host to bind on" <> value (Config.previewHost conf))
93+ commands = [
94+ ("build",(pure Build,fullDesc <> progDesc "Generate the site")),
95+ ("check",(pure Check <*> switch (long "internal-links" <> help "Check internal links only"), fullDesc <> progDesc "Validate the site output")),
96+ ("clean",(pure Clean,fullDesc <> progDesc "Clean up and remove cache")),
97+ ("deploy",(pure Deploy,fullDesc <> progDesc "Upload/deploy your site")),
98+ ("preview",(pure Preview <*> portParser,fullDesc <> progDesc "[DEPRECATED] Please use the watch command")),
99+ ("rebuild",(pure Rebuild,fullDesc <> progDesc "Clean and build again")),
100+ ("server",(pure Server <*> hostParser <*> portParser,fullDesc <> progDesc "Start a preview server")),
101+ ("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."))
102+ ]
122103
123104
124105--------------------------------------------------------------------------------