如何在Options.Applicative

时间:2020-07-08 16:41:53

标签: haskell optparse-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包中有一个使用<|>的示例,但它不是布尔标志的,而且我不知道如何进行转换。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:8)

使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')