optparse-applicative-cmdline-util: Utility functions for working with optparse-applicative

[ agpl, cli, library, options, parsing, system ] [ Propose Tags ]

See README.md here

[Skip to Readme]


[Index] [Quick Jump]


Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


  • No Candidates
Versions [RSS] 0.2.0
Change log CHANGELOG.md
Dependencies attoparsec (>= && <0.15), base (>=4.7 && <5), optparse-applicative (>= && <0.18), text (>= && <1.3) [details]
License AGPL-3.0-only
Copyright 2020–2022 Tony Zorman
Author Tony Zorman
Maintainer soliditsallgood@mailbox.org
Revised Revision 1 made by TonyZorman at 2022-11-17T14:58:47Z
Category System, CLI, Options, Parsing
Home page https://github.com/slotThe/optparse-applicative-cmdline-util
Bug tracker https://github.com/slotThe/optparse-applicative-cmdline-util/issues
Source repo head: git clone https://github.com/slotThe/optparse-applicative-cmdline-util
Uploaded by TonyZorman at 2022-08-12T18:38:07Z
Distributions NixOS:0.2.0
Downloads 43 total (3 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2022-08-12 [all 1 reports]

Readme for optparse-applicative-cmdline-util-0.2.0

[back to package description]


Utility functions for working with optparse-applicative.

Much of the module revolves around easily building options that can take "multiple arguments" in the form of separated inputs (e.g. program --option one,two,three,four). This still honours the POSIX standard for options only taking a single argument (by not using spaces to separate the different inputs), while also being very convenient to enter (as opposed to, say, wrapping everything inside quotes).

Another focus involves connecting the attoparsec library with optparse-applicative (this is often useful when options involve more complex parsing patterns).


Consider the following pseudo-EBNF:

  start        ∷= prefix , ":" , options ;
  prefix       ∷= "a" | "b" | "c" ;
  options      ∷= { option-chars , "," } | option-chars ;
  option-chars ∷= { ?all-characters? - "," } ;

I.e., we would like an option that parses strings like a:this,that,these. The following Haskell code builds an option --ignore that does exactly that:

  import Control.Applicative ((<|>))
  import Data.Text (Text)
  import Options.Applicative (Parser)
  import Options.Applicative.CmdLine.Util (optionA, splitOn, AttoParser)

  data Example = A [Text] | B [Text] | C [Text]

  pIgnore :: Parser [Example]
  pIgnore = many $ optionA (parse "a:" A <|> parse "b:" B <|> parse "c:" C)
     ( long "ignore"
    <> short 'i'
    <> help "Explanation of the feature."
    <> value []  -- default
    parse :: AttoParser Text -> ([Text] -> Example) -> AttoParser Example
    parse s d = s *> (d <$> splitOn sep)

    sep :: [Char]
    sep = ","

Note that, due to invocation of many, it would be possible to call --ignore multiple times—e.g., my-cli-program --ignore a:a,b,c --ignore c:d,e.