我将从参数计数中确定程序模式(没有标记/' subparser'&'命令')但没有成功。
main% stack ghci optparse-applicative
Configuring GHCi with the following packages:
GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help
λ: :load Main.hs
[1 of 1] Compiling Main ( Main.hs, interpreted )
Ok, modules loaded: Main.
λ: :main -v
v 0.0.1
λ: :main a
mode 1 with a: a
λ: :main a b
Usage: <interactive> (-v | a | a b)
*** Exception: ExitFailure 1
:main a b 我预期模式2,结果为:a和b:b 。
我错过了什么?谢谢!
module Main where
import Options.Applicative
data AppArgs = ShowVersion
| Mode1 {
a :: String
}
| Mode2 {
a :: String
, b :: String
}
main :: IO ()
main = execParser opts >>= run
where opts = info (helper <*> appArgs)
( fullDesc
<> header "example")
appArgs :: Parser AppArgs
appArgs = flag' ShowVersion (short 'v')
<|> Mode1 <$> strArgument (metavar "a")
<|> Mode2 <$> strArgument (metavar "a") <*> strArgument (metavar "b")
run :: AppArgs -> IO ()
run ShowVersion = putStrLn "v 0.0.1"
run (Mode1 a) = putStrLn $ "mode 1 with a: " ++ a
run (Mode2 a b) = putStrLn $ "mode 2 with a: " ++ a ++ " and b: " ++ b