2

我在读取haskell中的参数值时遇到了一点问题:

我有这样声明的选项:

options :: [OptDescr ArgFlag]
options = [Option [] ["help"] (NoArg Help) "",
           Option [] ["version"] (NoArg Version) "",
           Option ['a'] [] (NoArg FSAll) "",
           Option ['L'] [] (ReqArg FSLevel "NUM") ""]

我读了这样的论点(我可以读到哪个论点是活动的):

main = do
         args <- getArgs
         case getOpt RequireOrder options args of
             ([Version], [], []) -> do ...

             ([Help], [], []) -> do ...

             (flags, files, []) -> do ........ *

             otherwise -> do ...

在有“*”字符的地方,我需要读取可能的“-l 3”标志的值 -> 我通过谷歌搜索但我什么也没找到(虽然可能是错误的关键字)。他们只是检测论点是否无处不在,而不是它的价值。

4

1 回答 1

2

我想你已经定义了

data ArgFlag = Help
             | Version
             | FSAll
             | FSLevel String

带有辅助功能

unFSLevel :: ArgFlag -> Maybe String
unFSLevel (FSLevel value) = Just value
unFSLevel _               = Nothing

然后你可以得到这样的值:

case listToMaybe $ mapMaybe unFSLevel of
     Just value -> -- code to handle the -L flag having this value
     Nothing    -> -- code to handle there not being an -L flag

(您需要从Data.MaybemapMaybe导入和导入。)listToMaybe

于 2012-04-09T12:14:55.410 回答