Skip to content

Commit

Permalink
Merge pull request #894 from IntersectMBO/smelc/adapt-to-api-removal-…
Browse files Browse the repository at this point in the history
…of-optparse

Move parsing helpers from API
  • Loading branch information
smelc authored Sep 17, 2024
2 parents 294c3b9 + fcec670 commit ca49409
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Byron/Parsers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module Cardano.CLI.Byron.Parsers
)
where

import Cardano.Api hiding (GenesisParameters, UpdateProposal)
import Cardano.Api hiding (GenesisParameters, UpdateProposal, parseFilePath)
import Cardano.Api.Byron (Address (..), ByronProtocolParametersUpdate (..),
toByronLovelace)
import qualified Cardano.Api.Ledger as L
Expand Down
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
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/EraBased/Options/Genesis.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Cardano.CLI.EraBased.Options.Genesis
)
where

import Cardano.Api hiding (QueryInShelleyBasedEra (..))
import Cardano.Api hiding (QueryInShelleyBasedEra (..), parseFilePath)
import Cardano.Api.Ledger (Coin (..))

import Cardano.Chain.Common (BlockCount (BlockCount))
Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/EraBased/Options/Key.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Cardano.CLI.EraBased.Options.Key
)
where

import Cardano.Api hiding (QueryInShelleyBasedEra (..))
import Cardano.Api hiding (QueryInShelleyBasedEra (..), parseFilePath)

import Cardano.CLI.EraBased.Commands.Key
import Cardano.CLI.EraBased.Options.Common
Expand Down
4 changes: 2 additions & 2 deletions cardano-cli/src/Cardano/CLI/Legacy/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ module Cardano.CLI.Legacy.Options
)
where

import Cardano.Api hiding (QueryInShelleyBasedEra (..))
import Cardano.Api hiding (QueryInShelleyBasedEra (..), parseFilePath)
import Cardano.Api.Ledger (Coin (..))
import Cardano.Api.Shelley hiding (QueryInShelleyBasedEra (..))
import Cardano.Api.Shelley hiding (QueryInShelleyBasedEra (..), parseFilePath)

import Cardano.Chain.Common (BlockCount (BlockCount))
import Cardano.CLI.Environment
Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Legacy/Options/Key.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Cardano.CLI.Legacy.Options.Key
)
where

import Cardano.Api hiding (QueryInShelleyBasedEra (..))
import Cardano.Api hiding (QueryInShelleyBasedEra (..), parseFilePath)

import Cardano.CLI.EraBased.Options.Common
import Cardano.CLI.Legacy.Commands.Key
Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Options/Debug.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Cardano.CLI.Options.Debug
)
where

import Cardano.Api.Shelley hiding (QueryInShelleyBasedEra (..))
import Cardano.Api.Shelley hiding (QueryInShelleyBasedEra (..), parseFilePath)

import Cardano.CLI.Commands.Debug
import Cardano.CLI.Commands.Debug.LogEpochState
Expand Down

0 comments on commit ca49409

Please sign in to comment.