GJL commented on a change in pull request #6737: [FLINK-10399] Refactor
ParameterTool#fromArgs
URL: https://github.com/apache/flink/pull/6737#discussion_r223394447
##########
File path:
flink-java/src/main/java/org/apache/flink/api/java/utils/ParameterTool.java
##########
@@ -68,81 +68,46 @@
* @return A {@link ParameterTool}
*/
public static ParameterTool fromArgs(String[] args) {
- Map<String, String> map = new HashMap<String,
String>(args.length / 2);
-
- String key = null;
- String value = null;
- boolean expectValue = false;
- for (String arg : args) {
- // check for -- argument
- if (arg.startsWith("--")) {
- if (expectValue) {
- // we got into a new key, even though
we were a value --> current key is one without value
- if (value != null) {
- throw new
IllegalStateException("Unexpected state");
- }
- map.put(key, NO_VALUE_KEY);
- // key will be overwritten in the next
step
- }
- key = arg.substring(2);
- expectValue = true;
- } // check for - argument
- else if (arg.startsWith("-")) {
- // we are waiting for a value, so this is a -
prefixed value (negative number)
- if (expectValue) {
-
- if (NumberUtils.isNumber(arg)) {
- // negative number
- value = arg;
- expectValue = false;
- } else {
- if (value != null) {
- throw new
IllegalStateException("Unexpected state");
- }
- // We waited for a value but
found a new key. So the previous key doesnt have a value.
- map.put(key, NO_VALUE_KEY);
- key = arg.substring(1);
- expectValue = true;
- }
- } else {
- // we are not waiting for a value, so
its an argument
- key = arg.substring(1);
- expectValue = true;
- }
+ final Map<String, String> map = new HashMap<>(args.length / 2);
+
+ final String errorMessage = "Error parsing arguments '%s' on
'%s'.";
+
+ int i = 0;
+ while (i < args.length) {
+ final String key;
Review comment:
Optional:
The function could benefit from _extract method_ refactoring:
```
final String key = getNextKey(args, i);
```
```
private static String getNextKey(final String[] args, final int index) {
final String arg = args[index];
final String key;
if (arg.startsWith("--")) {
key = arg.substring(2);
} else if (arg.startsWith("-")) {
key = arg.substring(1);
} else {
throw new IllegalArgumentException(
String.format("Error parsing arguments '%s' on
'%s'." + " Please prefix keys with -- or -.",
Arrays.toString(args), arg));
}
if (key.length() == 0) {
throw new IllegalArgumentException(
String.format("The input %s contains an empty
argument",
Arrays.toString(args)));
}
return key;
}
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services