11{-# LANGUAGE NamedFieldPuns #-}
2- -----------------------------------------------------------------------------
32{-# LANGUAGE TupleSections #-}
43
4+ -----------------------------------------------------------------------------
5+
56-- |
67-- Module : Distribution.GetOpt
78-- Copyright : (c) Sven Panne 2002-2005
@@ -67,12 +68,12 @@ data ArgDescr a
6768 | -- | option requires argument
6869 ReqArg (String -> Either String a ) String
6970 | -- | optional argument
70- OptArg (Maybe String -> Either String a ) String
71+ OptArg String (Maybe String -> Either String a ) String
7172
7273instance Functor ArgDescr where
7374 fmap f (NoArg a) = NoArg (f a)
7475 fmap f (ReqArg g s) = ReqArg (fmap f . g) s
75- fmap f (OptArg g s) = OptArg (fmap f . g) s
76+ fmap f (OptArg dv g s) = OptArg dv (fmap f . g) s
7677
7778data OptKind a -- kind of cmd line arg (internal use only):
7879 = Opt a -- an option
@@ -130,17 +131,41 @@ zipDefault _ bd (a : as) [] = (a, bd) : map (,bd) as
130131zipDefault ad _ [] (b : bs) = (ad, b) : map (ad,) bs
131132zipDefault ad bd (a : as) (b : bs) = (a, b) : zipDefault ad bd as bs
132133
134+ -- | Pretty printing of short options.
135+ -- * With required arguments can be given as:
136+ -- @-w PATH or -wPATH (but not -w=PATH)@
137+ -- This is dislayed as:
138+ -- @-w PATH or -wPATH@
139+ -- * With optional but default arguments can be given as:
140+ -- @-j or -jNUM (but not -j=NUM or -j NUM)@
141+ -- This is dislayed as:
142+ -- @-j[NUM]@
133143fmtShort :: ArgDescr a -> Char -> String
134144fmtShort (NoArg _) so = " -" ++ [so]
135- fmtShort (ReqArg _ _) so = " -" ++ [so]
136- fmtShort (OptArg _ _) so = " -" ++ [so]
137-
138- -- unlike upstream GetOpt we omit the arg name for short options
139-
145+ fmtShort (ReqArg _ ad) so =
146+ let opt = " -" ++ [so]
147+ in opt ++ " " ++ ad ++ " or " ++ opt ++ ad
148+ fmtShort (OptArg _ _ ad) so =
149+ let opt = " -" ++ [so]
150+ in opt ++ " [" ++ ad ++ " ]"
151+
152+ -- | Pretty printing of long options.
153+ -- * With required arguments can be given as:
154+ -- @--with-compiler=PATH (but not --with-compiler PATH)@
155+ -- This is dislayed as:
156+ -- @--with-compiler=PATH@
157+ -- * With optional but default arguments can be given as:
158+ -- @--jobs or --jobs=NUM (but not --jobs NUM)@
159+ -- This is dislayed as:
160+ -- @--jobs[=NUM]@
140161fmtLong :: ArgDescr a -> String -> String
141162fmtLong (NoArg _) lo = " --" ++ lo
142- fmtLong (ReqArg _ ad) lo = " --" ++ lo ++ " =" ++ ad
143- fmtLong (OptArg _ ad) lo = " --" ++ lo ++ " [=" ++ ad ++ " ]"
163+ fmtLong (ReqArg _ ad) lo =
164+ let opt = " --" ++ lo
165+ in opt ++ " =" ++ ad
166+ fmtLong (OptArg _ _ ad) lo =
167+ let opt = " --" ++ lo
168+ in opt ++ " [=" ++ ad ++ " ]"
144169
145170wrapText :: Int -> String -> [String ]
146171wrapText width = map unwords . wrap 0 [] . words
@@ -230,8 +255,8 @@ longOpt ls rs optDescr = long ads arg rs
230255 long [ReqArg _ d] [] [] = (errReq d optStr, [] )
231256 long [ReqArg f _] [] (r : rest) = (fromRes (f r), rest)
232257 long [ReqArg f _] (' =' : xs) rest = (fromRes (f xs), rest)
233- long [OptArg f _] [] rest = (fromRes (f Nothing ), rest)
234- long [OptArg f _] (' =' : xs) rest = (fromRes (f (Just xs)), rest)
258+ long [OptArg _ f _] [] rest = (fromRes (f Nothing ), rest)
259+ long [OptArg _ f _] (' =' : xs) rest = (fromRes (f (Just xs)), rest)
235260 long _ _ rest = (UnreqOpt (" --" ++ ls), rest)
236261
237262-- handle short option
@@ -249,8 +274,8 @@ shortOpt y ys rs optDescr = short ads ys rs
249274 short (ReqArg _ d : _) [] [] = (errReq d optStr, [] )
250275 short (ReqArg f _ : _) [] (r : rest) = (fromRes (f r), rest)
251276 short (ReqArg f _ : _) xs rest = (fromRes (f xs), rest)
252- short (OptArg f _ : _) [] rest = (fromRes (f Nothing ), rest)
253- short (OptArg f _ : _) xs rest = (fromRes (f (Just xs)), rest)
277+ short (OptArg _ f _ : _) [] rest = (fromRes (f Nothing ), rest)
278+ short (OptArg _ f _ : _) xs rest = (fromRes (f (Just xs)), rest)
254279 short [] [] rest = (UnreqOpt optStr, rest)
255280 short [] xs rest = (UnreqOpt (optStr ++ xs), rest)
256281
0 commit comments