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]

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.2.0, 0.2.1
Change log CHANGELOG.md
Dependencies attoparsec (>=0.13.2.4 && <0.16), base (>=4.7 && <5), optparse-applicative (>=0.15.0.0 && <0.19), text (>=1.2.4.0 && <2.3) [details]
License AGPL-3.0-only
Copyright 2020–2024 Tony Zorman
Author Tony Zorman
Maintainer soliditsallgood@mailbox.org
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 2024-03-18T11:59:58Z
Distributions
Downloads 106 total (22 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for optparse-applicative-cmdline-util-0.2.1

[back to package description]

optparse-applicative-cmdline-util

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).

Example

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
     )
   where
    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.