-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
50 lines (46 loc) · 1.47 KB
/
Main.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
50
module Main where
import Data.Maybe
import Control.Monad
import qualified Data.ByteString as B
import qualified Data.Text as T
import Data.Text.Encoding
import Options.Applicative
data WcOptions = WcOptions {
l :: Bool,
c :: Bool,
m :: Bool,
w :: Bool,
filename :: Maybe String
}
wc :: WcOptions -> IO ()
wc opts = do
contents <- fromMaybe getContents $ readFile <$> filename opts
when (none || l opts) $ print $ length $ lines contents
when (none || w opts) $ print $ length $ words contents
when (none || m opts) $ print $ length contents
when (none || c opts) $ print $ B.length $ encodeUtf8 $ T.pack contents
where
none = not $ l opts || c opts || m opts || w opts
main :: IO ()
main = execParser (info parser idm) >>= wc
where
parser = WcOptions <$>
switch (
short 'l' <>
long "lines" <>
help "Write number of lines to stdout") <*>
switch (
short 'c' <>
long "bytes" <>
help "Write number of bytes to stdout") <*>
switch (
short 'm' <>
long "chars" <>
help "Write number of characters to stdout") <*>
switch (
short 'w' <>
long "words" <>
help "write number of words to the stdout") <*>
optional (
strArgument $ metavar "filename") <**>
helper