pcapriotti / pcapriotti/optparse-applicative

problem combining partially overlapping parsers with Alternative

Open
#333 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Haskell
Stars
959
Forks
123
Avg merge
3d 19h
Merged PRs (30d)
1

Description

The test program below prints this --help

Usage: foo ([--bar ARG] [string] | [string])

And "./foo --bar a b" works. However:

joey@darkstar:~>./foo a
Missing: --bar ARG

Seems that in bar <|> foo, the bar parser can fail in a way that makes it never try the
foo parser, which would succeed if it were tried.

If it's changed to foo <|> bar, the example above works, as does "./foo --bar a b" , so that version of the program works ok. Except, "./foo b --bar a" does not work; normally the bar
parser would accept that, but now the foo parser fails in a way that makes the bar parser not work.

import Options.Applicative
import Options.Applicative.Internal

data Foo = Foo [String] | Bar String String
        deriving (Show)

parser :: Parser Foo
parser = bar <|> foo
  where
        foo = Foo <$> many (argument str ( metavar ""))
        bar = Bar <$> strOption (long "bar") <*> argument str (metavar "dir")

main = print =<< execParser opts
  where
         opts = info (parser <**> helper)
                ( fullDesc
                <> progDesc "testcase"
                <> header "testcase"
                )

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling and running the supplied Haskell program, then inspect Options.Applicative.Internal and the Alternative behavior for the two parser orderings. Done means both orderings handle ./foo a, ./foo --bar a b, and ./foo b --bar a without an incorrect Missing: --bar ARG failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
cli
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.