diff --git a/Makefile b/Makefile index 782fd3b..49e2d58 100644 --- a/Makefile +++ b/Makefile @@ -53,6 +53,14 @@ test: pistol @echo or verbosity, although the arguments are passed to pistol @echo ------------------- @./pistol --config tests/config tests/renovate.json5.bz2 -v -L + @echo ------------------- + @echo Checks substitution of multiple pistol-extra arguments without + @echo a space between them \(issue 56\). The output should be: + @echo + @echo " tests/multi-extra AxB" + @echo + @echo ------------------- + @./pistol --config tests/config tests/multi-extra A B deps: go get github.com/c4milo/github-release diff --git a/previewer.go b/previewer.go index dc5bb1e..5c389a0 100644 --- a/previewer.go +++ b/previewer.go @@ -5,7 +5,7 @@ import ( "os" "io" "os/exec" - // "fmt" + "fmt" "strconv" "strings" "regexp" @@ -163,18 +163,29 @@ func (p *Previewer) Write(w io.Writer) (error) { re := regexp.MustCompile(`%pistol-extra([0-9]+)%`) for _, arg := range p.Args { + argAux := arg if(re.MatchString(arg)) { - auxStr := re.ReplaceAllString(arg, "$1") - auxInt, err := strconv.Atoi(auxStr); - if (err == nil && len(p.Extras) > auxInt) { - arg = re.ReplaceAllString(arg, p.Extras[auxInt]) - } else { - continue + // We iterate all indices of matches in every command line + // argument written in the config, because the match can occur + // in multiple arguments, see #56. + allIndexes := re.FindAllStringSubmatchIndex(arg, -1) + for _, loc := range allIndexes { + // We try to convert the string found in the argument to a + // number. + auxInt, err := strconv.Atoi(arg[loc[2]:loc[3]]) + if (err == nil && len(p.Extras) > auxInt) { + // substitute the %pistol-extra[#]% argument in the + // final CLI string. + current := fmt.Sprintf("%%pistol-extra%d%%", auxInt) + argAux = strings.ReplaceAll(argAux, current, p.Extras[auxInt]) + } else { + continue + } } } else { - arg = strings.ReplaceAll(arg, "%pistol-filename%", replStr) + argAux = strings.ReplaceAll(argAux, "%pistol-filename%", replStr) } - argsOut = append(argsOut, arg) + argsOut = append(argsOut, argAux) } if p.Command == "sh:" { diff --git a/tests/config b/tests/config index 877fffd..661e54e 100644 --- a/tests/config +++ b/tests/config @@ -14,3 +14,5 @@ fpath .*/tests/renovate.json5.bz2$ bzcat %pistol-filename% application/x-bzip2 not-a-real-command %pistol-filename% # Test exit code is non-zero in case a command starts but fails to finish fpath .*/tests$ bat %pistol-filename% +# Test substitution of multiple extra arguments without spaces between (#56) +fpath .*/tests/multi-extra echo %pistol-filename% %pistol-extra0%x%pistol-extra1% diff --git a/tests/multi-extra b/tests/multi-extra new file mode 100644 index 0000000..e69de29