How to parse an optional flag as a Maybe value?

mb14

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
jub0bs

See the following passage of the optparse-applicative README:

Parsers are instances of both Applicative and Alternative, and work with any generic combinator, like many and some. For example, to make a option return Nothing instead of failing when it's not supplied, you can use the optional combinator in Control.Applicative:

optional $ strOption
   ( long "output"
  <> metavar "DIRECTORY" )

Accordingly, all you have to do is apply the optional combinator to the result of strOption:

import Options.Applicative

data Config = Config
    { cIn  :: Maybe String
    , cOut :: Maybe String
    } deriving Show

configParser :: Parser Config
configParser = Config
    <$> (optional $ strOption $ long "in" <> short 'i')
    <*> (optional $ strOption $ long "out" <> short 'o')

main :: IO ()
main = do
    conf <- execParser (info configParser fullDesc)
    print conf

Tests at the command line:

$ main --in foo -o bar
Config {cIn = Just "foo", cOut = Just "bar"}
$ main -i foo
Config {cIn = Just "foo", cOut = Nothing}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to parse the flag value from a LINUX command string with Javascript

From Dev

How to log a Maybe value?

From Dev

How to securely quote an optional flag?

From Dev

How to parse optional inputs in MATLAB?

From Dev

How to parse optional URL parameters

From Dev

How to set flag value in rails?

From Dev

How can you parse NON wellformed JSON (maybe using jQuery)

From Dev

How can you parse NON wellformed JSON (maybe using jQuery)

From Dev

How to handle optional parameter + optional return value

From Dev

How to use boost::program_options to accept an optional flag?

From Dev

How to enter an optional flag with no parameters in Go CLI program

From Dev

How to parse date with optional characters in format

From Dev

How to parse optional and named arguments into list and dict?

From Dev

how to parse optional values from json in android

From Dev

How to parse date with optional characters in format

From Dev

how to compare value in swift optional

From Dev

How to return value in orElse in Optional?

From Dev

How to print optional value in Swift?

From Dev

How to define a @Value property as optional?

From Dev

Unexpectedly found nil while unwrapping optional value (Swift, Parse)

From Dev

How to insert a null value in Sequelize with omitNull flag?

From Dev

How to test C Preprocessor -D flag value?

From Dev

How to test C Preprocessor -D flag value?

From Dev

How to get a value with argument/flag from controller

From Dev

How do I get the value OUT of my maybe<> monad?

From Dev

How to check if string starts with value containing #, tab, and maybe space?

From Dev

Manipulating a value in a Maybe

From Dev

Get value of Maybe in Haskell

From Dev

argparse: optional argument as flag and variable

Related Related

  1. 1

    How to parse the flag value from a LINUX command string with Javascript

  2. 2

    How to log a Maybe value?

  3. 3

    How to securely quote an optional flag?

  4. 4

    How to parse optional inputs in MATLAB?

  5. 5

    How to parse optional URL parameters

  6. 6

    How to set flag value in rails?

  7. 7

    How can you parse NON wellformed JSON (maybe using jQuery)

  8. 8

    How can you parse NON wellformed JSON (maybe using jQuery)

  9. 9

    How to handle optional parameter + optional return value

  10. 10

    How to use boost::program_options to accept an optional flag?

  11. 11

    How to enter an optional flag with no parameters in Go CLI program

  12. 12

    How to parse date with optional characters in format

  13. 13

    How to parse optional and named arguments into list and dict?

  14. 14

    how to parse optional values from json in android

  15. 15

    How to parse date with optional characters in format

  16. 16

    how to compare value in swift optional

  17. 17

    How to return value in orElse in Optional?

  18. 18

    How to print optional value in Swift?

  19. 19

    How to define a @Value property as optional?

  20. 20

    Unexpectedly found nil while unwrapping optional value (Swift, Parse)

  21. 21

    How to insert a null value in Sequelize with omitNull flag?

  22. 22

    How to test C Preprocessor -D flag value?

  23. 23

    How to test C Preprocessor -D flag value?

  24. 24

    How to get a value with argument/flag from controller

  25. 25

    How do I get the value OUT of my maybe<> monad?

  26. 26

    How to check if string starts with value containing #, tab, and maybe space?

  27. 27

    Manipulating a value in a Maybe

  28. 28

    Get value of Maybe in Haskell

  29. 29

    argparse: optional argument as flag and variable

HotTag

Archive