pcapriotti / optparse-applicative

Applicative option parser
BSD 3-Clause "New" or "Revised" License
910 stars 116 forks source link

partitioning args #489

Closed juhp closed 4 months ago

juhp commented 4 months ago

I have wondered for a while if/why not this is possible:

Some of my tools take multiple arguments of two types, one of which can be easily identified (parsed uniquely): a typical example might be: version1 version2 version3 name1 name2 name3

ie eg 18 20 22 ghc idris Agda say

The first args might always be Natural's (or Version's or some specific Branch's etc). However naively parsing some of them will always fail when reaching the second type of args (eg 'ghc' in the above example).

cannot parse value `ghc'

My fear is this is just not possible with applicatives perhaps, which seems quite a shame? Anyway I have been working around it by just passing all the args though as [String] and then partitioning afterwards (with partitionEithers), but it seems kinda ugly for something that naively could work more simply.

Is there anything that could be done for this or am I just dreaming?

I could attach some minimal code example if it helps..

HuwCampbell commented 4 months ago

The ReadM type has an alternative instance, so you can do the left and right parsers there, and as Parser is a Functor, you can also put a partition eithers after many or some too.

See the s3 parser is the readme.

It seems like a weird CLI design though.

HuwCampbell commented 4 months ago

If it had to be separated with nats first and no separate you'll have to break in to the ParserM type, but it won't be pretty.

juhp commented 4 months ago

Thank you that's helpful