Use JCommander instead of Blaze/Bazel's Options parser in ResourceProcessorBusyBox.java

PiperOrigin-RevId: 651760891
Change-Id: I6d5f18042cd0a54c0e2ffd668254b24038a9cd74
diff --git a/src/tools/android/java/com/google/devtools/build/android/CompatOptionsParsingException.java b/src/tools/android/java/com/google/devtools/build/android/CompatOptionsParsingException.java
new file mode 100644
index 0000000..a226f39
--- /dev/null
+++ b/src/tools/android/java/com/google/devtools/build/android/CompatOptionsParsingException.java
@@ -0,0 +1,50 @@
+// Copyright 2024 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.android;
+
+/**
+ * An exception that's thrown when the {@link CompatShellQuotedParamsFilePreProcessor} fails.
+ *
+ * <p>This is effectively a fork of OptionsParsingException that's part of the OptionsParser lib.
+ */
+public class CompatOptionsParsingException extends Exception {
+  private final String invalidArgument;
+
+  public CompatOptionsParsingException(String message) {
+    this(message, (String) null);
+  }
+
+  public CompatOptionsParsingException(String message, String argument) {
+    super(message);
+    this.invalidArgument = argument;
+  }
+
+  public CompatOptionsParsingException(String message, Throwable throwable) {
+    this(message, null, throwable);
+  }
+
+  public CompatOptionsParsingException(String message, String argument, Throwable throwable) {
+    super(message, throwable);
+    this.invalidArgument = argument;
+  }
+
+  /**
+   * Gets the name of the invalid argument or {@code null} if the exception can not determine the
+   * exact invalid arguments
+   */
+  public String getInvalidArgument() {
+    return invalidArgument;
+  }
+}
diff --git a/src/tools/android/java/com/google/devtools/build/android/CompatShellQuotedParamsFilePreProcessor.java b/src/tools/android/java/com/google/devtools/build/android/CompatShellQuotedParamsFilePreProcessor.java
new file mode 100644
index 0000000..ab44a10
--- /dev/null
+++ b/src/tools/android/java/com/google/devtools/build/android/CompatShellQuotedParamsFilePreProcessor.java
@@ -0,0 +1,163 @@
+// Copyright 2024 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.android;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import java.io.IOException;
+import java.io.PushbackReader;
+import java.io.Reader;
+import java.nio.file.FileSystem;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import javax.annotation.Nullable;
+
+/**
+ * Emulates the behavior of the ShellQuotedParamsFilePreProcessor class from Bazel.
+ *
+ * <p>This class purely emulates the quote escaping/unescaping that
+ * ShellQuotedParamsFilePreProcessor does. It is intended to be used in ResourceProcessorBusyBox
+ * (and affiliated tools) in conjunction with JCommander's Parameter annotations instead of the
+ * Bazel-specific OptionsParser. There is no guarantee that this class will behave 100% identically
+ * to ShellQuotedParamsFilePreProcessor.
+ */
+public class CompatShellQuotedParamsFilePreProcessor {
+  private FileSystem fs;
+  static final String UNFINISHED_QUOTE_MESSAGE_FORMAT = "Unfinished quote %s at %s";
+
+  public CompatShellQuotedParamsFilePreProcessor(FileSystem fs) {
+    this.fs = fs;
+  }
+
+  public List<String> preProcess(List<String> args) throws CompatOptionsParsingException {
+    if (!args.isEmpty() && args.get(0).startsWith("@")) {
+      if (args.size() > 1) {
+        throw new CompatOptionsParsingException(
+            String.format("A params file must be the only argument: %s", args), args.get(0));
+      }
+      Path path = fs.getPath(args.get(0).substring(1));
+      try {
+        return parse(path);
+      } catch (RuntimeException | IOException e) {
+        throw new CompatOptionsParsingException(
+            String.format("Error reading params file: %s %s", path, e.getMessage()),
+            args.get(0),
+            e);
+      }
+    }
+    return args;
+  }
+
+  public List<String> parse(Path paramsFile) throws IOException {
+    List<String> args = new ArrayList<>();
+    try (ShellQuotedReader reader =
+        new ShellQuotedReader(Files.newBufferedReader(paramsFile, UTF_8))) {
+      String arg;
+      while ((arg = reader.readArg()) != null) {
+        args.add(arg);
+      }
+    }
+    return args;
+  }
+
+  private static class ShellQuotedReader implements AutoCloseable {
+
+    private final PushbackReader reader;
+    private int position = -1;
+
+    public ShellQuotedReader(Reader reader) {
+      this.reader = new PushbackReader(reader, 10);
+    }
+
+    private char read() throws IOException {
+      int value = reader.read();
+      position++;
+      return (char) value;
+    }
+
+    private void unread(char value) throws IOException {
+      reader.unread(value);
+      position--;
+    }
+
+    private boolean hasNext() throws IOException {
+      char value = read();
+      boolean hasNext = value != (char) -1;
+      unread(value);
+      return hasNext;
+    }
+
+    @Override
+    public void close() throws IOException {
+      reader.close();
+    }
+
+    @Nullable
+    public String readArg() throws IOException {
+      if (!hasNext()) {
+        return null;
+      }
+
+      StringBuilder arg = new StringBuilder();
+
+      int quoteStart = -1;
+      boolean quoted = false;
+      char current;
+
+      while ((current = read()) != (char) -1) {
+        if (quoted) {
+          if (current == '\'') {
+            StringBuilder escapedQuoteRemainder =
+                new StringBuilder().append(read()).append(read()).append(read());
+            if (escapedQuoteRemainder.toString().equals("\\''")) {
+              arg.append("'");
+            } else {
+              for (char c : escapedQuoteRemainder.reverse().toString().toCharArray()) {
+                unread(c);
+              }
+              quoted = false;
+              quoteStart = -1;
+            }
+          } else {
+            arg.append(current);
+          }
+        } else {
+          if (current == '\'') {
+            quoted = true;
+            quoteStart = position;
+          } else if (current == '\r') {
+            char next = read();
+            if (next == '\n') {
+              return arg.toString();
+            } else {
+              unread(next);
+              return arg.toString();
+            }
+          } else if (Character.isWhitespace(current)) {
+            return arg.toString();
+          } else {
+            arg.append(current);
+          }
+        }
+      }
+      if (quoted) {
+        throw new IOException(String.format(UNFINISHED_QUOTE_MESSAGE_FORMAT, "'", quoteStart));
+      }
+      return arg.toString();
+    }
+  }
+}
diff --git a/src/tools/android/java/com/google/devtools/build/android/ResourceProcessorBusyBox.java b/src/tools/android/java/com/google/devtools/build/android/ResourceProcessorBusyBox.java
index 7601e71..9250887 100644
--- a/src/tools/android/java/com/google/devtools/build/android/ResourceProcessorBusyBox.java
+++ b/src/tools/android/java/com/google/devtools/build/android/ResourceProcessorBusyBox.java
@@ -14,24 +14,23 @@
 
 package com.google.devtools.build.android;
 
