1 changed files with 42 additions and 0 deletions
@ -0,0 +1,42 @@ |
|||||||
|
{-# LANGUAGE NoImplicitPrelude #-} |
||||||
|
|
||||||
|
import Prelude (Bool (False, True), IO, String, ($), (++), |
||||||
|
concat, flip, foldl, id, ioError, |
||||||
|
otherwise, putStr, putStrLn, read, unwords, |
||||||
|
userError) |
||||||
|
import System.Console.GetOpt (ArgDescr (NoArg), ArgOrder (RequireOrder), |
||||||
|
OptDescr (Option), getOpt, usageInfo) |
||||||
|
import System.Environment (getArgs) |
||||||
|
|
||||||
|
data Options = Options { newline :: Bool, escape :: Bool } |
||||||
|
defaultOptions = Options { newline = True, escape = False } |
||||||
|
|
||||||
|
unescape :: String -> String |
||||||
|
unescape str = read ("\"" ++ str ++ "\"") :: String |
||||||
|
|
||||||
|
echo :: Options -> [String] -> IO () |
||||||
|
echo (Options newline escape) txt = puts text |
||||||
|
where puts | newline = putStrLn |
||||||
|
| otherwise = putStr |
||||||
|
|
||||||
|
text | escape = unescape $ unwords txt |
||||||
|
| otherwise = unwords txt |
||||||
|
|
||||||
|
options :: [OptDescr (Options -> Options)] |
||||||
|
options = [ Option ['n'] [] (NoArg (\opts -> opts { newline = False })) "do not print terminating newline" |
||||||
|
, Option ['e'] [] (NoArg (\opts -> opts { escape = True })) "print escape sequences" |
||||||
|
] |
||||||
|
|
||||||
|
main :: IO () |
||||||
|
main = do |
||||||
|
argv <- getArgs |
||||||
|
case getOpt RequireOrder options argv of |
||||||
|
(o, n, [] ) -> echo (set o) n |
||||||
|
(_, _, errs) -> ioError (userError (concat errs ++ usageInfo usage options)) |
||||||
|
where |
||||||
|
usage :: String |
||||||
|
usage = "Usage: echo [-e] [-n] string [...]" |
||||||
|
|
||||||
|
set :: [Options -> Options] -> Options |
||||||
|
set = foldl (flip id) defaultOptions |
||||||
|
|
Loading…
Reference in new issue