~yoctocell/flaskell

ref: 5ceb3714ef9062645527888c6f4371d91940f98c flaskell/src/Flaskell/Cli.hs -rw-r--r-- 1.8 KiB
5ceb3714 — yoctocell gitignore: Ingore ./result-doc 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
70
71
72
73
74
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"
  <> help "Read from stdin" )
  
-- input :: Parser Input
-- input = fileInput <|> stdInput

cliArgs :: Parser Options
cliArgs = Options
          <$> switch
          (long "version"
          <> short 'v'
          <> 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

cli :: IO ()
cli = do
  parseCliArgs =<< execParser args
  where
    args = info (cliArgs <**> helper)
      ( fullDesc
        <> progDesc "Generate flashcards from FILE"
        <> header "Flaskell - CLI flashcard program" )