packages feed

nptools-0.2.2: bufferize.hs

import System.Environment (getArgs)
import Control.Arrow
import Control.Monad
import qualified Data.ByteString as B

usage :: String -> a
usage msg =
  error . unlines $
    [""
    ,"Usage: bufferize [-o <outfile>]* <infile>* -- <infile>*"
    ,""
    ,msg]

argv :: [String] -> ([String],[String])
argv ("-o":out:xs)  = first (out:) (argv xs)
argv ("-o":[])      = usage "filename expected after -o"
argv ("--":xs)      = ([], xs)
argv (('-':x):_)    = usage $ "Unpected option -" ++ x
argv (x:xs)         = second (x:) (argv xs)
argv []             = ([],[])

main :: IO ()
main = do args  <- getArgs
          let (outputs,inputs) = argv args
              write s = do
                when (null outputs) $ B.putStr s
                forM_ outputs       $ flip B.writeFile s
          when (null inputs)  $ write =<< B.getContents
          forM_ inputs        $ write <=< B.readFile