1

I want to parse command line options corresponding to a product type resembling below.


data SumType1 = d | e | f

data SumType2 = g | h | i

data Config = Config {
    a :: Bool,
    b :: SumType1,
    c :: SumType2
}

pB :: Parser SumType1
pB = flag' d <|> flag' e <|> flag' f

pC :: Parser SumType2
pC = flag' g <|> flag' h <|> flag' i

pConfig :: Parser Config
pConfig = Config <$> pA <*> pB <*> pC

opts :: ParserInfo Config
opts = info (pConfig <**> helper)
       (fullDesc <> progDesc "My CLI" <> header "CLI executable")

main :: IO()
main = do
       (Config a b c) <- execParser opts
-- Populate a default config using a b c values

For simplicity I did not add "long" and "help" here with flag'.

The above is what I have right now. It may also be that "d" itself is another sum type. In this the problem is each of the a, b, c values I need to specify on the command line.

How do I specify such that either of a, b or c may not be specified and the CLI parser still works. Currently if I do not pass even one of a, b or c the parser fails with an error message. I understand that my builder construction is incorrect, but, I am not able to grasp how to fix it. Hope I have described the problem correctly, if not please let me know.

4

1 回答 1

1

Try adding a default alternative to the sum parsers:

pB :: Parser SumType1
pB = flag' d <|> flag' e <|> flag' f <|> pure d  -- defaults to d
于 2019-02-01T14:08:39.753 回答