Allow options to consume an arbitrary number of arguments

This commit is contained in:
Paolo Capriotti 2014-10-15 17:32:26 +01:00
parent 37fe3df3bf
commit 1f3a68b41e
2 changed files with 14 additions and 5 deletions

View File

@ -64,7 +64,7 @@ resetArgs = modify $ \s -> s
, skippedArgs = [] }
data BaseOption a
= BaseReg [OptName] (ReadM a)
= BaseReg [OptName] (ArgParser a)
| BaseFlag [OptName] a
| BaseCommand String a
@ -87,7 +87,7 @@ instance Pretty1 BaseOption where
instance Opt BaseOption where
optFind arg (BaseReg ns v)
| matchNames arg ns = Just (argParser1 v)
| matchNames arg ns = Just v
optFind arg (BaseFlag ns x)
| matchNames arg ns = Just (pure x)
optFind arg (BaseCommand cmd x)

View File

@ -95,6 +95,7 @@ module Options.Applicative.Builder (
) where
import Control.Applicative
import Control.Monad.Except
import Data.Foldable (asum)
import Data.Monoid (Monoid (..)
#if __GLASGOW_HASKELL__ > 702
@ -262,8 +263,13 @@ switch = flag False True
-- 'infoOption' instead.
abortOption :: HasOption (WithInfo OptProperties BaseOption) f
=> ParseError -> Mod OptionFields a -> f a
abortOption err m = option (readerAbort err) . (`mappend` m) $ mconcat
[ noArgError err , metavar "" ]
abortOption err m = liftOption
. WithInfo (mkProps d g)
. BaseReg (optNames fields)
$ throwError err
where
Mod f d g = metavar "" `mappend` m
fields = f (OptionFields [] mempty err)
-- | An option that always fails and displays a message.
infoOption :: HasOption (WithInfo OptProperties BaseOption) f
@ -278,7 +284,10 @@ strOption = option str
-- | Builder for an option using the 'auto' reader.
option :: HasOption (WithInfo OptProperties BaseOption) f
=> ReadM a -> Mod OptionFields a -> f a
option r m = liftOption . WithInfo (mkProps d g) . BaseReg (optNames fields) $ r
option r m = liftOption
. WithInfo (mkProps d g)
. BaseReg (optNames fields)
. argParser1 $ r
where
Mod f d g = metavar "ARG" `mappend` m
fields = f (OptionFields [] mempty (ErrorMsg ""))