如何解析部分产品类型

问题描述 投票:0回答:1
data Config = Config {
    a :: Bool,
    b :: Type1,
    c :: Type2
}

pA :: Parser Bool

pB :: Parser Type1

pC :: Parser Type2

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

是否有可能来分析产品类型部分?配置是产品类型与成员a,b和c和假设这是来自一个库,所以我不能重新定义此。我只想解析a和b,而无需关心℃。但是,因为“分析器配置”只能有一个像下面施工

Config <$> pA <*> pB <*> pC

由于作为一个产品类型,如果我不给“PC”是错误的。如何正确处理这种情况?

haskell optparse-applicative
1个回答
3
投票

Config <$> pA <*> pB <*> pC符号不关心Config是一个构造函数;你也可以使用类型Bool -> Type1 -> Type2 -> Config的任何功能。如果你不想解析Type2,你可以使用类型Bool -> Type1 -> Config的任何功能。

config' :: Bool -> Type1 -> Config
config' a b c = Config a b someDefaultType2

pConfig :: Parser Config
pConfig = config' <$> pA <*> pB

同样地,你坚持使用Config构造函数,但连接pure值,而不是一个分析器,它的第三个参数。

pConfig :: Parser Config
pConfig = Config <$> pA <*> pB <*> pure someDefaultType2

(无论哪种方式,你就需要有提供Type2的值,如果你想获得一个Config出来的某种方式。)

© www.soinside.com 2019 - 2024. All rights reserved.