@Override
  public int run(InputStream in, PrintStream out, PrintStream err, List<String> args) throws Exception {
    boolean useJavaCC = "--useJavaCC".equals(getArg(args, 0, null));

    if (args.isEmpty() || args.size() > (useJavaCC ? 3 : 2) || isRequestingHelp(args)) {
      err.println("Usage: idl2schemata [--useJavaCC] [idl [outdir]]");
      err.println();
      err.println("If an output directory is not specified, " + "outputs to current directory.");
      return -1;
    }

    String inputName = getArg(args, useJavaCC ? 1 : 0, "-");
    File inputFile = "-".equals(inputName) ? null : new File(inputName);
    File outputDirectory = getOutputDirectory(getArg(args, useJavaCC ? 2 : 1, ""));

    if (useJavaCC) {
      try (Idl parser = new Idl(inputFile)) {
        final Protocol protocol = parser.CompilationUnit();
        final List<String> warnings = parser.getWarningsAfterParsing();
        for (String warning : warnings) {
          err.println("Warning: " + warning);
        }
        for (Schema schema : protocol.getTypes()) {
          print(schema, outputDirectory);
        }
      }
    } else {
      IdlReader parser = new IdlReader();
      IdlFile idlFile = inputFile == null ? parser.parse(in) : parser.parse(inputFile.toPath());
      for (String warning : idlFile.getWarnings()) {
        err.println("Warning: " + warning);
      }
      for (Schema schema : idlFile.getNamedSchemas().values()) {
        print(schema, outputDirectory);
      }
    }

    return 0;
  }
@Test
  public void testSplitIdlIntoSchemataUsingJavaCC() throws Exception {
    String idl = "src/test/idl/protocol.avdl";
    String outdir = "target/test-split";

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    List<String> arglist = Arrays.asList("--useJavaCC", idl, outdir);
    new IdlToSchemataTool().run(null, null, new PrintStream(buffer), arglist);

    String[] files = new File(outdir).list();
    assertEquals(4, files.length);

    String warnings = readPrintStreamBuffer(buffer);
    assertEquals(
        "Warning: Found documentation comment at line 19, column 1. Ignoring previous one at line 1, column 1: "
            + "\"Licensed to the Apache Software Foundation (ASF) under one\n"
            + "or more contributor license agreements.  See the NOTICE file\n"
            + "distributed with this work for additional information\n"
            + "regarding copyright ownership.  The ASF licenses this file\n"
            + "to you under the Apache License, Version 2.0 (the\n"
            + "\"License\"); you may not use this file except in compliance\n"
            + "with the License.  You may obtain a copy of the License at\n"
            + "\n    https://www.apache.org/licenses/LICENSE-2.0\n\n"
            + "Unless required by applicable law or agreed to in writing, software\n"
            + "distributed under the License is distributed on an \"AS IS\" BASIS,\n"
            + "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"
            + "See the License for the specific language governing permissions and\n"
            + "limitations under the License.\"\nDid you mean to use a multiline comment ( /* ... */ ) instead?",
        warnings);
  }