程序模式由参数count [optparse-applicative]确定

时间:2015-12-15 16:18:48

标签: haskell command-line-arguments optparse-applicative

我将从参数计数中确定程序模式(没有标记/' subparser'&'命令')但没有成功。

ghci session

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

我错过了什么?谢谢!

Main.hs

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

0 个答案:

没有答案