Uli*_*ler 7 haskell command-line-arguments haskell-cmdargs
我用来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
Run Code Online (Sandbox Code Playgroud)
如果调用该程序cmdtest.hs,我可以执行
$ runghc cmdtest.hs --hello=world
Sample {hello = "world"}
Run Code Online (Sandbox Code Playgroud)
但是,如果我省略等号(如-o选项中所示gcc -o foo foobar.c),cmdargs会尝试识别world为位置参数
$ runghc cmdtest.hs --hello world
Unhandled argument, none expected: world
Run Code Online (Sandbox Code Playgroud)
是否有任何选项/注释我可以设置为在cmdargs中允许此语法?
更新:使用短选项会暴露同样的问题:
$ runghc cmdtest.hs -h world
Unhandled argument, none expected: world
Run Code Online (Sandbox Code Playgroud)
更新2:将在cmdargs 模块似乎表明它是某种可能的,至少如果使用的不是FlagInfo dataExplicitExplicitImplicit
你的问题是你正在使用opt这使得可选值--hello。so--hello world表示--hello没有值,后跟world作为位置参数。这当然是 中的一种设计选择cmdargs,但我认为这是一个合理的选择,并且它与大多数其他程序的行为相匹配。拿出来opt看看它是否按照您想要的方式工作。