0

私は optparse-applicative で 2 つの問題を抱えています。

  1. 使用法は次のとおりです: (COMMAND | COMMAND)

    Crime cli - CRIME テンプレートの処理とコマンドの展開

    使用法: (COMMAND | COMMAND) CRIME の CLI

実際にプログラムの名前をリストし(インタラクティブではない)、コマンドもリストしたいのですが、特定のサブコマンドのメタデータを追加する方法がわかりません。

  1. 特定のサブコマンドのヘルプを取得しようとすると、必要な引数が欠落しているというメッセージが表示されます。(

stack exec commandline-exe -- DeployStack --help

> Invalid option `--help'
> 
> Usage: <interactive> DeployStack (-d|--deployment DEPLOYMENT)
>                                  (-t|--template TEMPLATE) 
>                                  [-p|--provider PROVIDER]   Deploy a Stack

ヘルプを作成する方法は次のとおりです。

-- For information about how this was done see: https://stackoverflow.com/questions/36339103/using-optparse-applicative-with-multiple-subcommands-and-global-options
-- and https://hackage.haskell.org/package/optparse-applicative
module CLIParser
  ( Actions(..)
  , actions
  ) where

import           Data.Semigroup      ((<>))
import           Options.Applicative
import           Text.Show.Functions

data Actions
  = CreateTemplate
      { name :: String
      , path :: String
      }
  | DeployStack
      { deployment :: String
      , template   :: String
      , provider   :: String
      }
  deriving (Show)

actions :: Parser Actions
actions =
  subparser
    (command
       "CreateTemplate"
       (info templateCreator (progDesc "Create Template")) <>
     commandGroup "Template commands:") <|>
  subparser
    (command "DeployStack" (info deployStack (progDesc "Deploy a Stack")) <>
     commandGroup "Deploy commands:")

templateCreator :: Parser Actions
templateCreator = CreateTemplate <$> nameArg <*> pathArg

deployStack :: Parser Actions
deployStack = DeployStack <$> deployArg <*> templateArg <*> providerArg

nameArg :: Parser String
nameArg =
  strOption
    (long "name" <> metavar "NAME" <> short 'n' <> help "Name to give template")

pathArg :: Parser String
pathArg =
  strOption
    (long "path" <> metavar "PATH" <> short 'p' <> help "Path to template file")

deployArg :: Parser String
deployArg =
  strOption
    (long "deployment" <>
     metavar "DEPLOYMENT" <> short 'd' <> help "Name of deployment")

templateArg :: Parser String
templateArg =
  strOption
    (long "template" <>
     metavar "TEMPLATE" <> short 't' <> help "Template for deployement")

providerArg :: Parser String
providerArg =
  strOption
    (long "provider" <>
     metavar "PROVIDER" <>
     short 'p' <> showDefault <> value "AWS" <> help "Cloud Provider (e.g. AWS)")

** Main.hs **

module Main where

import CLIParser
import Options.Applicative
import System.IO

options :: ParserInfo Actions
options = info (actions <**> helper)
  ( fullDesc
    <> progDesc "The CLI for CRIME"
    <> header "crime cli - process CRIME template and deploy commands" )

main :: IO ()
main = execParser options >>= display


display :: Show a => a -> IO ()
display x = print x
4

1 に答える 1