Revisit posix short handling in Lexer

- Posix short style is expected to have only single
  dash and letters so use this style to come up better
  separation between options and arguments.
- Fixes #1077
This commit is contained in:
Janne Valkealahti
2024-05-28 10:07:38 +01:00
parent c9f2c6fb27
commit 2eab1a6072
3 changed files with 83 additions and 5 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2023 the original author or authors.
* Copyright 2023-2024 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.
@@ -201,12 +201,17 @@ public interface Lexer {
}
}
else if (isLastTokenOfType(tokenList, TokenType.OPTION)) {
if (argument.startsWith("-")) {
// posix short style can only have one or more letters
int decuceArgumentStyle = decuceArgumentStyle(argument);
if (decuceArgumentStyle > 0) {
tokenList.add(Token.of(argument, TokenType.OPTION, i2));
}
else {
else if (decuceArgumentStyle < 0) {
tokenList.add(Token.of(argument, TokenType.ARGUMENT, i2));
}
else {
tokenList.add(Token.of(argument, TokenType.OPTION, i2));
}
}
else if (isLastTokenOfType(tokenList, TokenType.COMMAND)) {
if (argument.startsWith("-")) {
@@ -243,5 +248,28 @@ public interface Lexer {
}
return false;
}
private static int decuceArgumentStyle(String str) {
// positive - looks like posix short
// 0 - looks like long option
// negative - looks like argument, not option
if (str.length() < 2) {
return -1;
}
if (str.charAt(0) != '-') {
return -1;
}
if (str.length() > 1 && str.charAt(0) == '-' && str.charAt(1) == '-') {
return 0;
}
int ret = 1;
for (int i = 1; i < str.length(); i++) {
if (!Character.isLetter(str.charAt(i))) {
ret = -1;
break;
}
}
return ret;
}
}
}