module Main where
import Options.Applicative
import Control.Monad
import Control.Monad.State
main :: IO ()
main = join . customExecParser (prefs showHelpOnError) $
info (helper <*> parser) fullDesc
parser =
example <$> strOption (long "aaa")
<|>
example2 <$> strOption (long "aaa")
<*> strOption (long "bbb")
example :: String -> IO ()
example x = do
print "example..."
print x
example2 :: String -> String -> IO ()
example2 x y = do
print "example2..."
print x
print y
使用上面的代码和以下参数::set args --aaa test --bbb test2
结果:
Invalid option `--bbb'
Usage: <interactive> (--aaa ARG | --aaa ARG --bbb ARG)
Available options:
-h,--help Show this help text
*** Exception: ExitFailure 1
我怎样才能成功解析第二种情况(关于example2
)?
该问题似乎与strOption (long "aaa")
两个解析器中都存在的解析器代码有关。是否有替代方案(<|>)
可以实现所需的行为?