Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/slotthe/optparse-applicative-cmdline-util
Utility functions for writing command line interfaces with optparse-applicative // GitHub Mirror
https://github.com/slotthe/optparse-applicative-cmdline-util
cli haskell optparse-applicative
Last synced: 26 days ago
JSON representation
Utility functions for writing command line interfaces with optparse-applicative // GitHub Mirror
- Host: GitHub
- URL: https://github.com/slotthe/optparse-applicative-cmdline-util
- Owner: slotThe
- License: agpl-3.0
- Created: 2022-08-09T19:19:09.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-03-18T11:57:03.000Z (9 months ago)
- Last Synced: 2024-11-17T17:54:55.368Z (about 1 month ago)
- Topics: cli, haskell, optparse-applicative
- Language: Haskell
- Homepage: https://gitlab.com/slotThe/optparse-applicative-util
- Size: 37.1 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# 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:
``` 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:``` haskell
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`.