TisonKun commented on a change in pull request #6737: [FLINK-10399] Refactor 
ParameterTool#fromArgs
URL: https://github.com/apache/flink/pull/6737#discussion_r223415739
 
 

 ##########
 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;
+
+                       if (args[i].startsWith("--")) {
+                               key = args[i].substring(2);
+                       } else if (args[i].startsWith("-")) {
+                               key = args[i].substring(1);
                        } else {
-                               if (expectValue) {
-                                       value = arg;
-                                       expectValue = false;
-                               } else {
-                                       throw new RuntimeException("Error 
parsing arguments '" + Arrays.toString(args) + "' on '" + arg + "'. Unexpected 
value. Please prefix values with -- or -.");
-                               }
+                               throw new IllegalArgumentException(
+                                       String.format(errorMessage + " Please 
prefix keys with -- or -.",
+                                               Arrays.toString(args), 
args[i]));
                        }
 
-                       if (value == null && key == null) {
-                               throw new IllegalStateException("Value and key 
can not be null at the same time");
-                       }
-                       if (key != null && value == null && !expectValue) {
-                               throw new IllegalStateException("Value expected 
but flag not set");
-                       }
-                       if (key != null && value != null) {
-                               map.put(key, value);
-                               key = null;
-                               value = null;
-                               expectValue = false;
-                       }
-                       if (key != null && key.length() == 0) {
-                               throw new IllegalArgumentException("The input " 
+ Arrays.toString(args) + " contains an empty argument");
+                       if (key.length() == 0) {
+                               throw new IllegalArgumentException(
+                                       String.format(errorMessage + " Keys 
should never be empty",
 
 Review comment:
   Feel free to the revert and thanks for you tests advice, address it.

----------------------------------------------------------------
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

Reply via email to