|
| 1 | +{-# LANGUAGE ApplicativeDo #-} |
| 2 | +{-# LANGUAGE StrictData #-} |
| 3 | + |
| 4 | +module Options ( |
| 5 | + Options (..), |
| 6 | + options, |
| 7 | + parserInfo, |
| 8 | +) |
| 9 | +where |
| 10 | + |
| 11 | +import Data.ByteString (ByteString) |
| 12 | +import Options.Applicative qualified as O |
| 13 | +import PlutusLedgerApi.Common (MajorProtocolVersion (..), PlutusLedgerLanguage (..)) |
| 14 | +import PlutusLedgerApi.Common.Versions (knownPVs) |
| 15 | +import Text.Read (readMaybe) |
| 16 | + |
| 17 | +data Options = Options |
| 18 | + { scriptContextHex :: ByteString -- hex-encoded script context |
| 19 | + , datumHex :: ByteString -- hex-encoded datum |
| 20 | + , redeemerHex :: ByteString -- hex-encoded redeemer |
| 21 | + , scriptHex :: ByteString -- hex-encoded script |
| 22 | + , costModel :: String -- cost model parameter values "[1, 2, 3]" |
| 23 | + , plutusLedgerLanguage :: PlutusLedgerLanguage |
| 24 | + , majorProtocolVersion :: MajorProtocolVersion |
| 25 | + } |
| 26 | + deriving (Show) |
| 27 | + |
| 28 | +options :: O.Parser Options |
| 29 | +options = do |
| 30 | + scriptContextHex <- |
| 31 | + O.strOption |
| 32 | + ( mconcat |
| 33 | + [ O.long "script-context-hex" |
| 34 | + , O.metavar "SCRIPT_CONTEXT_HEX" |
| 35 | + , O.help "Hex-encoded script context" |
| 36 | + ] |
| 37 | + ) |
| 38 | + datumHex <- |
| 39 | + O.strOption |
| 40 | + ( mconcat |
| 41 | + [ O.long "datum-hex" |
| 42 | + , O.metavar "DATUM_HEX" |
| 43 | + , O.help "Hex-encoded datum" |
| 44 | + ] |
| 45 | + ) |
| 46 | + redeemerHex <- |
| 47 | + O.strOption |
| 48 | + ( mconcat |
| 49 | + [ O.long "redeemer-hex" |
| 50 | + , O.metavar "REDEEMER_HEX" |
| 51 | + , O.help "Hex-encoded redeemer" |
| 52 | + ] |
| 53 | + ) |
| 54 | + scriptHex <- |
| 55 | + O.strOption |
| 56 | + ( mconcat |
| 57 | + [ O.long "script-hex" |
| 58 | + , O.metavar "SCRIPT_HEX" |
| 59 | + , O.help "Hex-encoded script" |
| 60 | + ] |
| 61 | + ) |
| 62 | + costModel <- |
| 63 | + O.strOption |
| 64 | + ( mconcat |
| 65 | + [ O.short 'c' |
| 66 | + , O.long "cost-model" |
| 67 | + , O.metavar "COST_MODEL" |
| 68 | + , O.help "Cost model parameter values \"[1, 2, 3]\"" |
| 69 | + ] |
| 70 | + ) |
| 71 | + plutusLedgerLanguage <- |
| 72 | + O.option |
| 73 | + (O.eitherReader readPlutusLedgerLanguage) |
| 74 | + ( mconcat |
| 75 | + [ O.short 'l' |
| 76 | + , O.long "plutus-ledger-language" |
| 77 | + , O.metavar "PLUTUS_LEDGER_LANGUAGE" |
| 78 | + , O.help "Plutus ledger language" |
| 79 | + ] |
| 80 | + ) |
| 81 | + majorProtocolVersion <- |
| 82 | + O.option |
| 83 | + (O.eitherReader readMajorProtocolVersion) |
| 84 | + ( mconcat |
| 85 | + [ O.short 'p' |
| 86 | + , O.long "major-protocol-version" |
| 87 | + , O.metavar "MAJOR_PROTOCOL_VERSION" |
| 88 | + , O.help "Major protocol version" |
| 89 | + ] |
| 90 | + ) |
| 91 | + pure Options{..} |
| 92 | + |
| 93 | +readMajorProtocolVersion :: String -> Either String MajorProtocolVersion |
| 94 | +readMajorProtocolVersion s = |
| 95 | + case MajorProtocolVersion <$> readMaybe @Int s of |
| 96 | + Just mpv | mpv `elem` knownPVs -> Right mpv |
| 97 | + _ -> Left "Invalid Major Protocol Version, must be a number" |
| 98 | + |
| 99 | +readPlutusLedgerLanguage :: String -> Either String PlutusLedgerLanguage |
| 100 | +readPlutusLedgerLanguage s = |
| 101 | + case readMaybe @Int s of |
| 102 | + Just 1 -> Right PlutusV1 |
| 103 | + Just 2 -> Right PlutusV2 |
| 104 | + Just 3 -> Right PlutusV3 |
| 105 | + _ -> Left "Invalid Plutus Ledger Language, must be a number {1, 2, 3}" |
| 106 | + |
| 107 | +parserInfo :: O.ParserInfo Options |
| 108 | +parserInfo = O.info (options O.<**> O.helper) mempty |
0 commit comments