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: Revert buggy change to grep command #25

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 9 additions & 10 deletions script/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,10 @@ func match(s *State, args []string, text, name string) error {
isGrep := name == "grep"

wantArgs := 1
if !isGrep && len(args) != wantArgs {
if isGrep {
wantArgs = 2
}
if len(args) != wantArgs {
return ErrUsage
}

Expand All @@ -672,16 +675,12 @@ func match(s *State, args []string, text, name string) error {
}

if isGrep {
if len(args) == 1 || args[1] == "-" {
text = s.stdout
} else {
name = args[1] // for error messages
data, err := os.ReadFile(s.Path(args[1]))
if err != nil {
return err
}
text = string(data)
name = args[1] // for error messages
data, err := os.ReadFile(s.Path(args[1]))
if err != nil {
return err
}
text = string(data)
}

if n > 0 {
Expand Down
10 changes: 9 additions & 1 deletion script/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,15 @@ func (e *Engine) ListCmds(w io.Writer, verbose bool, names ...string) error {
}

for _, name := range names {
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
6 changes: 5 additions & 1 deletion script/scripttest/testdata/basic.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#! -foo=bar -baz=quux

help
help cat
help unknown-command

# Verify the shebang args parsing
args
grep '^-foo=bar:-baz=quux$'
stdout '^-foo=bar:-baz=quux$'

cat hello.txt
stdout 'hello world'
Expand Down
Loading