optparse-applicative icon indicating copy to clipboard operation
optparse-applicative copied to clipboard

Can't parse `some` or `many` arguments followed by one

Open richsmith92 opened this issue 10 years ago • 3 comments

I want to implement cp-like behavior, where many arguments are parsed as source and then the last one as destination. But this fails with optparse-applicative. The only way which works is to swap the order: have one argument followed by many args.

So, this works:

ghci> execParserMaybe (info ((,) <$> (strArgument mempty) <*> many (strArgument mempty)) mempty) $ ["a", "b", "c"]
Just ("a",["b","c"])

But this doesn't:

ghci> execParserMaybe (info ((,) <$> many (strArgument mempty) <*> strArgument mempty) mempty) $ ["a", "b", "c"]
Nothing

richsmith92 avatar Nov 04 '15 18:11 richsmith92

I don't have a great answer for this. It's like in Text.Parsec or Data.Attoparsec saying: let a = (,) <$> many anyChar <*> anyChar You'll never reach the last char as the many is too greedy. It's possible to do this with a lookahead in Parsec, but that's something rather heavy for a command line parser.

(remember too that you can always take the init and last from the parsed args list)

HuwCampbell avatar Nov 06 '15 04:11 HuwCampbell

@w3rs I would suggest using a single many argument and then simply take the last item from the resulting list and strip off the last item of the list and give it whatever different treatment you need.

mightybyte avatar Feb 23 '17 16:02 mightybyte

Has there been any progress on this issue? This is a feature I would like to have.

justinlovinger avatar Aug 28 '20 13:08 justinlovinger