Skip to content

Commit

Permalink
script: Implement regexp filtering for help
Browse files Browse the repository at this point in the history
Filter the commands shown by 'help' with a regexp instead
of searching by name. This will become useful when there are
many commands sharing the same prefix, for example StateDB has
11 commands and with this we can dump the help for all of them with
'help ^db'.

Signed-off-by: Jussi Maki <[email protected]>
  • Loading branch information
joamaki committed Dec 3, 2024
1 parent 188149d commit 4ba0771
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
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
28 changes: 15 additions & 13 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,25 +753,26 @@ 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 {
cmd, ok := e.Cmds[name]
if !ok {
fmt.Fprintf(w, "command %q not found\n", name)
if re != nil && !re.MatchString(name) {
continue
}
cmd := e.Cmds[name]
usage := cmd.Usage()

suffix := ""
Expand Down
6 changes: 6 additions & 0 deletions script/scripttest/testdata/basic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ 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

0 comments on commit 4ba0771

Please sign in to comment.