Skip to content

Commit

Permalink
Add a README paragraph about the arrow interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
pcapriotti committed Jul 20, 2012
1 parent 4a83be1 commit e4a4d54
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Options/Applicative/Arrows.hs
Expand Up @@ -18,7 +18,7 @@
-- >
-- > opts :: Parser Options
-- > opts = runA $ proc () -> do
-- > verbose <- asA (switch idm) -< ()
-- > verbose <- asA (switch (short 'v')) -< ()
-- > args <- asA (arguments str idm) -< ()
-- > returnA -< Options args verbose
--
Expand Down
28 changes: 28 additions & 0 deletions README.md
Expand Up @@ -264,6 +264,33 @@ need to import the `Category` module and hide the `(.)` operator from the
See the haddock documentation for `Options.Applicative.Builder` for a full list
of builders and modifiers.

# Arrow interface

It is also possible to use the [Arrow syntax][arrows] to combine basic parsers.

This can be particularly useful when the structure holding parse results is
deeply nested, or when the order of fields differs from the order in which the
parsers should be applied.

Using functions from the `Options.Applicative.Arrows` module, one can write,
for example:

```haskell
data Options = Options
{ optArgs :: [String]
, optVerbose :: Bool }

opts :: Parser Options
opts = runA $ proc () -> do
verbosity <- asA (option (short 'v' & value 0)) -< ()
let verbose = verbosity > 0
args <- asA (arguments str idm) -< ()
returnA -< Options args verbose
```

where parsers can be converted to arrows using `asA`, and the resulting
composed arrow is converted back to a `Parser` with `runA`.

## How it works

A `Parser a` is essentially a heterogeneous list of `Option`s, implemented with
Expand All @@ -279,3 +306,4 @@ simplified implementation.
[status-png]: https://secure.travis-ci.org/pcapriotti/optparse-applicative.png?branch=master
[status]: http://travis-ci.org/pcapriotti/optparse-applicative?branch=master
[blog]: http://paolocapriotti.com/blog/2012/04/27/applicative-option-parser/
[arrows]: http://www.haskell.org/arrows/syntax.html

0 comments on commit e4a4d54

Please sign in to comment.