我有以下代码;
module Billing.Options
(
GlobalOpts(..)
, globalOptsParser
, parseDb
, parseSql
) where
import Options.Applicative
import Options.Applicative.Simple
import Options.Applicative.Types
import System.FilePath.Posix
import Text.Regex.PCRE
-- ------------------------------------------------------------
data GlobalOpts = GlobalOpts
{
optDb :: String,
optSql :: String
} deriving Show
-- ------------------------------------------------------------
globalOptsParser :: Parser GlobalOpts
globalOptsParser = GlobalOpts
<$> option (parseDb =<< readerAsk)
( long "db"
<> short 'd'
<> metavar "<DB name>"
<> help "dmt | report"
)
<*> option parseSql
( long "sql"
<> metavar "<SQL SELECT statement>"
<> help "sql select statement to use in order to generate JSON config file"
)
-- ------------------------------------------------------------
matches :: String -> String -> Bool
matches = (=~)
-- ------------------------------------------------------------
parseDb :: String -> ReadM String
parseDb val = do
if not (elem val ["dmt", "report"])
then readerError $ "Unknown DB, '" ++ val ++ "'"
else return val
-- ------------------------------------------------------------
parseSql :: ReadM String
parseSql = do
val <- readerAsk
if not (val `matches` "(?i)select .+ from .+")
then readerError $ "Please provide a valid SQL SELECT statement"
else return val
-- [EOF]
我正在使用以下代码测试我的参数解析器;
error' = let mp = runParser AllowOpts globalOptsParser ["-d", "billing"]
opts = ParserPrefs "suffix" False False False 80
in fst $ runP mp opts
所需的参数是,
-d <DB name>
--sql <SQL SELECT statement>
我想测试我是否收到错误消息,
Missing: --sql <SQL SELECT statement>
当我只指定“-d billing”时。
如果我打印结果,上面的测试代码会给出以下输出,
Left (MissingError (MultNode [MultNode [MultNode [AltNode [Leaf (Chunk {unChunk = Just --sql <SQL SELECT statement>})]]]]))
有没有办法从上述结果(任一数据类型)生成预期的错误消息(字符串)?Haskell 是否提供了用于此目的的明显功能,因为我在文档中找不到某些内容,并且谷歌搜索示例也没有产生任何答案。