+import com.beust.jcommander.JCommander;
+import com.beust.jcommander.Parameter;
+import com.beust.jcommander.ParameterException;
+import com.google.common.collect.ImmutableList;
 import com.google.devtools.build.android.aapt2.Aapt2Exception;
 import com.google.devtools.build.android.resources.JavaIdentifierValidator.InvalidJavaIdentifier;
 import com.google.devtools.build.lib.worker.ProtoWorkerMessageProcessor;
 import com.google.devtools.build.lib.worker.WorkRequestHandler;
-import com.google.devtools.common.options.EnumConverter;
-import com.google.devtools.common.options.Option;
-import com.google.devtools.common.options.OptionDocumentationCategory;
-import com.google.devtools.common.options.OptionEffectTag;
-import com.google.devtools.common.options.OptionsBase;
-import com.google.devtools.common.options.OptionsParser;
-import com.google.devtools.common.options.OptionsParsingException;
-import com.google.devtools.common.options.ShellQuotedParamsFilePreProcessor;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.PrintStream;
 import java.io.PrintWriter;
+import java.nio.file.FileSystem;
 import java.nio.file.FileSystems;
+import java.nio.file.Path;
 import java.time.Duration;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Properties;
@@ -155,29 +154,27 @@
   private static final Logger logger = Logger.getLogger(ResourceProcessorBusyBox.class.getName());
   private static final Properties properties = loadSiteCustomizations();
 
