From 248929641aadd65b29e32648d5082c05d4b4df9d Mon Sep 17 00:00:00 2001 From: sof Date: Wed, 20 Jan 1999 10:43:46 +0000 Subject: [PATCH] [project @ 1999-01-20 10:43:46 by sof] Back again - somehow got lost. --- ghc/docs/libraries/GetOpt.sgml | 96 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 ghc/docs/libraries/GetOpt.sgml diff --git a/ghc/docs/libraries/GetOpt.sgml b/ghc/docs/libraries/GetOpt.sgml new file mode 100644 index 0000000..2aed0d8 --- /dev/null +++ b/ghc/docs/libraries/GetOpt.sgml @@ -0,0 +1,96 @@ + +

+ +The +module GetOpt where + +-- representing a single option: +data OptDescr a + = Option [Char] -- list of short option characters + [String] -- list of long option strings (without "--") + (ArgDescr a) -- argument descriptor + String -- explanation of option for user + +-- argument option: +data ArgDescr a + = NoArg a -- no argument expected + | ReqArg (String -> a) String -- option requires argument + | OptArg (Maybe String -> a) String -- optional argument + +usageInfo :: String -- header + -> [OptDescr a] -- options recognised + -> String -- nicely formatted decription of options + +getOpt :: ArgOrder a -- non-option handling + -> [OptDescr a] -- options recognised + -> [String] -- the command-line + -> ( [a] -- options + , [String] -- non-options + ,[String] -- error messages + ) + +data ArgOrder a + = RequireOrder + | Permute + | ReturnInOrder (String -> a) + + + + + The command-line options recognised is described by a list of + +From a list of option values, +To decode a command-line with respect to a list of options, + + +To hopefully illuminate the role of the different +module Opts where + +import GetOpt +import Maybe ( fromMaybe ) + +data Flag + = Verbose | Version + | Input String | Output String | LibDir String + deriving Show + +options :: [OptDescr Flag] +options = + [ Option ['v'] ["verbose"] (NoArg Verbose) "chatty output on stderr" + , Option ['V','?'] ["version"] (NoArg Version) "show version number" + , Option ['o'] ["output"] (OptArg outp "FILE") "output FILE" + , Option ['c'] [] (OptArg inp "FILE") "input FILE" + , Option ['L'] ["libdir"] (ReqArg LibDir "DIR") "library directory" + ] + +inp,outp :: Maybe String -> Flag +outp = Output . fromMaybe "stdout" +inp = Input . fromMaybe "stdout" + +compilerOpts :: [String] -> IO ([Flag], [String]) +compilerOpts argv = + case (getOpt Permute options argv) of + (o,n,[] ) -> return (o,n) + (_,_,errs) -> fail (userError (concat errs ++ usageInfo header options)) + where header = "Usage: ic [OPTION...] files..." + + + -- 1.7.10.4