Add program version in help and a separate --version flag
This commit is contained in:
parent
844cc5a638
commit
9d22da4c8b
1 changed files with 37 additions and 14 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue