module Options where import System.FilePath.Posix import Data.Maybe import Data.Semigroup ((<>)) import Data.List.Predicate import Options.Applicative data Options = Options { optIn :: String , optOut :: String , optTmp :: String , optStore :: String } validateOptions :: Options -> Maybe [String] validateOptions opts = case res of [] -> Nothing es -> Just es where res = catMaybes $ map (\f -> f opts) optionTests optionTests :: [(Options -> Maybe String)] optionTests = reverse [ (validPath optIn "input") , (validPath optOut "output") , (validPath optTmp "temp") , (validPath optStore "store") , uniqPaths ] validPath :: (Options -> String) -> String -> Options -> Maybe String validPath f n o = if isValid (f o) then Nothing else Just $ "invalid " ++ n ++ " path: " ++ (f o) uniqPaths :: Options -> Maybe String uniqPaths o = if allUnique $ pathList o then Nothing else Just $ "directories must be unique (i think)" where pathList o= [ (optIn o) , (optOut o) , (optTmp o) , (optStore o) ] options :: Parser Options options = Options <$> strOption ( long "input" <> short 'i' <> metavar "DIR" <> help "source input directory" ) <*> strOption ( long "output" <> short 'o' <> metavar "DIR" <> help "build output directory" ) <*> strOption ( long "temp" <> short 't' <> metavar "DIR" <> showDefault <> value "./tmp" <> help "temp directory (possibly unused)" ) <*> strOption ( long "store" <> short 's' <> metavar "DIR" <> showDefault <> value "./store" <> help "store directory (possibly unused)" ) getOptions = info (options <**> helper) ( fullDesc <> progDesc "build a static site from a set of input files" <> header "rf-hakyll - a static site generator" )