Skip to content

Commit 712fd76

Browse files
Add ability to skip to next option if parse error
Allows providing a function to handle the case of an unknown option. ``` public static void main(String[] args) { try { try (ConfigurationParser parser = new ConfigurationParser(new String[]{"-keep class * {}", "-unknownoption", "-whatisthisoption?"}, System.getProperties())) { parser.parse(new Configuration(), (option, location) -> { System.out.println("Unknown option: " + option + " @ " + location); }); } catch (ParseException ex) { ex.printStackTrace(); } } catch (IOException ex) { ex.printStackTrace(); } } ``` Output: ``` Unknown option: -unknownoption @ argument number 2 Unknown option: -whatisthisoption? @ argument number 3 ```
1 parent c35913c commit 712fd76

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

base/src/main/java/proguard/ConfigurationParser.java

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,26 @@
2020
*/
2121
package proguard;
2222

23-
import proguard.classfile.*;
23+
import proguard.classfile.AccessConstants;
24+
import proguard.classfile.ClassConstants;
25+
import proguard.classfile.JavaAccessConstants;
26+
import proguard.classfile.JavaTypeConstants;
27+
import proguard.classfile.TypeConstants;
2428
import proguard.classfile.util.ClassUtil;
25-
import proguard.util.*;
26-
27-
import java.io.*;
28-
import java.net.*;
29-
import java.util.*;
29+
import proguard.util.ListUtil;
30+
import proguard.util.StringUtil;
31+
32+
import java.io.File;
33+
import java.io.IOException;
34+
import java.io.LineNumberReader;
35+
import java.io.StringReader;
36+
import java.net.MalformedURLException;
37+
import java.net.URL;
38+
import java.util.ArrayList;
39+
import java.util.List;
40+
import java.util.Properties;
41+
import java.util.function.BiConsumer;
42+
import java.util.function.BiFunction;
3043

3144

3245
/**
@@ -139,9 +152,24 @@ public ConfigurationParser(WordReader reader,
139152
* @throws IOException if an IO error occurs while reading a configuration.
140153
*/
141154
public void parse(Configuration configuration)
155+
throws ParseException, IOException {
156+
parse(configuration, null);
157+
}
158+
159+
/**
160+
* Parses and returns the configuration.
161+
*
162+
* @param configuration the configuration that is updated as a side-effect.
163+
* @param unknownOptionHandler optional handler for unknown options; if null then a {@link ParseException}
164+
* is thrown when encountering an unknown option.
165+
* @throws ParseException if the any of the configuration settings contains
166+
* a syntax error.
167+
* @throws IOException if an IO error occurs while reading a configuration.
168+
*/
169+
public void parse(Configuration configuration, BiConsumer<String, String> unknownOptionHandler)
142170
throws ParseException, IOException
143171
{
144-
while (nextWord != null)
172+
parseWord: while (nextWord != null)
145173
{
146174
lastComments = reader.lastComments();
147175

@@ -234,7 +262,15 @@ else if (ConfigurationConstants.REPACKAGE_CLASSES_OPTION
234262
else if (ConfigurationConstants.IDENTIFIER_NAME_STRING .startsWith(nextWord)) parseUnsupportedR8Rules(ConfigurationConstants.IDENTIFIER_NAME_STRING, true);
235263
else
236264
{
237-
throw new ParseException("Unknown option " + reader.locationDescription());
265+
if (unknownOptionHandler != null) {
266+
unknownOptionHandler.accept(nextWord, reader.lineLocationDescription());
267+
while (nextWord != null) {
268+
readNextWord();
269+
if (nextWord != null && nextWord.startsWith("-")) {
270+
continue parseWord;
271+
}
272+
}
273+
} else throw new ParseException("Unknown option " + reader.locationDescription());
238274
}
239275
}
240276
}

0 commit comments

Comments
 (0)