Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Control.Monad(ap, replicateM, filterM, liftM)
- import Data.IORef
- import Control.Applicative
- import Options.Applicative
- data Command = Command
- { l :: Bool,
- c :: Bool,
- m :: Bool,
- w :: Bool,
- f :: Input}
- data Input
- = FileInput FilePath
- | StdInput
- readCommand :: Parser Command
- readCommand = Command
- <$> switch
- ( long "lines"
- <> short 'l'
- <> help "--вывести количество строк" )
- <*> switch
- ( long "bytes"
- <> short 'c'
- <> help "--вывести количество байт" )
- <*> switch
- ( long "chars"
- <> short 'm'
- <> help "--вывести количество символов" )
- <*> switch
- ( long "words"
- <> short 'w'
- <> help "--вывести количество слов" )
- <*> ( (FileInput <$> argument str ( metavar "FILE" <> help "Input file" ))
- <|> (StdInput <$ pure ()))
- runCommand :: Command -> IO ()
- runCommand (cm@(Command l c m w (FileInput f))) = do
- str <- readFile f
- writeAns cm str
- runCommand (cm@(Command l c m w StdInput)) = do
- str <- getLine
- writeAns cm str
- writeIf :: Bool -> Int -> IO ()
- writeIf p x = if p then print x else return ()
- writeAns :: Command -> String -> Int
- writeAns (Command l c m w _) str = do
- l' <- wcl str
- writeIf l l'
- c' <- wcc str
- writeIf c c'
- w' <- wcw str
- writeIf w w'
- wcc :: String -> Int
- wcc s = length s
- wcl :: String -> Int
- wcl s = length . lines s
- wcw :: String -> Int
- wcw s = length . words s
- main :: IO ()
- main = runCommand =<< execParser opts
- where
- opts = info (readCommand <**> helper)
- ( fullDesc
- <> header "[OPTIONS] <filename>` где `[OPTIONS]` могут быть:" )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement