From a44b801ab0033970660396a42462c4f7b4df56bb Mon Sep 17 00:00:00 2001 From: panne Date: Sat, 27 Mar 2004 13:18:12 +0000 Subject: [PATCH] =?utf8?q?[project=20@=202004-03-27=2013:18:12=20by=20panne]?= =?utf8?q?=20*=20Merged=20Martin=20Sj=1B-A=3Fgren's=20patch=20for=20multilin?= =?utf8?q?e=20descriptions=1B-b=20*=20Nuked=20some=20TABs=20in=20favour=20of?= =?utf8?q?=20space=20+=20some=20small=20reformatting=20*=20Updated=20copyrig?= =?utf8?q?ht?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- System/Console/GetOpt.hs | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/System/Console/GetOpt.hs b/System/Console/GetOpt.hs index 0062b02..8162997 100644 --- a/System/Console/GetOpt.hs +++ b/System/Console/GetOpt.hs @@ -1,7 +1,7 @@ ----------------------------------------------------------------------------- -- | -- Module : System.Console.GetOpt --- Copyright : (c) Sven Panne Oct. 1996 (small changes Feb. 2003) +-- Copyright : (c) Sven Panne 2002-2004 -- License : BSD-style (see the file libraries/base/LICENSE) -- -- Maintainer : libraries@haskell.org @@ -38,20 +38,19 @@ over 1100 lines, we need only 195 here, including a 46 line example! -} module System.Console.GetOpt ( - -- * GetOpt - getOpt, - usageInfo, - ArgOrder(..), - OptDescr(..), - ArgDescr(..), + -- * GetOpt + getOpt, + usageInfo, + ArgOrder(..), + OptDescr(..), + ArgDescr(..), - -- * Example - - -- $example - ) where + -- * Example -import Prelude -import Data.List ( isPrefixOf ) + -- $example +) where + +import Data.List ( isPrefixOf ) -- |What to do with options following non-options data ArgOrder a @@ -98,19 +97,22 @@ usageInfo :: String -- header -> [OptDescr a] -- option descriptors -> String -- nicely formatted decription of options usageInfo header optDescr = unlines (header:table) - where (ss,ls,ds) = (unzip3 . map fmtOpt) optDescr + where (ss,ls,ds) = (unzip3 . concatMap fmtOpt) optDescr table = zipWith3 paste (sameLen ss) (sameLen ls) ds paste x y z = " " ++ x ++ " " ++ y ++ " " ++ z sameLen xs = flushLeft ((maximum . map length) xs) xs flushLeft n xs = [ take n (x ++ repeat ' ') | x <- xs ] -fmtOpt :: OptDescr a -> (String,String,String) -fmtOpt (Option sos los ad descr) = (sepBy ',' (map (fmtShort ad) sos), - sepBy ',' (map (fmtLong ad) los), - descr) +fmtOpt :: OptDescr a -> [(String,String,String)] +fmtOpt (Option sos los ad descr) = + case lines descr of + [] -> [(sosFmt,losFmt,"")] + (d:ds) -> (sosFmt,losFmt,d) : [ ("","",d') | d' <- ds ] where sepBy _ [] = "" sepBy _ [x] = x sepBy ch (x:xs) = x ++ ch:' ':sepBy ch xs + sosFmt = sepBy ',' (map (fmtShort ad) sos) + losFmt = sepBy ',' (map (fmtLong ad) los) fmtShort :: ArgDescr a -> Char -> String fmtShort (NoArg _ ) so = "-" ++ [so] @@ -274,7 +276,7 @@ compiler: > data Flag > = Verbose | Version > | Input String | Output String | LibDir String -> deriving Show +> deriving Show > > options :: [OptDescr Flag] > options = @@ -291,9 +293,9 @@ compiler: > > compilerOpts :: [String] -> IO ([Flag], [String]) > compilerOpts argv = -> case (getOpt Permute options argv) of -> (o,n,[] ) -> return (o,n) -> (_,_,errs) -> ioError (userError (concat errs ++ usageInfo header options)) +> case (getOpt Permute options argv) of +> (o,n,[] ) -> return (o,n) +> (_,_,errs) -> ioError (userError (concat errs ++ usageInfo header options)) > where header = "Usage: ic [OPTION...] files..." -} -- 1.7.10.4