From 9d22da4c8b6f5ed253a8ad004b9ac9a2a93f5f1a Mon Sep 17 00:00:00 2001 From: Tissevert Date: Tue, 5 Feb 2019 10:48:57 +0100 Subject: [PATCH] Add program version in help and a separate --version flag --- src/Arguments.hs | 51 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/src/Arguments.hs b/src/Arguments.hs index 28b7e28..1e7523a 100644 --- a/src/Arguments.hs +++ b/src/Arguments.hs @@ -5,12 +5,14 @@ module Arguments ( ) where import Data.Monoid ((<>)) +import Data.Version (showVersion) import Options.Applicative +import qualified Paths_hablo as Hablo (version) import System.Directory (doesDirectoryExist, doesFileExist, makeAbsolute) -import System.Exit (die) +import System.Exit (die, exitSuccess) import System.FilePath.Posix (dropTrailingPathSeparator, isValid) -data Arguments = Arguments { +data Arguments = BlogConfig { sourceDir :: FilePath , blogName :: Maybe String , previewArticlesCount :: Int @@ -18,9 +20,10 @@ data Arguments = Arguments { , bannerPath :: Maybe FilePath , headPath :: Maybe FilePath } + | Version -parseArguments :: Parser Arguments -parseArguments = Arguments +blogConfig :: Parser Arguments +blogConfig = BlogConfig <$> argument filePath (metavar "INPUT_DIR") <*> option (optional str) ( metavar "BLOG_NAME" @@ -58,6 +61,16 @@ parseArguments = Arguments <> help "path to the file to add in the blog's head" ) +version :: Parser Arguments +version = flag' Version ( + long "version" + <> short 'v' + <> help "print the version number" + ) + +arguments :: Parser Arguments +arguments = blogConfig <|> version + filePath :: ReadM FilePath filePath = eitherReader $ \path -> if isValid path @@ -71,16 +84,16 @@ ifNotDie check messageBuilder input = do then return () else die $ messageBuilder input -get :: IO Arguments -get = do - arguments <- execParser $ info (parseArguments <**> helper) fullDesc - doesDirectoryExist `ifNotDie` noSuchDirectory $ sourceDir arguments - absoluteSourceDir <- makeAbsolute $ sourceDir arguments - mapM_ (doesFileExist `ifNotDie` noSuchFile) $ bannerPath arguments - absoluteBannerPath <- mapM makeAbsolute $ bannerPath arguments - mapM_ (doesFileExist `ifNotDie` noSuchFile) $ headPath arguments - absoluteHeadPath <- mapM makeAbsolute $ headPath arguments - return $ arguments { +checkAndMakeAbsolute :: Arguments -> IO Arguments +checkAndMakeAbsolute Version = return Version +checkAndMakeAbsolute aBlogConfig = do + doesDirectoryExist `ifNotDie` noSuchDirectory $ sourceDir aBlogConfig + absoluteSourceDir <- makeAbsolute $ sourceDir aBlogConfig + mapM_ (doesFileExist `ifNotDie` noSuchFile) $ bannerPath aBlogConfig + absoluteBannerPath <- mapM makeAbsolute $ bannerPath aBlogConfig + mapM_ (doesFileExist `ifNotDie` noSuchFile) $ headPath aBlogConfig + absoluteHeadPath <- mapM makeAbsolute $ headPath aBlogConfig + return $ aBlogConfig { sourceDir = absoluteSourceDir , bannerPath = absoluteBannerPath , headPath = absoluteHeadPath @@ -88,3 +101,13 @@ get = do where noSuchDirectory = (++ ": no such directory") noSuchFile = (++ ": no such file") + +get :: IO Arguments +get = do + args <- execParser $ + info + (arguments <**> helper) + (fullDesc <> header ("Hablo v" ++ showVersion Hablo.version)) + case args of + Version -> (putStrLn $ showVersion Hablo.version) >> exitSuccess + BlogConfig {} -> checkAndMakeAbsolute args