Haskell,optparse-generic

时间:2016-04-03 10:42:53

标签: haskell command-line-arguments optparse-generic

我正在使用optparse-generic来解析名为example的程序的命令行参数。我有一个带有未命名字段的数据类型。例如:

data Unlabeled = Unlabeled String deriving (Generic, Show)

这会生成一个程序,可以按如下方式调用:./exmaple "foo"。但是,对于用户,没有文档String - 参数的内容。特别是,./example --help没有提供有关此位置String参数./example所期望的任何有价值的信息。

使用命名数据类型(记录语法),可以向数据类型添加文档。例如

data Labeled = Labeled {name :: String <?> "Select the foo"} deriving (Generic, Show)

这会为程序生成帮助文本。例如,当调用./example --help时,它将显示--name STRING Select the foo

如何以与记录语法数据类型相同的方式向未命名的数据类型添加文档?

1 个答案:

答案 0 :(得分:1)

数据Labeled = Labeled (String <?> "Select the foo")会给你

...
STRING     Select the foo
... 
--help消息中的

。或许澄清一下,<?>只是一个类型构造函数,它在语法上只是一个运算符。也许有趣的事实:你也可以写data X = X (Int `Either` Bool)