I'm trying to use optparse-applicative to parse a Maybe String
but I can't find anywhere how to deal with Maybe
. The only thing I found is to add a default value but I really need a Nothing
if user didn't supply an option instead of ""
. Is there any way to achieve this ?
Here is an example of working code:
import Options.Applicative
data Config = Config
{ cIn :: String
, cOut :: String
} deriving Show
configParser :: Parser Config
configParser = Config
<$> strOption (long "in" <> short 'i')
<*> strOption (long "out" <> short 'o')
main :: IO ()
main = do
conf <- execParser (info configParser fullDesc)
print conf
However, I would like the parameters to be optional and use Maybe String
instead of String
in Config
:
data Config = Config
{ cIn :: Maybe String
, cOut :: Maybe String
} deriving Show
See the following passage of the
optparse-applicative
README:Accordingly, all you have to do is apply the
optional
combinator to the result ofstrOption
:Tests at the command line: