Skip to content

Commit 5f9c5cc

Browse files
authored
Remove extra newline when message contains trailing newlines. (#387)
1 parent e864bb0 commit 5f9c5cc

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

Diff for: kong.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ func (k *Kong) applyHookToDefaultFlags(ctx *Context, node *Node, name string) er
373373
}
374374

375375
func formatMultilineMessage(w io.Writer, leaders []string, format string, args ...interface{}) {
376-
lines := strings.Split(fmt.Sprintf(format, args...), "\n")
376+
lines := strings.Split(strings.TrimRight(fmt.Sprintf(format, args...), "\n"), "\n")
377377
leader := ""
378378
for _, l := range leaders {
379379
if l == "" {

Diff for: kong_test.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -593,11 +593,24 @@ func TestSliceWithDisabledSeparator(t *testing.T) {
593593
}
594594

595595
func TestMultilineMessage(t *testing.T) {
596-
w := &bytes.Buffer{}
597-
var cli struct{}
598-
p := mustNew(t, &cli, kong.Writers(w, w))
599-
p.Printf("hello\nworld")
600-
assert.Equal(t, "test: hello\n world\n", w.String())
596+
tests := []struct {
597+
name string
598+
text string
599+
want string
600+
}{
601+
{"Simple", "hello\nworld", "test: hello\n world\n"},
602+
{"WithNewline", "hello\nworld\n", "test: hello\n world\n"},
603+
}
604+
for _, test := range tests {
605+
test := test
606+
t.Run(test.name, func(t *testing.T) {
607+
w := &bytes.Buffer{}
608+
var cli struct{}
609+
p := mustNew(t, &cli, kong.Writers(w, w))
610+
p.Printf(test.text)
611+
assert.Equal(t, test.want, w.String())
612+
})
613+
}
601614
}
602615

603616
type cmdWithRun struct {

0 commit comments

Comments
 (0)