Skip to content

Commit

Permalink
Show help for root command with -h, --help, help
Browse files Browse the repository at this point in the history
Add same for version
  • Loading branch information
ad-si committed Feb 8, 2024
1 parent 27b3353 commit 3d6fda8
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 10 deletions.
16 changes: 15 additions & 1 deletion src/CliSpec.purs
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,31 @@ callCommand (CliSpec cliSpec) usageString args executor = do
setExitCode 1
pure (Error "No arguments provided")

Just _mainCmd -> do
Just firstArg | firstArg == FlagShort 'h'
|| firstArg == FlagLong "help"
|| firstArg == CmdArg "help" -> do
log usageString
pure $ Ok unit

Just firstArg | firstArg == FlagShort 'v'
|| firstArg == FlagLong "version"
|| firstArg == CmdArg "version" -> do
log usageString
pure $ Ok unit

Just _mainCmd ->
case args # drop 1 # head of
Just arg | arg == (CmdArg "help")
|| arg == (FlagLong "help")
|| arg == (FlagShort 'h') -> do
-- TODO: Only show help for subcommand
log usageString
pure $ Ok unit

Just arg | arg == (CmdArg "version")
|| arg == (FlagLong "version")
|| arg == (FlagShort 'v') -> do
-- TODO: Only show version of subcommand (if available)
log (cliSpec.version # fromMaybe "0")
pure $ Ok unit

Expand Down
13 changes: 10 additions & 3 deletions src/CliSpec/Parser.purs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,17 @@ tokensToCliArguments cliSpec@(CliSpec cliSpecRaw) tokens = do
"Something went wrong. "
<> "The first token should be a command or a value."


-- If the first token after the main command is a subcommand
-- parse recursively the rest of the tokens.
case tokens # drop 1 # head of
Just (FlagShortToken 'h') -> Ok [FlagShort 'h']
Just (FlagLongToken "help") -> Ok [FlagLong "help"]
Just (TextToken "help") -> Ok [CmdArg "help"]

Just (FlagShortToken 'v') -> Ok [FlagShort 'v']
Just (FlagLongToken "version") -> Ok [FlagLong "version"]
Just (TextToken "version") -> Ok [CmdArg "version"]

-- | If first token after the main command is a subcommand
-- | recursively parse the rest of the tokens.
Just (TextToken name) ->
case findSubCmd cliSpecRaw.commands name of
-- Is subcommand
Expand Down
95 changes: 89 additions & 6 deletions test/CliSpec.purs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,93 @@ tests = do
]

describe "Execution" do
describe "Help" do
let
cliSpec = CliSpec emptyCliSpecRaw
usageString = "Irrelevant"
executor cmdName usageStr providedArgs = do
cmdName `shouldEqual` "help"
usageStr `shouldEqual` usageString
providedArgs `shouldEqual` []
pure $ Ok unit

it "shows help output for -h" do
let
toolArgs = ["git", "-h"]
tokens = tokenizeCliArguments toolArgs

case tokensToCliArguments cliSpec tokens of
Error err -> fail err
Ok cliArgs ->
liftEffect (callCommand cliSpec usageString cliArgs executor)
`shouldReturn` (Ok unit)

it "shows help output for --help" do
let
toolArgs = ["git", "--help"]
tokens = tokenizeCliArguments toolArgs

case tokensToCliArguments cliSpec tokens of
Error err -> fail err
Ok cliArgs ->
liftEffect (callCommand cliSpec usageString cliArgs executor)
`shouldReturn` (Ok unit)

it "shows help output for `help`" do
let
toolArgs = ["git", "help"]
tokens = tokenizeCliArguments toolArgs

case tokensToCliArguments cliSpec tokens of
Error err -> fail err
Ok cliArgs ->
liftEffect (callCommand cliSpec usageString cliArgs executor)
`shouldReturn` (Ok unit)

describe "Version" do
let
cliSpec = CliSpec emptyCliSpecRaw
usageString = "Irrelevant"
executor cmdName usageStr providedArgs = do
cmdName `shouldEqual` "help"
usageStr `shouldEqual` usageString
providedArgs `shouldEqual` []
pure $ Ok unit

it "shows help output for -v" do
let
toolArgs = ["git", "-v"]
tokens = tokenizeCliArguments toolArgs

case tokensToCliArguments cliSpec tokens of
Error err -> fail err
Ok cliArgs ->
liftEffect (callCommand cliSpec usageString cliArgs executor)
`shouldReturn` (Ok unit)

it "shows help output for --version" do
let
toolArgs = ["git", "--version"]
tokens = tokenizeCliArguments toolArgs

case tokensToCliArguments cliSpec tokens of
Error err -> fail err
Ok cliArgs ->
liftEffect (callCommand cliSpec usageString cliArgs executor)
`shouldReturn` (Ok unit)

it "shows help output for `help`" do
let
toolArgs = ["git", "help"]
tokens = tokenizeCliArguments toolArgs

case tokensToCliArguments cliSpec tokens of
Error err -> fail err
Ok cliArgs ->
liftEffect (callCommand cliSpec usageString cliArgs executor)
`shouldReturn` (Ok unit)


it "executes a sub-command with one argument" do
let
cliSpec = CliSpec (emptyCliSpecRaw
Expand Down Expand Up @@ -378,12 +465,8 @@ tests = do
case (tokensToCliArguments cliSpec $ tokenizeCliArguments args) of
Error err -> fail err
Ok cliArgs ->
liftEffect (callCommand
cliSpec
usageString
cliArgs
executor
) `shouldReturn` (Ok unit)
liftEffect (callCommand cliSpec usageString cliArgs executor)
`shouldReturn` (Ok unit)

it "executes a sub-command with one option" do
let
Expand Down

0 comments on commit 3d6fda8

Please sign in to comment.