根据您的描述,我认为您有两种选择。在这两者中,我会说转换为字典是最简单的,但转换为元组会起作用,只是有点笨拙
所以,采用这个定义:
options :: [OptDescr (String, String)]
options = [Option ['a'] ["alpha"] (ReqArg (\a -> ("alpha", a)) "empty") "",
Option ['b'] ["beta"] (ReqArg (\a -> ("beta", a)) "empty") "",
Option ['g'] ["gamma"] (ReqArg (\a -> ("gamma", a)) "empty") ""]
main = do
args <- getArgs
let (opts, nonopts, errs) = getOpt Permute options args
putStrLn $ show opts
由此,我的几个示例输出是:
[("beta","b"),("alpha","a")]
[("alpha","a"),("gamma","g"),("beta","b")]
等等。与命令行相同的顺序。但是,由于我在上面设置它的方式,我基本上有一个关联列表,所以......如果我特别想要一个具有值(alpha,beta,gamma)的元组,那么我最好的选择是......
(lookup "alpha" opts, lookup "beta" opts, lookup "gamma" opts)
您生成的数据类型将是(Maybe String、Maybe String、Maybe String),按“alpha”、“beta”和“gamma”的顺序排列。