【问题标题】:cmdargs value arguments without equals sign没有等号的 cmdargs 值参数
【发布时间】:2014-03-26 22:33:30
【问题描述】:

我使用cmdargs 为我的 Haskell 程序编写 CLI 解析器。

让我们假设这个程序直接来自他们的示例:

{-# LANGUAGE DeriveDataTypeable #-}
import System.Console.CmdArgs.Implicit

data Sample = Sample {hello :: String} deriving (Show, Data, Typeable)

sample = Sample{hello = def &= help "World argument" &= opt "world"}
         &= summary "Sample v1"

main = print =<< cmdArgs sample

如果这个程序叫cmdtest.hs,我可以执行

$ runghc cmdtest.hs --hello=world
Sample {hello = "world"}

但是,如果我不使用等号(如在 gcc -o foo foobar.c 中的 -o 选项中),cmdargs 会尝试将 world 识别为位置参数

$ runghc cmdtest.hs --hello world
Unhandled argument, none expected: world

我可以设置任何选项/注释以允许在 cmdargs 中使用此语法吗?

更新:使用 short 选项会暴露同样的问题:

$ runghc cmdtest.hs -h world
Unhandled argument, none expected: world

更新 2: cmdargs Explicit 模块中的 FlagInfo data 似乎表明它是有可能的,至少如果使用 Explicit 而不是 Implicit

【问题讨论】:

    标签: haskell command-line-arguments haskell-cmdargs


    【解决方案1】:

    您的问题是您使用的是opt,这使得--hello 的值是可选的。所以--hello world 被认为是没有值的--hello,然后是world 作为位置参数。这当然是cmdargs 中的一种设计选择,但我认为这是一个合理的选择,它与大多数其他程序的行为相匹配。取出opt,看看它是否按照你想要的方式工作。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 2011-03-03
    • 2017-08-11
    • 2012-04-13
    • 1970-01-01
    相关资源
    最近更新 更多