Skip to content

Commit

Permalink
Move parsing helpers from API
Browse files Browse the repository at this point in the history
  • Loading branch information
smelc committed Sep 12, 2024
1 parent 39af17e commit f660fc0
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

module Cardano.CLI.EraBased.Options.Common where

import Cardano.Api
import Cardano.Api hiding (bounded, parseFilePath)
import qualified Cardano.Api.Ledger as L
import Cardano.Api.Shelley
import Cardano.Api.Shelley hiding (bounded, parseFilePath)

import Cardano.CLI.Environment (EnvCli (..), envCliAnyShelleyBasedEra,
envCliAnyShelleyToBabbageEra)
Expand All @@ -26,7 +26,7 @@ import Cardano.CLI.Types.Key
import Cardano.CLI.Types.Key.VerificationKey
import qualified Ouroboros.Network.Protocol.LocalStateQuery.Type as Consensus

import Control.Monad (mfilter, void)
import Control.Monad (mfilter, void, when)
import qualified Data.Aeson as Aeson
import Data.Bifunctor
import Data.Bits (Bits, toIntegralSized)
Expand Down Expand Up @@ -55,6 +55,7 @@ import qualified Text.Parsec.Error as Parsec
import qualified Text.Parsec.Language as Parsec
import qualified Text.Parsec.String as Parsec
import qualified Text.Parsec.Token as Parsec
import qualified Text.Read as Read
import Text.Read (readEither, readMaybe)

command' :: String -> String -> Parser a -> Mod CommandFields a
Expand All @@ -72,6 +73,22 @@ prefixFlag prefix longFlag =
Nothing -> longFlag
Just prefix' -> prefix' <> "-" <> longFlag

bounded :: forall a. (Bounded a, Integral a, Show a) => String -> Opt.ReadM a
bounded t = Opt.eitherReader $ \s -> do
i <- Read.readEither @Integer s
when (i < fromIntegral (minBound @a)) $ Left $ t <> " must not be less than " <> show (minBound @a)
when (i > fromIntegral (maxBound @a)) $ Left $ t <> " must not greater than " <> show (maxBound @a)
pure (fromIntegral i)

parseFilePath :: String -> String -> Opt.Parser FilePath
parseFilePath optname desc =
Opt.strOption
( Opt.long optname
<> Opt.metavar "FILEPATH"
<> Opt.help desc
<> Opt.completer (Opt.bashCompleter "file")
)

pNetworkIdDeprecated :: Parser NetworkId
pNetworkIdDeprecated =
asum
Expand Down

0 comments on commit f660fc0

Please sign in to comment.