Skip to content

Commit

Permalink
script: Fix panic when help called on unregistered cmd
Browse files Browse the repository at this point in the history
'help aoeu' panic due to not checking whether the cmd
was found before calling Usage()

Signed-off-by: Jussi Maki <[email protected]>
  • Loading branch information
joamaki committed Dec 5, 2024
1 parent fa6a582 commit d02f07f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
10 changes: 9 additions & 1 deletion script/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,15 @@ func (e *Engine) ListCmds(w io.Writer, verbose bool, regexMatch string) error {
if re != nil && !re.MatchString(name) {
continue
}
cmd := e.Cmds[name]
cmd, ok := e.Cmds[name]
if !ok {
_, err := fmt.Fprintf(w, "command %q is not registered\n", name)
if err != nil {
return err
}
return nil
}

usage := cmd.Usage()

suffix := ""
Expand Down
14 changes: 9 additions & 5 deletions script/scripttest/testdata/basic.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#! -foo=bar -baz=quux

help
help cat
help unknown-command

# Verify the shebang args parsing
args
stdout '^-foo=bar:-baz=quux$'
Expand All @@ -21,14 +25,14 @@ wait
# Test help in various ways
help
help wait
grep wait
stdout wait
help -v wait
grep wait
stdout wait
help unknowncommand
help ^e
! grep wait
grep ^exec
grep ^exists
! stdout wait
stdout ^exec
stdout ^exists

-- hello.txt --
hello world

0 comments on commit d02f07f

Please sign in to comment.