~yoctocell/flaskell

e3a468e0c3a7c42104f349c4e64a2ed70becfba4 — yoctocell 1 year, 10 months ago 5ceb371
Cli.hs: Parse file on -f
1 files changed, 16 insertions(+), 21 deletions(-)

M src/Flaskell/Cli.hs
M src/Flaskell/Cli.hs => src/Flaskell/Cli.hs +16 -21
@@ 31,11 31,9 @@ fileInput = FileInput <$> strOption
stdInput :: Parser Input
stdInput = flag' StdInput
  (  long "stdin"
  <> short 'i'
  <> help "Read from stdin" )
  
-- input :: Parser Input
-- input = fileInput <|> stdInput

cliArgs :: Parser Options
cliArgs = Options
          <$> switch


@@ 44,26 42,23 @@ cliArgs = Options
          <> help "Print the version number")
          <*> (fileInput <|> stdInput)

-- cliArgs :: ParserInfo Options
-- cliArgs = info (args <**> helper)
--   ( fullDesc
--   <> progDesc "Generate flashcards from FILE"
--   <> header "Flaskell - CLI flashcard program" )
--   where args = Options
--           <$> switch
--               ( long "version"
--              <> short 'v'
--              <> help "Print the version number" )

parseCliArgs :: Options -> IO ()
parseCliArgs (Options True _) = putStrLn versionNumber
parseCliArgs (Options False (FileInput file)) = putStrLn file
parseCliArgs (Options False (StdInput)) = putStrLn "stdin"

-- -- TODO cli arguments
-- getContent :: IO [String]
-- getContent = getArgs >>= fmap lines . readFile . head
parseCliArgs options = case options of
  (Options True _) -> putStrLn versionNumber
  (Options False (FileInput file)) -> do
    contents <- readFile file
    feedCards . getCards $ contents
  (Options False (StdInput)) -> putStrLn "stdin"
    
-- Take contents of a file and return a list of Flashcard
getCards :: String -> [Flashcard]
getCards contents =
  let hints = getHint <$> (lines contents)
      answers = getAnswer <$> (lines contents)
  in
    zip hints answers

  
cli :: IO ()
cli = do
  parseCliArgs =<< execParser args