Haskell读取args值

时间:2012-04-09 12:00:44

标签: haskell functional-programming args

我在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'标志的值 - &gt;我通过谷歌搜索,但我没有发现任何东西(可能是错误的关键字)。他们只是检测论证是否在任何地方都是活跃的,而不是它的价值。

1 个答案:

答案 0 :(得分: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.Maybe导入mapMaybelistToMaybe。)