module Potrero.Opts ( Config(..) , Mode(..) , getConfig , usage , version ) where import qualified System.Console.GetOpt as Opt data Mode = ShowHelp | ShowVersion | REPL deriving (Eq, Show) data Config = Config { configFiles :: [FilePath] , configShowRolls :: Bool , configMode :: Mode } deriving (Eq, Show) defaultConfig :: Config defaultConfig = Config { configFiles = [] , configShowRolls = False , configMode = REPL } opts :: [Opt.OptDescr (Config -> Config)] opts = [ Opt.Option ['s'] ["show-rolls"] (Opt.NoArg (\ conf -> conf { configShowRolls = True })) "show the exact roll results" , Opt.Option ['h'] ["help"] (Opt.NoArg (\ conf -> conf { configMode = ShowHelp })) "show this help text" , Opt.Option ['v'] ["version"] (Opt.NoArg (\ conf -> conf { configMode = ShowVersion })) "show the version" ] usage :: String usage = Opt.usageInfo "potrero" opts version :: String version = "potrero, version 0.1" getConfig :: [String] -> Either String Config getConfig args = let (fs, files, errors) = Opt.getOpt Opt.Permute opts args conf = foldr ($) defaultConfig fs in case errors of [] -> pure conf { configFiles = files } _ -> Left (unlines errors)