是否可以有一个optparse应用选项与几个参数? [英] Is it possible to have a optparse-applicative option with several parameters?

查看:120
本文介绍了是否可以有一个optparse应用选项与几个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  roi :: Parser解析器(可能ROI)
roi =可选的$ option(ROI< $> auto> auto> auto> auto> auto)
$ longroi< ;> metavarROI<>帮助仅处理选定的感兴趣区域

其中 ROI = ROI Int Int Int Int



如果这很重要,它嵌套在一个更高的解析器中

  options :: Parser Opts 
options = Opts< $>输入< *>输出< *> roi * startT< *> endT

其中 Opts 是一个合适的ADT。

现在我假设 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 < - str
- 没有错误检查,实际上不这样做
让[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 Int

If that is important, it is nested in a higher parser

options :: Parser Opts
options = Opts <$> input <*> output <*> roi <*> startT <*> endT  

where 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 where

import Options.Applicative

data ROI = ROI Int Int Int Int
  deriving Show

-- didn't remember what this function was called, don't use this
splitOn :: Eq a => a -> [a] -> [[a]]
splitOn sep (x:xs) | sep==x     = [] : splitOn sep xs
                   | otherwise = let (xs':xss) = splitOn sep xs in (x:xs'):xss
splitOn _ [] = [[]]

roiReader :: ReadM ROI
roiReader = 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 d

roiParser :: Parser ROI
roiParser = option roiReader (long "roi")

main :: IO ()
main = execParser opts >>= print where
  opts = info (helper <*> roiParser) fullDesc

这篇关于是否可以有一个optparse应用选项与几个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