本文介绍了是否可以有一个optparse应用选项与几个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 现在我假设 roi 解析器将解析表达式,如 - roi 1 2 3 4 但它失败,并且无效参数'128'并给我使用消息。 - roi 1 改为解析,但返回 Just(ROI 1 1 1 1) 有没有办法让这项工作成功? 解决方案应该消耗多个参数。至少我不确定你会怎样去实现它。我建议你简单地放弃这个想法,并使用之类的语法将你的ROI选项放入一个参数中,例如roi 1,2,3,4 。 您只需为此实现自定义阅读器,以下是您如何做到这一点的示例: $ b data ROI = ROI Int Int Int Int 导出显示 - 不记得这个函数被调用了什么,不要使用这个 splitOn :: Eq a => a - > [a] - > [[a]] splitOn sep(x:xs)| sep == x = []:splitOn sep xs |否则= let(xs':xss)= splitOn sep xs in(x:xs'):xss splitOn _ [] = [[]] roiReader :: ReadM ROI roiReader = do o - 没有错误检查,实际上不这样做让[a,b,c,d] =地图读取$ splitOn', 'o return $ ROI abcd roiParser :: Parser ROI roiParser = option roiReader(longroi) main :: IO( ) main = execParser opts>> = print其中 opts = info(helper *> roiParser)fullDesc I just found out that my carefully crafted parser fails to parse any string I throw at it:roi :: Parser (Maybe ROI)roi = optional $ option (ROI <$> auto <*> auto <*> auto <*> auto) $ long "roi" <> metavar "ROI" <> help "Only process selected region of interest"where ROI = ROI Int Int Int IntIf that is important, it is nested in a higher parseroptions :: Parser Optsoptions = Opts <$> input <*> output <*> roi <*> startT <*> endTwhere Opts is an appropriate ADT.Now I assumed that the roi parser will parse expressions such as --roi 1 2 3 4 but it fails with Invalid argument '128' and giving me usage message.--roi 1 instead parses but returns Just (ROI 1 1 1 1)Is there a way to make this work? 解决方案 I don't think options are supposed to consume multiple arguments. At least I'm not sure how you'd go about implementing that. I'd suggest simply going away from that idea and putting your ROI options into a single argument, using syntax like --roi 1,2,3,4.You'd simply have to implement a custom reader for that, here's an example of how you could do that:module Main whereimport Options.Applicativedata ROI = ROI Int Int Int Int deriving Show-- didn't remember what this function was called, don't use thissplitOn :: Eq a => a -> [a] -> [[a]]splitOn sep (x:xs) | sep==x = [] : splitOn sep xs | otherwise = let (xs':xss) = splitOn sep xs in (x:xs'):xsssplitOn _ [] = [[]]roiReader :: ReadM ROIroiReader = do o <- str -- no error checking, don't actually do this let [a,b,c,d] = map read $ splitOn ',' o return $ ROI a b c droiParser :: Parser ROIroiParser = option roiReader (long "roi")main :: IO ()main = execParser opts >>= print where opts = info (helper <*> roiParser) fullDesc 这篇关于是否可以有一个optparse应用选项与几个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..
09-08 00:49