Add program version in help and a separate --version flag

This commit is contained in:
Tissevert 2019-02-05 10:48:57 +01:00
parent 844cc5a638
commit 9d22da4c8b

View file

@ -5,12 +5,14 @@ module Arguments (
) where ) where
import Data.Monoid ((<>)) import Data.Monoid ((<>))
import Data.Version (showVersion)
import Options.Applicative import Options.Applicative
import qualified Paths_hablo as Hablo (version)
import System.Directory (doesDirectoryExist, doesFileExist, makeAbsolute) import System.Directory (doesDirectoryExist, doesFileExist, makeAbsolute)
import System.Exit (die) import System.Exit (die, exitSuccess)
import System.FilePath.Posix (dropTrailingPathSeparator, isValid) import System.FilePath.Posix (dropTrailingPathSeparator, isValid)
data Arguments = Arguments { data Arguments = BlogConfig {
sourceDir :: FilePath sourceDir :: FilePath
, blogName :: Maybe String , blogName :: Maybe String
, previewArticlesCount :: Int , previewArticlesCount :: Int
@ -18,9 +20,10 @@ data Arguments = Arguments {
, bannerPath :: Maybe FilePath , bannerPath :: Maybe FilePath
, headPath :: Maybe FilePath , headPath :: Maybe FilePath
} }
| Version
parseArguments :: Parser Arguments blogConfig :: Parser Arguments
parseArguments = Arguments blogConfig = BlogConfig
<$> argument filePath (metavar "INPUT_DIR") <$> argument filePath (metavar "INPUT_DIR")
<*> option (optional str) ( <*> option (optional str) (
metavar "BLOG_NAME" metavar "BLOG_NAME"
@ -58,6 +61,16 @@ parseArguments = Arguments
<> help "path to the file to add in the blog's head" <> 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 :: ReadM FilePath
filePath = eitherReader $ \path -> filePath = eitherReader $ \path ->
if isValid path if isValid path
@ -71,16 +84,16 @@ ifNotDie check messageBuilder input = do
then return () then return ()
else die $ messageBuilder input else die $ messageBuilder input
get :: IO Arguments checkAndMakeAbsolute :: Arguments -> IO Arguments
get = do checkAndMakeAbsolute Version = return Version
arguments <- execParser $ info (parseArguments <**> helper) fullDesc checkAndMakeAbsolute aBlogConfig = do
doesDirectoryExist `ifNotDie` noSuchDirectory $ sourceDir arguments doesDirectoryExist `ifNotDie` noSuchDirectory $ sourceDir aBlogConfig
absoluteSourceDir <- makeAbsolute $ sourceDir arguments absoluteSourceDir <- makeAbsolute $ sourceDir aBlogConfig
mapM_ (doesFileExist `ifNotDie` noSuchFile) $ bannerPath arguments mapM_ (doesFileExist `ifNotDie` noSuchFile) $ bannerPath aBlogConfig
absoluteBannerPath <- mapM makeAbsolute $ bannerPath arguments absoluteBannerPath <- mapM makeAbsolute $ bannerPath aBlogConfig
mapM_ (doesFileExist `ifNotDie` noSuchFile) $ headPath arguments mapM_ (doesFileExist `ifNotDie` noSuchFile) $ headPath aBlogConfig
absoluteHeadPath <- mapM makeAbsolute $ headPath arguments absoluteHeadPath <- mapM makeAbsolute $ headPath aBlogConfig
return $ arguments { return $ aBlogConfig {
sourceDir = absoluteSourceDir sourceDir = absoluteSourceDir
, bannerPath = absoluteBannerPath , bannerPath = absoluteBannerPath
, headPath = absoluteHeadPath , headPath = absoluteHeadPath
@ -88,3 +101,13 @@ get = do
where where
noSuchDirectory = (++ ": no such directory") noSuchDirectory = (++ ": no such directory")
noSuchFile = (++ ": no such file") 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