packages feed

optparse-applicative 0.5.0 → 0.5.1

raw patch · 8 files changed

+95/−10 lines, 8 files

Files

Options/Applicative/Builder.hs view
@@ -70,6 +70,8 @@   PrefsMod,   multiSuffix,   disambiguate,+  showHelpOnError,+  noBacktrack,   prefs   ) where @@ -284,13 +286,20 @@ disambiguate :: PrefsMod disambiguate = PrefsMod $ \p -> p { prefDisambiguate = True } +showHelpOnError :: PrefsMod+showHelpOnError = PrefsMod $ \p -> p { prefShowHelpOnError = True }++noBacktrack :: PrefsMod+noBacktrack = PrefsMod $ \p -> p { prefBacktrack = False }+ prefs :: PrefsMod -> ParserPrefs prefs m = applyPrefsMod m base   where     base = ParserPrefs       { prefMultiSuffix = ""       , prefDisambiguate = False-      , prefShowHelpOnError = False }+      , prefShowHelpOnError = False+      , prefBacktrack = True }  -- convenience shortcuts 
Options/Applicative/Builder/Internal.hs view
@@ -72,6 +72,31 @@   mappend (DefaultProp d1 s1) (DefaultProp d2 s2) =     DefaultProp (d1 `mplus` d2) (s1 `mplus` s2) +-- | An option modifier.+--+-- Option modifiers are values that represent a modification of the properties+-- of an option.+--+-- The type parameter @a@ is the return type of the option, while @f@ is a+-- record containing its properties (e.g. 'OptionFields' for regular options,+-- 'FlagFields' for flags, etc...).+--+-- An option modifier consists of 3 elements:+--+--  - A field modifier, of the form @f a -> f a@. These are essentially+--  (compositions of) setters for some of the properties supported by @f@.+--+--  - An optional default value and function to display it.+--+--  - A property modifier, of the form @OptProperties -> OptProperties@. This+--  is just like the field modifier, but for properties applicable to any+--  option.+--+-- Modifiers are instances of 'Monoid', and can be composed as such.+--+-- You rarely need to deal with modifiers directly, as most of the times it is+-- sufficient to pass them to builders (such as 'strOption' or 'flag') to+-- create options (see 'Options.Applicative.Builder'). data Mod f a = Mod (f a -> f a)                    (DefaultProp a)                    (OptProperties -> OptProperties)
Options/Applicative/Common.hs view
@@ -99,10 +99,12 @@   CmdReader _ f ->     flip fmap (f arg) $ \subp args -> do       setContext (Just arg) subp-      x <- tryP $ runParser (infoParser subp) args-      case x of-        Left e -> errorP e-        Right r -> return r+      prefs <- getPrefs+      let runSubparser+            | prefBacktrack prefs = runParser+            | otherwise = \p a+            -> (,) <$> runParserFully p a <*> pure []+      runSubparser (infoParser subp) args   where     parsed =       case arg of
Options/Applicative/Help.hs view
@@ -75,7 +75,10 @@      fold_tree (Leaf x) = x     fold_tree (MultNode xs) = unwords (fold_trees xs)-    fold_tree (AltNode xs) = "(" ++ intercalate " | " (fold_trees xs) ++ ")"+    fold_tree (AltNode xs) = alt_node (fold_trees xs)++    alt_node [n] = n+    alt_node ns = "(" ++ intercalate " | " ns ++ ")"      fold_trees = filter (not . null) . map fold_tree 
Options/Applicative/Types.hs view
@@ -59,8 +59,9 @@ -- | Global preferences for a top-level 'Parser'. data ParserPrefs = ParserPrefs   { prefMultiSuffix :: String    -- ^ metavar suffix for multiple options-  , prefDisambiguate :: Bool     -- ^ automatically disambiguate abbreviations-  , prefShowHelpOnError :: Bool  -- ^ always show help text on parse errors+  , prefDisambiguate :: Bool     -- ^ automatically disambiguate abbreviations (default: False)+  , prefShowHelpOnError :: Bool  -- ^ always show help text on parse errors (default: False)+  , prefBacktrack :: Bool        -- ^ backtrack to parent parser when a subcommand fails (default: True)   }  data OptName = OptShort !Char
README.md view
@@ -69,6 +69,17 @@  containing a detailed list of options with descriptions. +Parsers are instances of both `Applicative` and `Alternative`, and work with+any generic combinator, like `many` and `some`. For example, to make a option+return `Nothing` instead of failing when it's not supplied, you can use the+`optional` combinator in `Control.Applicative`:++```haskell+optional $ strOption+  ( long "output"+  & metavar "DIRECTORY" )+```+  [applicative]: http://www.soi.city.ac.uk/~ross/papers/Applicative.html  ## Supported options@@ -185,6 +196,13 @@  <> help "Retain all intermediate temporary files" ) ``` +There is also a `flag'` builder, which has no default value. For example, to+add a `--version` switch to a program, you could write:++```haskell+flag' Nothing (long "version" <> hidden) <|> (Just <$> normal_options)+```+ ### Arguments  An **argument** parser specifies a positional command line argument.@@ -307,6 +325,10 @@  See `tests/Examples/Cabal.hs` for a slightly more elaborate example using the arrow syntax for defining parsers.++Note that the `Arrow` interface is provided only for convenience. The API based+on `Applicative` is just as expressive, although it might be cumbersome to use+in certain cases.  ## How it works 
optparse-applicative.cabal view
@@ -1,5 +1,5 @@ name:                optparse-applicative-version:             0.5.0+version:             0.5.1 synopsis:            Utilities and combinators for parsing command line options description:     Here is a simple example of an applicative option parser:
tests/Tests.hs view
@@ -126,7 +126,7 @@  case_nested_commands :: Assertion case_nested_commands = do-  let p3 = strOption (short 'a'<> metavar "A")+  let p3 = strOption (short 'a' <> metavar "A")       p2 = subparser (command "b" (info p3 idm))       p1 = subparser (command "c" (info p2 idm))       i = info (p1 <**> helper) idm@@ -218,6 +218,29 @@   case result of     Left _ -> assertFailure "unexpected parse error"     Right r -> ["foo", "bar", "baz"] @=? r++case_issue_35 :: Assertion+case_issue_35 = do+  let p =  flag' True (short 't' <> hidden)+       <|> flag' False (short 'f')+      i = info p idm+      result = run i []+  case result of+    Left (ParserFailure err _) -> do+      text <- head . lines <$> err "test"+      "Usage: test -f" @=? text+    Right val ->+      assertFailure $ "unexpected result " ++ show val++case_backtracking :: Assertion+case_backtracking = do+  let p2 = switch (short 'a')+      p1 = (,)+        <$> subparser (command "c" (info p2 idm))+        <*> switch (short 'b')+      i = info (p1 <**> helper) idm+      result = execParserPure (prefs noBacktrack) i ["c", "-b"]+  assertLeft result $ \ _ -> return ()  main :: IO () main = $(defaultMainGenerator)