Skip to content

problem combining partially overlapping parsers with Alternative #333

Open
@joeyh

Description

@joeyh

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions