~yoctocell/flaskell

ref: e3a468e0c3a7c42104f349c4e64a2ed70becfba4 flaskell/src/Flaskell/Cli.hs -rw-r--r-- 1.5 KiB
e3a468e0 — yoctocell Cli.hs: Parse file on -f 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module Flaskell.Cli
  -- ( getContent
  ( cli
  )
where

import           Flaskell.Parser
import           Flaskell.Ui

import Options.Applicative
import System.Exit
import           System.Environment (getArgs, getProgName)


data Input = FileInput FilePath | StdInput

data Options = Options
  { version :: Bool
  , input :: Input }
  
versionNumber :: String
versionNumber = "0.1.0.0"

fileInput :: Parser Input
fileInput = FileInput <$> strOption
  (  long "file"
  <> short 'f'
  <> metavar "FILE"
  <> help "Input file" )

stdInput :: Parser Input
stdInput = flag' StdInput
  (  long "stdin"
  <> short 'i'
  <> help "Read from stdin" )
  
cliArgs :: Parser Options
cliArgs = Options
          <$> switch
          (long "version"
          <> short 'v'
          <> help "Print the version number")
          <*> (fileInput <|> stdInput)

parseCliArgs :: Options -> IO ()
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
  where
    args = info (cliArgs <**> helper)
      ( fullDesc
        <> progDesc "Generate flashcards from FILE"
        <> header "Flaskell - CLI flashcard program" )