【问题标题】:Parsing "enum" options with optparse-applicative使用 optparse-applicative 解析“枚举”选项
【发布时间】:2017-09-13 02:19:30
【问题描述】:

我如何从grep --help 实现此示例的解析器:

 --binary-files=TYPE   assume that binary files are TYPE;
                       TYPE is 'binary', 'text', or 'without-match'

假设我有

data BinaryFiles = Binary | Text | WithoutMatch

如何编写解析器? option auto 似乎是一团糟,因为 Read 应该是 Show 的“逆”,我想保留派生的 instance Show BinaryFiles

【问题讨论】:

    标签: haskell optparse-applicative


    【解决方案1】:

    使用str 代替auto

    binFile :: ReadM BinaryFiles
    binFile = str >>= \s -> case s of
        "binary"        -> return Binary
        "text"          -> return Text
        "without-match" -> return WithoutMatch
        _ -> readerError "Accepted binary file types are 'binary', 'text', and 'without-match'."
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多