diff options
author | Hendrik Jaeger <henk@frustcomp> | 2013-12-09 01:19:16 +0100 |
---|---|---|
committer | Hendrik Jaeger <henk@frustcomp> | 2013-12-09 01:19:16 +0100 |
commit | 7bc2168552de76fa7c985b65608199296b6ff55f (patch) | |
tree | 13eac8228ea92d62833f2104e1be854973438189 | |
parent | d38b5df5b924ba9d04e1b5babf28df41883465cd (diff) |
On branch optparse-applicative
Changes to be committed:
modified: diddohs.hs
Added: option parser basics
-rw-r--r-- | diddohs.hs | 26 |
1 files changed, 19 insertions, 7 deletions
@@ -3,7 +3,9 @@ import Data.DateTime( parseDateTime, startOfTime, diffSeconds, formatDateTime ) import Data.List( zip4, zipWith4, transpose ) import Data.List.Split( splitOn ) import Data.Maybe( fromJust, fromMaybe ) +import Data.Monoid( mempty ) import Data.Time.Clock( secondsToDiffTime ) +import Options.Applicative( execParser, info, strOption, long ) import System.Environment( getArgs ) import Text.Printf( printf ) @@ -24,17 +26,27 @@ data DiddoEntry = DiddoEntry { start :: String instance Show DiddoEntry where show (DiddoEntry start finish delta entry) = printf "%s;%s;%s;%s" start finish (show delta) entry -main :: IO () -main = do - logfile_name : timestring_format : _ <- getArgs - logfile_lines_split <- map (splitOn ";") . lines <$> readFile logfile_name +data DiddoOpts = DiddoOpts + { inDateFmt :: String + , inFile :: String + } - let [timestrings_finish +mainWithOpts :: DiddoOpts -> IO () +mainWithOpts opts = do + [timestrings_finish , entries - ] = transpose logfile_lines_split - utcTimes_finish = map (fromMaybe (error "Input data broken.") . parseDateTime timestring_format) timestrings_finish + ] <- transpose . map (splitOn ";") . lines <$> readFile (inFile opts) + let utcTimes_finish = map (fromMaybe (error "Input data broken.") . parseDateTime (inDateFmt opts)) timestrings_finish entry_deltas_HMMSS = zipWith (\x y -> secondsToHMS $ diffSeconds x y) utcTimes_finish (startOfTime : init utcTimes_finish) diddos_summarized = zipWith4 DiddoEntry ("" : init timestrings_finish) timestrings_finish entry_deltas_HMMSS entries mapM_ print diddos_summarized +main :: IO () +main = execParser opts >>= mainWithOpts + where + opts = info parser mempty + parser = DiddoOpts + <$> strOption ( long "indateform" ) + <*> strOption ( long "infile" ) + |