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”是错误的。如何正确处理这种情况?
该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
出来的某种方式。)