我将根据参数计数确定程序模式(没有标志/'subparser' & 'command')但没有成功。
ghci 会话
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 ab我期望模式 2 与 a: 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