mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-16 13:08:25 +00:00
Cleanup previous PR
Strip the optional leading = from values attached to short options, matching rg semantics (-e=foo means the pattern foo). Replace the rg execution based test with unit tests of the argument rewriting.
This commit is contained in:
parent
040347ba83
commit
fcf697a5e0
2 changed files with 22 additions and 80 deletions
|
|
@ -233,19 +233,22 @@ func parse_args(args ...string) (delegate_to_rg bool, sanitized_args []string, k
|
|||
}
|
||||
}
|
||||
} else if x != "-" && strings.HasPrefix(x, "-") {
|
||||
// rg semantics: everything after the first short option that
|
||||
// takes a value is that value, with an optional leading = stripped
|
||||
ok := true
|
||||
chars := make([]string, 0, len(x)-1)
|
||||
option_arg := ""
|
||||
for i, ch := range x[1:] {
|
||||
option_arg, has_option_arg := "", false
|
||||
for j, ch := range x[1:] {
|
||||
chars = append(chars, string(ch))
|
||||
target, found := alias_map[string(ch)]
|
||||
ok = found
|
||||
if !ok {
|
||||
var target string
|
||||
if target, ok = alias_map[string(ch)]; !ok {
|
||||
sanitized_args = append(sanitized_args, x)
|
||||
break
|
||||
}
|
||||
if options_that_expect_args[target] {
|
||||
option_arg = x[i+2:]
|
||||
if rest := x[j+2:]; rest != "" {
|
||||
option_arg, has_option_arg = strings.TrimPrefix(rest, "="), true
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
|
@ -253,7 +256,7 @@ func parse_args(args ...string) (delegate_to_rg bool, sanitized_args []string, k
|
|||
for _, ch := range chars {
|
||||
target := alias_map[ch]
|
||||
if options_that_expect_args[target] {
|
||||
if option_arg == "" {
|
||||
if !has_option_arg {
|
||||
expecting_option_arg = target
|
||||
} else if err = handle_option_arg(target, option_arg, false); err != nil {
|
||||
return
|
||||
|
|
|
|||
|
|
@ -3,12 +3,7 @@
|
|||
package hyperlinked_grep
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
|
|
@ -94,72 +89,16 @@ func TestRgArgParsing(t *testing.T) {
|
|||
check_args("-m 10 abcd", "--max-count 10 abcd")
|
||||
check_args("-nm 10 abcd", "-n --max-count 10 abcd")
|
||||
check_args("-mn 10 abcd", "--max-count n 10 abcd")
|
||||
|
||||
}
|
||||
|
||||
func TestRgArgumentSemantics(t *testing.T) {
|
||||
if RgExe() == "rg" {
|
||||
t.Skip("Skipping as rg not found in PATH")
|
||||
}
|
||||
dir := t.TempDir()
|
||||
for name, data := range map[string]string{"first.txt": "foo\nfoo.bar\n-f\n--help\nπ\n", "second.txt": "bar\n"} {
|
||||
if err := os.WriteFile(filepath.Join(dir, name), []byte(data), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
run := func(t *testing.T, args []string, input string) (int, string) {
|
||||
t.Helper()
|
||||
common := []string{"--no-config", "--no-heading", "--no-line-number", "--color=never", "--with-filename", "--sort=path"}
|
||||
cmd := exec.Command(RgExe(), append(common, args...)...)
|
||||
cmd.Dir = dir
|
||||
cmd.Stdin = strings.NewReader(input)
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
var exit_err *exec.ExitError
|
||||
if !errors.As(err, &exit_err) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return exit_err.ExitCode(), string(output)
|
||||
}
|
||||
return 0, string(output)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name, input string
|
||||
args []string
|
||||
exit_code int
|
||||
}{
|
||||
{name: "separate_value", args: []string{"-e", "foo", "first.txt", "second.txt"}},
|
||||
{name: "attached_value", args: []string{"-efoo", "first.txt", "second.txt"}},
|
||||
{name: "grouped_attached_value", args: []string{"-nefoo", "first.txt", "second.txt"}},
|
||||
{name: "punctuation_in_value", args: []string{"-efoo.bar", "first.txt", "second.txt"}},
|
||||
{name: "hyphen_in_value", args: []string{"-e-f", "first.txt", "second.txt"}},
|
||||
{name: "option_like_value", args: []string{"-e--help", "first.txt", "second.txt"}},
|
||||
{name: "flag_letters_in_value", args: []string{"-ehelp", "first.txt", "second.txt"}},
|
||||
{name: "unicode_value", args: []string{"-eπ", "first.txt", "second.txt"}},
|
||||
{name: "attached_glob", args: []string{"-g*.txt", "foo", "."}},
|
||||
{name: "attached_literal_glob", args: []string{"-gfirst.txt", "foo", "."}},
|
||||
{name: "multiple_patterns", args: []string{"-efoo", "-ebar", "first.txt", "second.txt"}},
|
||||
{name: "boolean_group", args: []string{"-nH", "foo", "first.txt", "second.txt"}},
|
||||
{name: "stdin_with_file", args: []string{"foo", "-", "second.txt"}, input: "foo from stdin\n"},
|
||||
{name: "stdin_only", args: []string{"foo", "-"}, input: "foo from stdin\n"},
|
||||
{name: "stdin_after_terminator", args: []string{"foo", "--", "-", "second.txt"}, input: "foo from stdin\n"},
|
||||
{name: "pattern_after_terminator", args: []string{"--", "-f", "first.txt", "second.txt"}},
|
||||
{name: "invalid_attached_count", args: []string{"-mn", "10", "first.txt", "second.txt"}, exit_code: 2},
|
||||
{name: "unknown_option", args: []string{"-n?", "foo", "first.txt", "second.txt"}, exit_code: 2},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
_, args, _, err := parse_args(tc.args...)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want_code, want_output := run(t, tc.args, tc.input)
|
||||
if want_code != tc.exit_code {
|
||||
t.Fatalf("Unexpected rg exit code for %q: %d != %d", tc.args, want_code, tc.exit_code)
|
||||
}
|
||||
code, output := run(t, args, tc.input)
|
||||
if code != want_code || output != want_output {
|
||||
t.Fatalf("Changed rg results for %q (parsed as %q): exit code %d != %d, output %q != %q", tc.args, args, code, want_code, output, want_output)
|
||||
}
|
||||
})
|
||||
}
|
||||
check_args("-efoo a", "--regexp foo a")
|
||||
check_args("-nefoo a", "-n --regexp foo a")
|
||||
check_args("-e-f a", "--regexp -f a")
|
||||
check_args("-eπ a", "--regexp π a")
|
||||
check_args("-e=foo a", "--regexp foo a")
|
||||
check_args("-e==foo a", "--regexp =foo a")
|
||||
check_args("-e= a", "--regexp '' a")
|
||||
check_args("'-g*.txt' foo .", "--glob '*.txt' foo .")
|
||||
check_args("foo - a", "foo - a")
|
||||
check_args("-e - a", "--regexp - a")
|
||||
check_args("'-n?' foo", "'-n?' foo")
|
||||
check_args("foo -- -n -", "foo -- -n -")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue