-
Notifications
You must be signed in to change notification settings - Fork 2
/
showFile.hs
49 lines (41 loc) · 1.17 KB
/
showFile.hs
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
#!/usr/local/bin/runHaskell
-- runHaskell opt.hs --filename FILENAME
-- Usage: opt.hs --filename FILENAME [--linen LINES] [-q|--quiet]
-- Print first n lines of a file
import Options.Applicative
import Data.Semigroup ((<>))
import System.IO
takeCont :: Int -> String -> String
takeCont n = unlines . (take n) . lines
data Sample = Sample { filename :: FilePath, linen :: Int, quiet :: Bool}
sample :: Parser Sample
sample = Sample
<$> strOption
(long "filename"
<> short 'f'
<> metavar "FILENAME"
<> help "the name of a file")
<*> option auto
(long "linen"
<> short 'n'
<> help "the number of lines"
<> showDefault
<> value 1
<> metavar "LINES" )
<*> switch
(long "quiet"
<> short 'q'
<> help "Whether to be quiet" )
main :: IO ()
main = greet =<< execParser opts
where
opts = info (sample <**> helper)
( fullDesc
<> progDesc "Print first n lines of a file"
<> header "filename - my own optparse-applicative" )
greet :: Sample -> IO ()
greet (Sample name n False) = do {
s <- readFile name;
putStrLn (takeCont n s)
}
greet _ = return ()