-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhead.hs
More file actions
49 lines (44 loc) · 1.32 KB
/
head.hs
File metadata and controls
49 lines (44 loc) · 1.32 KB
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
module Main where
import Data.Foldable (forM_)
import Options.Applicative (Parser, ParserInfo, argument, auto, execParser,
fullDesc, header, help, helper, info, long, metavar,
option, progDesc, short, str, value, (<$>), (<>))
-- head
main :: IO()
main = do
-- run the parser over the cli arguments
opts <- execParser optsParserInfo
-- read the file
fileContents <- readFile $ filename opts
-- split the contents of the file into lines
let fileLines = lines fileContents
-- take the number of lines specified in the options
let headLines = take (lineCount opts) fileLines
-- print the desired lines
forM_ headLines putStrLn
-- Structure to hold cli arguments
data Options = Options
{ filename :: String
, lineCount :: Int
}
-- Parser for the cli arguments
optsParser :: Parser Options
optsParser = Options
<$> argument str
( metavar "FILENAME"
<> help "Input filename"
)
<*> option auto
( short 'n'
<> long "numlines"
<> metavar "NUM"
<> help "Number of lines to read"
<> value 10
)
-- Adding program help text to the parser
optsParserInfo :: ParserInfo Options
optsParserInfo = info (helper <*> optsParser)
( fullDesc
<> progDesc "A bad clone of head"
<> header "head - a bad clone of the real head"
)