From 9a1184006cbd1e199c7ea8922ae740ce4bdfe38f Mon Sep 17 00:00:00 2001 From: Tissevert Date: Fri, 4 Aug 2023 17:05:45 +0200 Subject: [PATCH] Clean the handling of --version argument, reusing the existing tools from optparse-applicative --- src/Arguments.hs | 37 ++++++++++++++----------------------- src/Main.hs | 13 ++++--------- src/TODO | 1 + 3 files changed, 19 insertions(+), 32 deletions(-) diff --git a/src/Arguments.hs b/src/Arguments.hs index cb8b12c..db2d72c 100644 --- a/src/Arguments.hs +++ b/src/Arguments.hs @@ -9,17 +9,17 @@ module Arguments ( import Data.Monoid ((<>)) #endif import Control.Applicative ((<|>), (<**>), optional) -import Options.Applicative ( - Parser, ReadM, argument, auto, eitherReader, execParser, flag', fullDesc - , header, help, helper, info, long, metavar, short, str, switch, value - ) +import Options.Applicative + ( Parser, ReadM, argument, auto, eitherReader, execParser, flag', fullDesc + , header, help, helper, info, infoOption, long, metavar, short, str, switch + , value ) import qualified Options.Applicative as Optparse (option) import System.FilePath (dropTrailingPathSeparator, isValid) version :: String version = "0.3.0.0" -data Arguments = BlogConfig { +data Arguments = Arguments { sourceDir :: FilePath , articlesPath :: Maybe FilePath , bannerPath :: Maybe FilePath @@ -37,7 +37,6 @@ data Arguments = BlogConfig { , siteURL :: Maybe String , wording :: Maybe FilePath } - | Version option :: ReadM a -> Char -> String -> String -> String -> Parser (Maybe a) option readM aShort aLong aMetavar aHelpMessage = @@ -49,8 +48,8 @@ option readM aShort aLong aMetavar aHelpMessage = <> help aHelpMessage ) -blogConfig :: Parser Arguments -blogConfig = BlogConfig +arguments :: Parser Arguments +arguments = Arguments <$> argument filePath (value "." <> metavar "INPUT_DIR") <*> option filePath 'a' "articles" "DIRECTORY" "relative path to the directory containing the articles within INPUT_DIR" @@ -83,16 +82,6 @@ blogConfig = BlogConfig <*> option filePath 'u' "site-url" "URL" "URL where the blog is published" <*> option filePath 'w' "wording" "FILE" "path to the file containing the wording to use" -versionP :: Parser Arguments -versionP = flag' Version ( - long "version" - <> short 'v' - <> help "print the version number" - ) - -arguments :: Parser Arguments -arguments = blogConfig <|> versionP - filePath :: ReadM FilePath filePath = eitherReader $ \path -> if isValid path @@ -100,8 +89,10 @@ filePath = eitherReader $ \path -> else Left "This string doesn't represent a valid path" get :: IO Arguments -get = do - execParser $ - info - (arguments <**> helper) - (fullDesc <> header ("Hablo v" ++ version)) +get = execParser + (info (arguments <**> showVersion <**> helper) + (fullDesc <> header ("Hablo v" ++ version))) + where + showVersion = infoOption + version + (long "version" <> short 'v' <> help "print current version") diff --git a/src/Main.hs b/src/Main.hs index 7cf576f..d0b5a41 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -10,12 +10,7 @@ import qualified RSS (generate) import System.Exit (exitSuccess) main :: IO () -main = do - arguments <- Arguments.get - case arguments of - Version -> (putStrLn version) >> exitSuccess - config@(BlogConfig {}) -> Blog.build config >>= runReaderT (do - HTML.generate - JS.generate - RSS.generate - ) +main = Arguments.get >>= Blog.build >>= runReaderT + (do HTML.generate + JS.generate + RSS.generate) diff --git a/src/TODO b/src/TODO index cdb045f..c36b1ad 100644 --- a/src/TODO +++ b/src/TODO @@ -1,4 +1,5 @@ - dans Arguments.hs, vérifier que l'implémentation de option est pas déjà dans la lib optparse-applicative + => ah non en fait c'est une fonction locale au fichier juste mal nommée, corriger ça - le Wording, c'était quand même pas l'idée du siècle -> ça pourrait pas tout revenir dans un thème qui serait du HTML ? - faque Template aussi - «Pretty» Oo ? mais c'est juste des Utils en fait