Skip to content

Commit

Permalink
do not spit back malformed network tag when given gibberish
Browse files Browse the repository at this point in the history
It's usually not a good idea to spit back to users something that we failed to parse in the first place. Instead, we should print what we would have expected.
  • Loading branch information
KtorZ committed Jul 10, 2020
1 parent d824f16 commit 3df6364
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
20 changes: 18 additions & 2 deletions src/Options/Applicative/Discrimination.hs
Expand Up @@ -17,11 +17,21 @@ import Prelude
import Cardano.Address
( NetworkTag (..) )
import Options.Applicative
( Parser, auto, completer, helpDoc, listCompleter, long, metavar, option )
( Parser
, completer
, eitherReader
, helpDoc
, listCompleter
, long
, metavar
, option
)
import Options.Applicative.Help.Pretty
( string, vsep )
import Options.Applicative.Style
( Style (..) )
import Text.Read
( readMaybe )

import qualified Cardano.Address.Style.Byron as Byron
import qualified Cardano.Address.Style.Jormungandr as Jormungandr
Expand All @@ -33,7 +43,7 @@ import qualified Cardano.Address.Style.Shelley as Shelley

-- | Parse a 'NetworkTag' from the command-line, as an option
networkTagOpt :: Style -> Parser NetworkTag
networkTagOpt style = option (NetworkTag <$> auto) $ mempty
networkTagOpt style = option (eitherReader reader) $ mempty
<> metavar "NETWORK-TAG"
<> long "network-tag"
<> helpDoc (Just (vsep (string <$> doc style)))
Expand Down Expand Up @@ -76,3 +86,9 @@ networkTagOpt style = option (NetworkTag <$> auto) $ mempty
[ unNetworkTag Shelley.shelleyMainnet
, unNetworkTag Shelley.shelleyTestnet
]

reader =
maybe
(Left "Invalid network tag. Must be a integer value.")
(Right . NetworkTag)
. readMaybe
2 changes: 1 addition & 1 deletion test/Command/Address/BootstrapSpec.hs
Expand Up @@ -53,7 +53,7 @@ specInvalidNetwork :: String -> SpecWith ()
specInvalidNetwork networkTag = it ("invalid network " <> networkTag) $ do
(out, err) <- cli [ "address", "bootstrap", "--network-tag", networkTag ] ""
out `shouldBe` ""
err `shouldContain` "--network-tag: cannot parse value"
err `shouldContain` "Invalid network tag. Must be a integer value."
err `shouldContain` "Usage"

defaultPhrase :: [String]
Expand Down
2 changes: 1 addition & 1 deletion test/Command/Address/PaymentSpec.hs
Expand Up @@ -37,7 +37,7 @@ specMalformedNetwork networkTag = it ("malformed network " <> networkTag) $ do
>>= cli [ "key", "public" ]
>>= cli [ "address", "payment", "--network-tag", networkTag ]
out `shouldBe` ""
err `shouldContain` "--network-tag: cannot parse value"
err `shouldContain` "Invalid network tag. Must be a integer value."
err `shouldContain` "Usage"

specInvalidNetwork :: String -> SpecWith ()
Expand Down

0 comments on commit 3df6364

Please sign in to comment.