-  /** Converter for the Tool enum. */
-  public static final class ToolConverter extends EnumConverter<Tool> {
-
-    public ToolConverter() {
-      super(Tool.class, "resource tool");
-    }
-  }
-
   /** Flag specifications for this action. */
-  public static final class Options extends OptionsBase {
-    @Option(
-        name = "tool",
-        defaultValue = "null",
-        converter = ToolConverter.class,
-        category = "input",
-        documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
-        effectTags = {OptionEffectTag.UNKNOWN},
-        help =
+  public static final class Options {
+    @Parameter(
+        names = "--tool",
+        description =
             "The processing tool to execute. "
                 + "Valid tools: GENERATE_BINARY_R, PARSE, "
                 + "GENERATE_AAR, MERGE_MANIFEST, COMPILE_LIBRARY_RESOURCES, "
                 + "LINK_STATIC_LIBRARY, AAPT2_PACKAGE, SHRINK_AAPT2, MERGE_COMPILED.")
     public Tool tool;
+
+    // See https://jcommander.org/#_main_parameter
+    @Parameter() private List<String> residue;
+
+    public Options() {
+      this.residue = new ArrayList<>();
+    }
+
+    public List<String> getResidue() {
+      return residue;
+    }
   }
 
   public static void main(String[] args) throws Exception {
@@ -230,23 +227,33 @@
   }
 
   private static int processRequest(List<String> args) throws Exception {
-    OptionsParser optionsParser =
-        OptionsParser.builder()
-            .optionsClasses(Options.class)
-            .allowResidue(true)
-            .argsPreProcessor(new ShellQuotedParamsFilePreProcessor(FileSystems.getDefault()))
-            .build();
-    Options options;
+    Options options = new Options();
     try {
-      optionsParser.parse(args);
-      options = optionsParser.getOptions(Options.class);
-      options.tool.call(optionsParser.getResidue().toArray(new String[0]));
+      JCommander jc = new JCommander(options);
+      // Handle arg files (start with "@")
+      // NOTE: JCommander handles this automatically, but enabling Main Parameter (aka "residue")
+      // collection seems to disable this behavior. In case that behavior changes in the future,
+      // we'll want to _always_ disable this, since JCommander's handling of escaped quotes in arg
+      // files does not interact well with how the sub-tools handle them.
+      jc.setExpandAtSign(false);
+      if (args.size() == 1 && args.get(0).startsWith("@")) {
+        // Use CompatShellQuotedParamsFilePreProcessor to handle the arg file.
+        FileSystem fs = FileSystems.getDefault();
+        Path argFile = fs.getPath(args.get(0).substring(1));
+        CompatShellQuotedParamsFilePreProcessor paramsFilePreProcessor =
+            new CompatShellQuotedParamsFilePreProcessor(fs);
+        args = paramsFilePreProcessor.preProcess(ImmutableList.of("@" + argFile));
+      }
+      jc.parse(args.toArray(new String[0]));
+      ArrayList<String> residue = new ArrayList<>(options.getResidue());
+
+      options.tool.call(residue.toArray(new String[0]));
     } catch (UserException e) {
       // UserException is for exceptions that shouldn't have stack traces recorded, including
       // AndroidDataMerger.MergeConflictException.
       logger.log(Level.SEVERE, e.getMessage());
       return 1;
-    } catch (OptionsParsingException | IOException | Aapt2Exception | InvalidJavaIdentifier e) {
+    } catch (ParameterException | IOException | Aapt2Exception | InvalidJavaIdentifier e) {
       logSuppressed(e);
       throw e;
     } catch (Exception e) {