2

引数の数からプログラムモードを決定します(フラグ/「サブパーサー」と「コマンド」なし)が、成功しません。

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を使用すると、結果としてa: a および b: b のモード 2 が期待されます。

私は何を取りこぼしたか?ありがとう!


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
4

0 に答える 0