Skip to content

Commit

Permalink
feature(cli): default arguments
Browse files Browse the repository at this point in the history
Also add src/Utils & src/System to move some helpers there, and changed
(again) the way the reading, parsing and writing is composed
  • Loading branch information
gillchristian committed May 3, 2019
1 parent 818c554 commit 594e0d3
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
24 changes: 12 additions & 12 deletions app/Main.hs
Expand Up @@ -2,21 +2,21 @@ module Main
( main
) where

import qualified Data.Aeson as Aeson
import Parsing (parseEntries)
import qualified System.Environment as Env
import qualified System.IO as Sys
import qualified Data.Aeson as Aeson
import Parsing (parseEntries)
import System (argOr, putStderr)

putStderr :: String -> IO ()
putStderr = Sys.hPutStrLn Sys.stderr
parse :: String -> Aeson.Value
parse = Aeson.toJSON . parseEntries

doStuff :: FilePath -> FilePath -> IO ()
doStuff input output =
Aeson.encodeFile output =<< Aeson.toJSON . parseEntries <$> readFile input
parseToFile :: FilePath -> FilePath -> IO ()
parseToFile input output =
pure parse <*> readFile input >>= Aeson.encodeFile output

main :: IO ()
main = do
(input:output:_) <- Env.getArgs -- TODO handle missing files
input <- argOr 0 "statements.txt"
output <- argOr 1 "data.json"
putStderr $ "Processing statements from: \"" ++ input ++ "\""
doStuff input output
putStderr $ "Data written to: \"" ++ input ++ "\""
parseToFile input output
putStderr $ "Data written to: \"" ++ output ++ "\""
4 changes: 2 additions & 2 deletions src/Date.hs
Expand Up @@ -5,8 +5,8 @@ module Date
, readFormatedDate
) where

import qualified Data.Aeson as Aeson
import qualified Text.Regex as R
import qualified Data.Aeson as Aeson
import qualified Text.Regex as R

newtype Date =
Date (String, String, String)
Expand Down
20 changes: 20 additions & 0 deletions src/System.hs
@@ -0,0 +1,20 @@
module System
( nthArg
, argOr
, putStderr
) where

import qualified Control.Applicative as Ap
import qualified Data.Maybe as M
import qualified System.Environment as Env
import qualified System.IO as Sys
import Utils (nth)

nthArg :: Int -> IO (Maybe String)
nthArg = Ap.liftA2 nth Env.getArgs . pure

argOr :: Int -> String -> IO String
argOr n def = M.fromMaybe def <$> nthArg n

putStderr :: String -> IO ()
putStderr = Sys.hPutStrLn Sys.stderr
8 changes: 8 additions & 0 deletions src/Utils.hs
@@ -0,0 +1,8 @@
module Utils
(nth) where

nth :: [a] -> Int -> Maybe a
nth xs n
| n >= length xs = Nothing
| n < 0 = Nothing
| otherwise = Just (xs !! n)
2 changes: 2 additions & 0 deletions vigilant-engine.cabal
Expand Up @@ -25,6 +25,8 @@ library
, Matchers
, Date
, Types
, Utils
, System

build-depends: base ^>= 4.12.0.0
, regex-compat ^>= 0.95.1
Expand Down

0 comments on commit 594e0d3

Please sign in to comment.