Skip to content

Commit

Permalink
Add ability to skip to next option if parse error
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
mrjameshamilton committed Jan 22, 2024
1 parent c35913c commit 712fd76
Showing 1 changed file with 44 additions and 8 deletions.
52 changes: 44 additions & 8 deletions base/src/main/java/proguard/ConfigurationParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,26 @@
*/
package proguard;

import proguard.classfile.*;
import proguard.classfile.AccessConstants;
import proguard.classfile.ClassConstants;
import proguard.classfile.JavaAccessConstants;
import proguard.classfile.JavaTypeConstants;
import proguard.classfile.TypeConstants;
import proguard.classfile.util.ClassUtil;
import proguard.util.*;

import java.io.*;
import java.net.*;
import java.util.*;
import proguard.util.ListUtil;
import proguard.util.StringUtil;

import java.io.File;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;


/**
Expand Down Expand Up @@ -139,9 +152,24 @@ public ConfigurationParser(WordReader reader,
* @throws IOException if an IO error occurs while reading a configuration.
*/
public void parse(Configuration configuration)
throws ParseException, IOException {
parse(configuration, null);
}

/**
* Parses and returns the configuration.
*
* @param configuration the configuration that is updated as a side-effect.
* @param unknownOptionHandler optional handler for unknown options; if null then a {@link ParseException}
* is thrown when encountering an unknown option.
* @throws ParseException if the any of the configuration settings contains
* a syntax error.
* @throws IOException if an IO error occurs while reading a configuration.
*/
public void parse(Configuration configuration, BiConsumer<String, String> unknownOptionHandler)
throws ParseException, IOException
{
while (nextWord != null)
parseWord: while (nextWord != null)
{
lastComments = reader.lastComments();

Expand Down Expand Up @@ -234,7 +262,15 @@ else if (ConfigurationConstants.REPACKAGE_CLASSES_OPTION
else if (ConfigurationConstants.IDENTIFIER_NAME_STRING .startsWith(nextWord)) parseUnsupportedR8Rules(ConfigurationConstants.IDENTIFIER_NAME_STRING, true);
else
{
throw new ParseException("Unknown option " + reader.locationDescription());
if (unknownOptionHandler != null) {
unknownOptionHandler.accept(nextWord, reader.lineLocationDescription());
while (nextWord != null) {
readNextWord();
if (nextWord != null && nextWord.startsWith("-")) {
continue parseWord;
}
}
} else throw new ParseException("Unknown option " + reader.locationDescription());
}
}
}
Expand Down

0 comments on commit 712fd76

Please sign in to comment.