----------------------------------------------------------------------------- -- -- Module : QifOptions -- Copyright : (c) Ingolf Wagner -- License : BSD3 -- -- Maintainer : Ingolf Wagner -- Stability : unstable -- Portability : -- -- | -- ----------------------------------------------------------------------------- module QifOptions where import System.Console.GetOpt import System.Exit import System.IO import System.Environment import Control.Monad import Text.Read import Data.List.Split import Data.Maybe data Options = Options { optVerbose :: Bool , optInput :: String , optOutput :: String , optDate :: Maybe Int , optBalance :: Maybe Int , optText :: [Int] , optLongText :: [Int] , optSkip :: Int , optSeparator :: Char , optUpdater :: Maybe String } deriving (Show) startOptions :: Options startOptions = Options { optVerbose = False , optInput = "" , optOutput = "" , optDate = Nothing , optBalance = Nothing , optText = [] , optLongText = [] , optSkip = 0 , optSeparator = ',' , optUpdater = Nothing } --instance Show (a -> b) where -- show _ = "" instance Show (IO a) where show _ = "" options :: [ OptDescr (Options -> IO Options) ] options = [ Option "i" ["input"] (ReqArg (\arg opt -> return opt { optInput = arg }) "FILE") "Input file (CSV Format)" , Option "o" ["output"] (ReqArg (\arg opt -> return opt { optOutput = arg }) "FILE") "Output file (Qif Format)" , Option "u" ["updater"] (ReqArg (\arg opt -> return opt { optUpdater = Just arg }) "FILE") "Updater file format is `match`<->`replacement`" , Option "z" ["separator"] (ReqArg (\arg opt -> return opt { optSeparator = head arg }) "char") "separator of the csv file" , Option "d" ["date"] (ReqArg (\arg opt -> return opt { optDate = readColumn arg}) "") "Date Column" , Option "b" ["balance"] (ReqArg (\arg opt -> return opt { optBalance = readColumn arg}) "") "Balance Column" , Option "t" ["text"] (ReqArg (\arg opt -> return opt { optText = readColumns arg}) "") "Text Columns" , Option "l" ["longtext"] (ReqArg (\arg opt -> return opt { optLongText = readColumns arg}) "") "Long Text Columns" , Option "s" ["skip"] (ReqArg (\arg opt -> case (readColumn arg) of Nothing -> return opt Just r -> return opt { optSkip = r} ) "") "Rows to Skip before reading" , Option "v" ["verbose"] (NoArg (\opt -> return opt { optVerbose = True })) "Enable verbose messages" , Option "V" ["version"] (NoArg (\_ -> do hPutStrLn stderr "Version 0.2" exitWith ExitSuccess)) "Print version" , Option "h" ["help"] (NoArg (\_ -> do prg <- getProgName hPutStrLn stderr (usageInfo (useage prg) options) exitWith ExitSuccess)) "Show help" ] useage :: String -> String useage prog = prog ++ "\n\n" ++ "author : Ingolf Wagner\n\n" ++ header ++ "\n\n" ++ information ++ "\n\n" where header = "Converting CSV files to Qif Files" information = "all column numbers start at 0!" readColumn :: String -> Maybe Int readColumn input = readMaybe input readColumns :: String -> [Int] readColumns input = catMaybes $ map readColumn $ splitOn "," input