Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

script: Fix crash when help called with unknown command #28

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions script/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,11 +716,13 @@ func Help() Cmd {
return Command(
CmdUsage{
Summary: "log help text for commands and conditions",
Args: "[-v] name...",
Args: "[-v] (regexp)",
Detail: []string{
"To display help for a specific condition, enclose it in brackets: 'help [amd64]'.",
"To display complete documentation when listing all commands, pass the -v flag.",
"Commands can be filtered with a regexp: 'help ^db'",
},
RegexpArgs: firstNonFlag,
},
func(s *State, args ...string) (WaitFunc, error) {
if s.engine == nil {
Expand Down Expand Up @@ -757,7 +759,7 @@ func Help() Cmd {
if len(args) == 0 {
out.WriteString("\ncommands:\n\n")
}
s.engine.ListCmds(out, verbose, cmds...)
s.engine.ListCmds(out, verbose, strings.Join(cmds, " "))
}

wait := func(*State) (stdout, stderr string, err error) {
Expand Down
26 changes: 16 additions & 10 deletions script/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import (
"errors"
"fmt"
"io"
"regexp"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -752,20 +753,25 @@ func checkStatus(cmd *command, err error) error {
// ListCmds prints to w a list of the named commands,
// annotating each with its arguments and a short usage summary.
// If verbose is true, ListCmds prints full details for each command.
//
// Each of the name arguments should be a command name.
// If no names are passed as arguments, ListCmds lists all the
// commands registered in e.
func (e *Engine) ListCmds(w io.Writer, verbose bool, names ...string) error {
if names == nil {
names = make([]string, 0, len(e.Cmds))
for name := range e.Cmds {
names = append(names, name)
func (e *Engine) ListCmds(w io.Writer, verbose bool, regexMatch string) error {
var re *regexp.Regexp
if regexMatch != "" {
var err error
re, err = regexp.Compile(regexMatch)
if err != nil {
return err
}
sort.Strings(names)
}
names := make([]string, 0, len(e.Cmds))
for name := range e.Cmds {
names = append(names, name)
}
sort.Strings(names)

for _, name := range names {
if re != nil && !re.MatchString(name) {
continue
}
cmd := e.Cmds[name]
usage := cmd.Usage()

Expand Down
12 changes: 12 additions & 0 deletions script/scripttest/testdata/basic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,17 @@ echo hello

wait

# Test help in various ways
help
help wait
grep wait
help -v wait
grep wait
help unknowncommand
help ^e
! grep wait
grep ^exec
grep ^exists

-- hello.txt --
hello world
Loading