CommandLineOptionsParser { static CommandLineOptions parse(Iterable<String> options) { CommandLineOptions.Builder optionsBuilder = CommandLineOptions.builder(); List<String> expandedOptions = new ArrayList<>(); expandParamsFiles(options, expandedOptions); Iterator<String> it = expandedOptions.iterator(); while (it.hasNext()) { String option = it.next(); if (!option.startsWith("-")) { optionsBuilder.filesBuilder().add(option).addAll(it); break; } String flag; String value; int idx = option.indexOf('='); if (idx >= 0) { flag = option.substring(0, idx); value = option.substring(idx + 1, option.length()); } else { flag = option; value = null; } switch (flag) { case "-i": case "-r": case "-replace": case "--replace": optionsBuilder.inPlace(true); break; case "--lines": case "-lines": case "--line": case "-line": parseRangeSet(optionsBuilder.linesBuilder(), getValue(flag, it, value)); break; case "--offset": case "-offset": optionsBuilder.addOffset(parseInteger(it, flag, value)); break; case "--length": case "-length": optionsBuilder.addLength(parseInteger(it, flag, value)); break; case "--aosp": case "-aosp": case "-a": optionsBuilder.aosp(true); break; case "--version": case "-version": case "-v": optionsBuilder.version(true); break; case "--help": case "-help": case "-h": optionsBuilder.help(true); break; case "--fix-imports-only": optionsBuilder.fixImportsOnly(true); break; case "--skip-sorting-imports": optionsBuilder.sortImports(false); break; case "--skip-removing-unused-imports": optionsBuilder.removeUnusedImports(false); break; case "--skip-reflowing-long-strings": optionsBuilder.reflowLongStrings(false); break; case "--skip-javadoc-formatting": optionsBuilder.formatJavadoc(false); break; case "-": optionsBuilder.stdin(true); break; case "-n": case "--dry-run": optionsBuilder.dryRun(true); break; case "--set-exit-if-changed": optionsBuilder.setExitIfChanged(true); break; case "-assume-filename": case "--assume-filename": optionsBuilder.assumeFilename(getValue(flag, it, value)); break; default: throw new IllegalArgumentException("unexpected flag: " + flag); } } return optionsBuilder.build(); }    }
@Test public void defaults() { CommandLineOptions options = CommandLineOptionsParser.parse(Collections.<String>emptyList()); assertThat(options.files()).isEmpty(); assertThat(options.stdin()).isFalse(); assertThat(options.aosp()).isFalse(); assertThat(options.help()).isFalse(); assertThat(options.lengths()).isEmpty(); assertThat(options.lines().asRanges()).isEmpty(); assertThat(options.offsets()).isEmpty(); assertThat(options.inPlace()).isFalse(); assertThat(options.version()).isFalse(); assertThat(options.sortImports()).isTrue(); assertThat(options.removeUnusedImports()).isTrue(); assertThat(options.dryRun()).isFalse(); assertThat(options.setExitIfChanged()).isFalse(); assertThat(options.reflowLongStrings()).isTrue(); assertThat(options.formatJavadoc()).isTrue(); }
@Test public void hello() { CommandLineOptions options = CommandLineOptionsParser.parse( Arrays.asList("-lines=1:10,20:30", "-i", "Hello.java", "Goodbye.java")); assertThat(options.lines().asRanges()) .containsExactly(Range.closedOpen(0, 10), Range.closedOpen(19, 30)); assertThat(options.inPlace()).isTrue(); assertThat(options.files()).containsExactly("Hello.java", "Goodbye.java"); }
@Test public void stdin() { assertThat(CommandLineOptionsParser.parse(Arrays.asList("-")).stdin()).isTrue(); }
@Test public void aosp() { assertThat(CommandLineOptionsParser.parse(Arrays.asList("-aosp")).aosp()).isTrue(); }
@Test public void help() { assertThat(CommandLineOptionsParser.parse(Arrays.asList("-help")).help()).isTrue(); }
@Test public void lengths() { assertThat( CommandLineOptionsParser.parse(Arrays.asList("-length", "1", "--length", "2")) .lengths()) .containsExactly(1, 2); }
@Test public void lines() { assertThat( CommandLineOptionsParser.parse( Arrays.asList("--lines", "1:2", "-lines=4:5", "--line", "7:8", "-line=10:11")) .lines() .asRanges()) .containsExactly( Range.closedOpen(0, 2), Range.closedOpen(3, 5), Range.closedOpen(6, 8), Range.closedOpen(9, 11)); }
@Test public void offset() { assertThat( CommandLineOptionsParser.parse(Arrays.asList("-offset", "1", "--offset", "2")) .offsets()) .containsExactly(1, 2); }
@Test public void inPlace() { assertThat(CommandLineOptionsParser.parse(Arrays.asList("-i", "A.java")).inPlace()).isTrue(); }
@Test public void version() { assertThat(CommandLineOptionsParser.parse(Arrays.asList("-v")).version()).isTrue(); }
@Test public void skipSortingImports() { assertThat( CommandLineOptionsParser.parse(Arrays.asList("--skip-sorting-imports")).sortImports()) .isFalse(); }
@Test public void skipRemovingUnusedImports() { assertThat( CommandLineOptionsParser.parse(Arrays.asList("--skip-removing-unused-imports")) .removeUnusedImports()) .isFalse(); }
@Test public void dryRun() { assertThat(CommandLineOptionsParser.parse(Arrays.asList("--dry-run")).dryRun()).isTrue(); assertThat(CommandLineOptionsParser.parse(Arrays.asList("-n")).dryRun()).isTrue(); }
@Test public void setExitIfChanged() { assertThat( CommandLineOptionsParser.parse(Arrays.asList("--set-exit-if-changed")) .setExitIfChanged()) .isTrue(); }
@Test public void illegalLines() { try { CommandLineOptionsParser.parse(Arrays.asList("-lines=1:1", "-lines=1:1")); fail(); } catch (IllegalArgumentException e) { assertThat(e.getMessage()).contains("overlap"); } }
@Test public void paramsFile() throws IOException { Path outer = testFolder.newFile("outer").toPath(); Path exit = testFolder.newFile("exit").toPath(); Path nested = testFolder.newFile("nested").toPath(); String[] args = {"--dry-run", "@" + exit, "L", "@" + outer, "Q"}; Files.write(exit, "--set-exit-if-changed".getBytes(UTF_8)); Files.write(outer, ("M\n@" + nested.toAbsolutePath() + "\nP").getBytes(UTF_8)); Files.write(nested, "ℕ\n\n \n@@O\n".getBytes(UTF_8)); CommandLineOptions options = CommandLineOptionsParser.parse(Arrays.asList(args)); assertThat(options.files()).containsExactly("L", "M", "ℕ", "@O", "P", "Q"); }
@Test public void assumeFilename() { assertThat( CommandLineOptionsParser.parse(Arrays.asList("--assume-filename", "Foo.java")) .assumeFilename()) .hasValue("Foo.java"); assertThat(CommandLineOptionsParser.parse(Arrays.asList("Foo.java")).assumeFilename()) .isEmpty(); }
@Test public void skipReflowLongStrings() { assertThat( CommandLineOptionsParser.parse(Arrays.asList("--skip-reflowing-long-strings")) .reflowLongStrings()) .isFalse(); }
@Test public void skipJavadocFormatting() { assertThat( CommandLineOptionsParser.parse(Arrays.asList("--skip-javadoc-formatting")) .formatJavadoc()) .isFalse(); }