Skip to content

Commit 06e89dc

Browse files
committed
ExtendedDefaultParser should not add empty arguments
- Fixes issue when last "word" is within quotes and cursor is at the end of a line which caused empty "word" string in an argument list. - This then caused i.e. string option to have a collection as an input(if no arity settings used) and via spring conversions a comma were added. - Fixes #763
1 parent 58e3a5e commit 06e89dc

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

spring-shell-core/src/main/java/org/springframework/shell/jline/ExtendedDefaultParser.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2021 the original author or authors.
2+
* Copyright 2017-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -126,10 +126,12 @@ else if (!isEscapeChar(line, i)) {
126126
}
127127

128128
if (current.length() > 0 || (line != null && cursor == line.length())) {
129-
words.add(current.toString());
129+
if (current.length() > 0) {
130+
words.add(current.toString());
131+
}
130132
}
131133

132-
if (line != null && cursor == line.length()) {
134+
if (line != null && cursor == line.length() && words.size() > 0) {
133135
wordIndex = words.size() - 1;
134136
wordCursor = words.get(words.size() - 1).length();
135137
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.jline;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
22+
public class ExtendedDefaultParserTests {
23+
24+
@Test
25+
void wordsParsing() {
26+
ExtendedDefaultParser parser = new ExtendedDefaultParser();
27+
28+
assertThat(parser.parse("one", 0).words()).hasSize(1);
29+
assertThat(parser.parse("one", 3).words()).hasSize(1);
30+
31+
assertThat(parser.parse("one two", 0).words()).hasSize(2);
32+
assertThat(parser.parse("one two", 7).words()).hasSize(2);
33+
34+
assertThat(parser.parse("'one'", 0).words()).hasSize(1);
35+
assertThat(parser.parse("'one'", 5).words()).hasSize(1);
36+
37+
assertThat(parser.parse("'one' two", 0).words()).hasSize(2);
38+
assertThat(parser.parse("'one' two", 9).words()).hasSize(2);
39+
40+
assertThat(parser.parse("one 'two'", 0).words()).hasSize(2);
41+
assertThat(parser.parse("one 'two'", 9).words()).hasSize(2);
42+
43+
assertThat(parser.parse("\"one\"", 0).words()).hasSize(1);
44+
assertThat(parser.parse("\"one\"", 5).words()).hasSize(1);
45+
46+
assertThat(parser.parse("\"one\" two", 0).words()).hasSize(2);
47+
assertThat(parser.parse("\"one\" two", 9).words()).hasSize(2);
48+
49+
assertThat(parser.parse("one \"two\"", 0).words()).hasSize(2);
50+
assertThat(parser.parse("one \"two\"", 9).words()).hasSize(2);
51+
}
52+
}

0 commit comments

Comments
 (0)