如何在 Options.Applicative 中编码互斥标志

How to code for mutually exclusive flags in Options.Applicative

我是 Haskell 新手。作为一项学习练习,我正在尝试将我的一个 Rust 程序移植到 Haskell。在 Rust 中,我使用了令人惊叹的 clap 包,并且发现 Options.Applicative 作为一个漂亮的替代品。这是一个例子:

import Options.Applicative
import Data.Semigroup ((<>))

data Sample = Sample
  { tod        :: Bool
  , pmc        :: Bool
  , tai        :: Bool
  }

sample :: Parser Sample
sample = Sample
      <$>  switch
          ( long "tod"
            <> short 'o'
            <> help "Convert from TOD" )
      <*> switch
          ( long "pmc"
            <> short 'p'
            <> help "Convert from PMC" ) 
      <*> switch
          ( long "tai"
            <> short 't'
            <> help "Set TAI mode" )

main :: IO ()
main = greet =<< execParser opts
  where
    opts = info (sample <**> helper) ( fullDesc )
greet :: Sample -> IO ()
greet (Sample a b c) = print [a,b,c]

说到这里,我碰壁了。我需要使“tod”和“pmc”标志相互排斥。 README 包中有一个使用 <|> 的示例,但它不适用于布尔标志,我不知道如何转换它。

有人能帮忙吗?

pmctod中的一个作为计算值,只存储另一个。

data Sample = Sample
    { tod :: Bool
    , tai :: Bool
    }

pmc = not . tod

sample = Sample
    <$> (   flag' True  (short 'o')
        <|> flag' False (short 'p')
        <|> pure True -- what do you want to do if they specify neither?
        )
    <*> switch (short 't')

或者说实际上有三种操作模式。然后将 todpmc 都设为计算字段。

data Mode = TOD | PMC | Other deriving Eq

data Sample = Sample
    { mode :: Mode
    , tai :: Bool
    }

tod = (TOD==) . mode
pmc = (PMC==) . mode

sample = Sample
    <$> (   flag' TOD (short 'o')
        <|> flag' PMC (short 'p')
        <|> pure Other
        )
    <*> switch (short 't')