Skip to content

Commit

Permalink
ExtendedDefaultParser should not add empty arguments
Browse files Browse the repository at this point in the history
- 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.
- Backport #763
- Fixes #764
  • Loading branch information
jvalkeal committed Jun 7, 2023
1 parent 7768d46 commit af7be31
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2021 the original author or authors.
* Copyright 2017-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -126,10 +126,12 @@ else if (!isEscapeChar(line, i)) {
}

if (current.length() > 0 || (line != null && cursor == line.length())) {
words.add(current.toString());
if (current.length() > 0) {
words.add(current.toString());
}
}

if (line != null && cursor == line.length()) {
if (line != null && cursor == line.length() && words.size() > 0) {
wordIndex = words.size() - 1;
wordCursor = words.get(words.size() - 1).length();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.jline;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class ExtendedDefaultParserTests {

@Test
void wordsParsing() {
ExtendedDefaultParser parser = new ExtendedDefaultParser();

assertThat(parser.parse("one", 0).words()).hasSize(1);
assertThat(parser.parse("one", 3).words()).hasSize(1);

assertThat(parser.parse("one two", 0).words()).hasSize(2);
assertThat(parser.parse("one two", 7).words()).hasSize(2);

assertThat(parser.parse("'one'", 0).words()).hasSize(1);
assertThat(parser.parse("'one'", 5).words()).hasSize(1);

assertThat(parser.parse("'one' two", 0).words()).hasSize(2);
assertThat(parser.parse("'one' two", 9).words()).hasSize(2);

assertThat(parser.parse("one 'two'", 0).words()).hasSize(2);
assertThat(parser.parse("one 'two'", 9).words()).hasSize(2);

assertThat(parser.parse("\"one\"", 0).words()).hasSize(1);
assertThat(parser.parse("\"one\"", 5).words()).hasSize(1);

assertThat(parser.parse("\"one\" two", 0).words()).hasSize(2);
assertThat(parser.parse("\"one\" two", 9).words()).hasSize(2);

assertThat(parser.parse("one \"two\"", 0).words()).hasSize(2);
assertThat(parser.parse("one \"two\"", 9).words()).hasSize(2);
}
}

0 comments on commit af7be31

Please sign in to comment.