Skip to content

Commit

Permalink
fix exit code when using --help in CLIs --> use 0 instead of 1
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed May 16, 2019
1 parent 295ac7a commit ffa5212
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
5 changes: 2 additions & 3 deletions exe/launcher/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,7 +31,6 @@ import System.Console.Docopt
, Docopt
, Option
, docopt
, exitWithUsage
, isPresent
, longOption
, parseArgsOrExit
Expand Down Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions exe/wallet/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import Cardano.CLI
( Port (..)
, getLine
, getSensitiveLine
, help
, parseAllArgsWith
, parseArgWith
, putErrLn
Expand Down Expand Up @@ -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"
Expand Down
11 changes: 10 additions & 1 deletion lib/cli/src/Cardano/CLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module Cardano.CLI
-- * Parsing Arguments
, parseArgWith
, parseAllArgsWith
, help

-- * Working with Sensitive Data
, getLine
Expand Down Expand Up @@ -67,9 +68,10 @@ import System.Console.Docopt
, exitWithUsageMessage
, getAllArgs
, getArgOrExitWith
, usage
)
import System.Exit
( exitFailure )
( exitFailure, exitSuccess )
import System.IO
( BufferMode (..)
, Handle
Expand Down Expand Up @@ -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
-------------------------------------------------------------------------------}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -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"
Expand Down

0 comments on commit ffa5212

Please sign in to comment.