Skip to content
Permalink
Browse files

fix exit code when using --help in CLIs --> use 0 instead of 1

  • Loading branch information...
KtorZ committed May 16, 2019
1 parent 728db74 commit 04eb7fc1b746859edf3519b404da2851b65a7be9
@@ -6,7 +6,7 @@ module Main where
import Prelude

import Cardano.CLI
( Port, parseArgWith )
( Port, help, parseArgWith )
import Cardano.Environment.HttpBridge
( Network, network )
import Cardano.Launcher
@@ -31,7 +31,6 @@ import System.Console.Docopt
, Docopt
, Option
, docopt
, exitWithUsage
, isPresent
, longOption
, parseArgsOrExit
@@ -68,7 +67,7 @@ Options:
main :: IO ()
main = do
args <- parseArgsOrExit cli =<< getArgs
when (args `isPresent` (longOption "help")) $ exitWithUsage cli
when (args `isPresent` (longOption "help")) $ help cli

bridgePort <- args `parseArg` longOption "http-bridge-port"
walletPort <- args `parseArg` longOption "wallet-server-port"
@@ -28,6 +28,7 @@ import Cardano.CLI
( Port (..)
, getLine
, getSensitiveLine
, help
, parseAllArgsWith
, parseArgWith
, putErrLn
@@ -167,8 +168,7 @@ main = do

exec :: Manager -> Arguments -> IO ()
exec manager args
| args `isPresent` (longOption "help") = do
exitWithUsage cli
| args `isPresent` (longOption "help") = help cli

| args `isPresent` command "server" = do
walletPort <- args `parseArg` longOption "port"
@@ -26,6 +26,7 @@ module Cardano.CLI
-- * Parsing Arguments
, parseArgWith
, parseAllArgsWith
, help

-- * Working with Sensitive Data
, getLine
@@ -67,9 +68,10 @@ import System.Console.Docopt
, exitWithUsageMessage
, getAllArgs
, getArgOrExitWith
, usage
)
import System.Exit
( exitFailure )
( exitFailure, exitSuccess )
import System.IO
( BufferMode (..)
, Handle
@@ -140,6 +142,13 @@ getAllArgsOrExitWith doc args opt =
where
err = exitWithUsageMessage doc $ "argument expected for: " ++ show opt

-- | Like 'exitWithUsage', but with a success exit code
help :: Docopt -> IO ()
help cli = do
TIO.putStrLn $ T.pack $ usage cli
exitSuccess


{-------------------------------------------------------------------------------
Unicode Terminal Helpers
-------------------------------------------------------------------------------}
@@ -17,10 +17,10 @@ import Data.List
( length )
import Data.Text
( Text )
import System.Exit
( ExitCode (..))
import System.Command
( Exit (..), Stdout (..), command )
import System.Exit
( ExitCode (..) )
import Test.Hspec
( SpecWith, describe, it )
import Test.Hspec.Expectations.Lifted
@@ -41,7 +41,6 @@ import qualified Network.HTTP.Types.Status as HTTP

specNoCluster :: SpecWith ()
specNoCluster = do

it "CLI - Shows help on bad argument" $ do
(Exit c, Stdout out) <- command [] "cardano-wallet" ["--bad arg"]
out `shouldContain` "Cardano Wallet CLI"
@@ -50,12 +49,12 @@ specNoCluster = do
it "CLI - Shows help with --help" $ do
(Exit c, Stdout out) <- command [] "cardano-wallet" ["--help"]
out `shouldContain` "Cardano Wallet CLI"
c `shouldBe` ExitFailure 1
c `shouldBe` ExitSuccess

it "CLI - Shows version" $ do
(Exit c, Stdout out) <- command [] "cardano-wallet" ["--version"]
cabal <- readFile "../../cardano-wallet.cabal"
let cabalVersion = ( words $ (lines cabal) !! 1 ) !! 1
let cabalVersion = words ((lines cabal) !! 1 ) !! 1
let returnedVersion = T.unpack $ T.dropEnd 1 (T.pack out)
returnedVersion `shouldBe` cabalVersion
c `shouldBe` ExitSuccess
@@ -74,7 +73,6 @@ specNoCluster = do

specWithCluster :: SpecWith Context
specWithCluster = do

it "CLI - Can get a wallet" $ \ctx -> do
walId <- createWallet ctx "1st CLI Wallet" mnemonics15
(Exit c, Stdout out) <- command [] "cardano-wallet"

0 comments on commit 04eb7fc

Please sign in to comment.
You can’t perform that action at this time.