X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=System%2FConsole%2FGetOpt.hs;h=0062b02c36d14f462fb8f3be9acc0769afd9f0ad;hb=9da2d4c92821be09b27afa5b8bc1368b305a1496;hp=0b564e53b471524e3ce6dab54bba7c3c1140b88e;hpb=0a331d25b66a8b0f72d430df452ec6ae2fe778c7;p=ghc-base.git diff --git a/System/Console/GetOpt.hs b/System/Console/GetOpt.hs index 0b564e5..0062b02 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 Dec. 1997) +-- Copyright : (c) Sven Panne Oct. 1996 (small changes Feb. 2003) -- License : BSD-style (see the file libraries/base/LICENSE) -- -- Maintainer : libraries@haskell.org @@ -24,13 +24,17 @@ it...) and the recognition of long options with a single dash (e.g. '-help' is recognised as '--help', as long as there is no short option 'h'). -Other differences between GNU's getopt and this implementation: * To -enforce a coherent description of options and arguments, there are -explanation fields in the option/argument descriptor. * Error -messages are now more informative, but no longer POSIX -compliant... :-( And a final Haskell advertisement: The GNU C -implementation uses well over 1100 lines, we need only 195 here, -including a 46 line example! :-) +Other differences between GNU's getopt and this implementation: + +* To enforce a coherent description of options and arguments, there + are explanation fields in the option/argument descriptor. + +* Error messages are now more informative, but no longer POSIX + compliant... :-( + +And a final Haskell advertisement: The GNU C implementation uses well +over 1100 lines, we need only 195 here, including a 46 line example! +:-) -} module System.Console.GetOpt ( @@ -62,7 +66,7 @@ The arguments to 'Option' are: * list of short option characters -* list of long option strings (without "--") +* list of long option strings (without \"--\") * argument descriptor @@ -127,7 +131,7 @@ Process the command-line, and return the list of values that matched * The option descriptions (see 'OptDescr') * The actual command line arguments (presumably got from - 'System.Console.Environment.getArgs'). + 'System.Environment.getArgs'). 'getOpt' returns a triple, consisting of the argument values, a list of options that didn\'t match, and a list of error messages. @@ -161,7 +165,9 @@ getNext a rest _ = (NonOpt a,rest) longOpt :: String -> [String] -> [OptDescr a] -> (OptKind a,[String]) longOpt ls rs optDescr = long ads arg rs where (opt,arg) = break (=='=') ls - options = [ o | o@(Option _ ls _ _) <- optDescr, l <- ls, opt `isPrefixOf` l ] + getWith p = [ o | o@(Option _ ls _ _) <- optDescr, l <- ls, opt `p` l ] + exact = getWith (==) + options = if null exact then getWith isPrefixOf else exact ads = [ ad | Option _ _ ad _ <- options ] optStr = ("--"++opt) @@ -256,14 +262,14 @@ test order cmdline = case getOpt order options cmdline of {- $example -To hopefully illuminate the role of the different "GetOpt" data +To hopefully illuminate the role of the different data structures, here\'s the command-line options for a (very simple) compiler: > module Opts where > -> import GetOpt -> import Maybe ( fromMaybe ) +> import System.Console.GetOpt +> import Data.Maybe ( fromMaybe ) > > data Flag > = Verbose | Version @@ -287,7 +293,7 @@ compiler: > compilerOpts argv = > case (getOpt Permute options argv) of > (o,n,[] ) -> return (o,n) -> (_,_,errs) -> failIO (concat errs ++ usageInfo header options) +> (_,_,errs) -> ioError (userError (concat errs ++ usageInfo header options)) > where header = "Usage: ic [OPTION...] files..." -}