module GenHeader.Parsers (parseOptions) where

import Cardano.Tools.Headers (Options (..))
import Data.Version (showVersion)
import Options.Applicative
  ( Parser
  , ParserInfo
  , auto
  , command
  , execParser
  , help
  , helper
  , hsubparser
  , info
  , long
  , metavar
  , option
  , progDesc
  , short
  , (<**>)
  )
import Paths_ouroboros_consensus_cardano (version)

parseOptions :: IO Options
parseOptions :: IO Options
parseOptions = ParserInfo Options -> IO Options
forall a. ParserInfo a -> IO a
execParser ParserInfo Options
argsParser

argsParser :: ParserInfo Options
argsParser :: ParserInfo Options
argsParser =
  Parser Options -> InfoMod Options -> ParserInfo Options
forall a. Parser a -> InfoMod a -> ParserInfo a
info
    (Parser Options
optionsParser Parser Options -> Parser (Options -> Options) -> Parser Options
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> Parser (Options -> Options)
forall a. Parser (a -> a)
helper)
    ( String -> InfoMod Options
forall a. String -> InfoMod a
progDesc (String -> InfoMod Options) -> String -> InfoMod Options
forall a b. (a -> b) -> a -> b
$
        [String] -> String
unlines
          [ String
"gen-header - A utility to generate valid and invalid Praos headers for testing purpose"
          , String
"version: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Version -> String
showVersion Version
version
          ]
    )

optionsParser :: Parser Options
optionsParser :: Parser Options
optionsParser =
  Mod CommandFields Options -> Parser Options
forall a. Mod CommandFields a -> Parser a
hsubparser
    ( String -> ParserInfo Options -> Mod CommandFields Options
forall a. String -> ParserInfo a -> Mod CommandFields a
command
        String
"generate"
        ( Parser Options -> InfoMod Options -> ParserInfo Options
forall a. Parser a -> InfoMod a -> ParserInfo a
info
            Parser Options
generateOptionsParser
            ( String -> InfoMod Options
forall a. String -> InfoMod a
progDesc
                String
"Generate Praos headers context and valid/invalid headers. Writes JSON formatted context to stdout and headers to stdout."
            )
        )
        Mod CommandFields Options
-> Mod CommandFields Options -> Mod CommandFields Options
forall a. Semigroup a => a -> a -> a
<> String -> ParserInfo Options -> Mod CommandFields Options
forall a. String -> ParserInfo a -> Mod CommandFields a
command
          String
"validate"
          ( Parser Options -> InfoMod Options -> ParserInfo Options
forall a. Parser a -> InfoMod a -> ParserInfo a
info
              Parser Options
validateOptionsParser
              ( String -> InfoMod Options
forall a. String -> InfoMod a
progDesc
                  String
"Validate a sample of Praos headers within a context. Reads JSON formatted sample from stdin."
              )
          )
    )

validateOptionsParser :: Parser Options
validateOptionsParser :: Parser Options
validateOptionsParser = Options -> Parser Options
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Options
Validate

generateOptionsParser :: Parser Options
generateOptionsParser :: Parser Options
generateOptionsParser =
  Int -> Options
Generate (Int -> Options) -> Parser Int -> Parser Options
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Int
countParser

countParser :: Parser Int
countParser :: Parser Int
countParser =
  ReadM Int -> Mod OptionFields Int -> Parser Int
forall a. ReadM a -> Mod OptionFields a -> Parser a
option
    ReadM Int
forall a. Read a => ReadM a
auto
    ( String -> Mod OptionFields Int
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"count"
        Mod OptionFields Int
-> Mod OptionFields Int -> Mod OptionFields Int
forall a. Semigroup a => a -> a -> a
<> Char -> Mod OptionFields Int
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'c'
        Mod OptionFields Int
-> Mod OptionFields Int -> Mod OptionFields Int
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields Int
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"INT"
        Mod OptionFields Int
-> Mod OptionFields Int -> Mod OptionFields Int
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields Int
forall (f :: * -> *) a. String -> Mod f a
help String
"Number of headers to generate"
    )