hablo/src/Arguments.hs

52 lines
1.4 KiB
Haskell

{-# LANGUAGE NamedFieldPuns #-}
module Arguments (
Arguments(..)
, configuration
) where
import Data.Monoid ((<>))
import Options.Applicative
import System.Directory (doesDirectoryExist, makeAbsolute)
import System.Exit (die)
import System.FilePath.Posix (dropTrailingPathSeparator, isValid)
data Arguments = Arguments {
sourceDir :: FilePath
, blogName :: Maybe String
, previewCountArg :: Int
}
arguments :: Parser Arguments
arguments = Arguments
<$> argument directory (metavar "INPUT_DIR")
<*> option (optional str) (
metavar "BLOG_NAME"
<> value Nothing
<> short 'n'
<> long "name"
<> help "name of the blog"
)
<*> option auto (
metavar "PREVIEW_COUNT"
<> value 3
<> short 'p'
<> long "preview-count"
<> help "number of articles listed on the page of each category"
)
directory :: ReadM FilePath
directory = eitherReader $ \path ->
if isValid path
then Right $ dropTrailingPathSeparator path
else Left "This string doesn't represent a valid path"
configuration :: IO Arguments
configuration = do
inputArguments <- execParser $ info (arguments <**> helper) fullDesc
directoryExists <- doesDirectoryExist $ sourceDir inputArguments
if directoryExists
then do
absolutePath <- makeAbsolute $ sourceDir inputArguments
return $ inputArguments { sourceDir = absolutePath }
else die "INPUT_DIR doesn't exist"