@Test public void testSortByShortestOptionNameComparator() throws Exception { class App { @Option(names = {"-t", "--aaaa"}) boolean aaaa; @Option(names = {"--bbbb", "-k"}) boolean bbbb; @Option(names = {"-c", "--cccc"}) boolean cccc; } OptionSpec[] fields = options(new App(), "aaaa", "bbbb", "cccc"); // -tkc Arrays.sort(fields, new Help.SortByShortestOptionNameAlphabetically()); OptionSpec[] expected = options(new App(), "cccc", "bbbb", "aaaa"); // -ckt assertEquals(expected[0], fields[0]); assertEquals(expected[1], fields[1]); assertEquals(expected[2], fields[2]); assertArrayEquals(expected, fields); }
@Test public void testSortByShortestOptionNameComparator() throws Exception { class App { @Option(names = {"-t", "--aaaa"}) boolean aaaa; @Option(names = {"--bbbb", "-k"}) boolean bbbb; @Option(names = {"-c", "--cccc"}) boolean cccc; } OptionSpec[] fields = options(new App(), "aaaa", "bbbb", "cccc"); // -tkc Arrays.sort(fields, new Help.SortByShortestOptionNameAlphabetically()); OptionSpec[] expected = options(new App(), "cccc", "bbbb", "aaaa"); // -ckt assertEquals(expected[0], fields[0]); assertEquals(expected[1], fields[1]); assertEquals(expected[2], fields[2]); assertArrayEquals(expected, fields); }
public int compare(OptionSpec o1, OptionSpec o2) { Range arity1 = o1.arity(); Range arity2 = o2.arity(); int result = arity1.max - arity2.max; if (result == 0) { result = arity1.min - arity2.min; } if (result == 0) { // arity is same if (o1.isMultiValue() && !o2.isMultiValue()) { result = 1; } // f1 > f2 if (!o1.isMultiValue() && o2.isMultiValue()) { result = -1; } // f1 < f2 } return result == 0 ? super.compare(o1, o2) : result; } }
@SuppressWarnings("deprecation") @Test public void testDetailedSynopsis() { Help help = new Help(CommandSpec.create(), new Help.ColorScheme(Help.Ansi.OFF)); String str = help.detailedSynopsis(new Help.SortByShortestOptionNameAlphabetically(), true); assertEquals(String.format("<main class>%n"), str); }
@Test public void testSortByShortestOptionNameAlphabetically_handlesNulls() throws Exception { Help.SortByShortestOptionNameAlphabetically sort = new Help.SortByShortestOptionNameAlphabetically(); OptionSpec a = OptionSpec.builder("-a").build(); OptionSpec b = OptionSpec.builder("-b").build(); assertEquals(1, sort.compare(null, a)); assertEquals(-1, sort.compare(a, null)); assertEquals(-1, sort.compare(a, b)); assertEquals(0, sort.compare(a, a)); assertEquals(1, sort.compare(b, a)); }
/** Sorts {@link OptionSpec OptionSpecs} by their option name in case-insensitive alphabetic order. If an * option has multiple names, the shortest name is used for the sorting. Help options follow non-help options. * @return a comparator that sorts OptionSpecs by their option name in case-insensitive alphabetic order */ public static Comparator<OptionSpec> createShortOptionNameComparator() { return new SortByShortestOptionNameAlphabetically(); } /** Sorts {@link OptionSpec OptionSpecs} by their option {@linkplain Range#max max arity} first, by
/** Sorts {@link OptionSpec OptionSpecs} by their option name in case-insensitive alphabetic order. If an * option has multiple names, the shortest name is used for the sorting. Help options follow non-help options. * @return a comparator that sorts OptionSpecs by their option name in case-insensitive alphabetic order */ public static Comparator<OptionSpec> createShortOptionNameComparator() { return new SortByShortestOptionNameAlphabetically(); } /** Sorts {@link OptionSpec OptionSpecs} by their option {@linkplain Range#max max arity} first, by
public int compare(OptionSpec o1, OptionSpec o2) { Range arity1 = o1.arity(); Range arity2 = o2.arity(); int result = arity1.max - arity2.max; if (result == 0) { result = arity1.min - arity2.min; } if (result == 0) { // arity is same if (o1.isMultiValue() && !o2.isMultiValue()) { result = 1; } // f1 > f2 if (!o1.isMultiValue() && o2.isMultiValue()) { result = -1; } // f1 < f2 } return result == 0 ? super.compare(o1, o2) : result; } }
@Test public void testSortByShortestOptionNameAlphabetically_handlesNulls() throws Exception { Help.SortByShortestOptionNameAlphabetically sort = new Help.SortByShortestOptionNameAlphabetically(); OptionSpec a = OptionSpec.builder("-a").build(); OptionSpec b = OptionSpec.builder("-b").build(); assertEquals(1, sort.compare(null, a)); assertEquals(-1, sort.compare(a, null)); assertEquals(-1, sort.compare(a, b)); assertEquals(0, sort.compare(a, a)); assertEquals(1, sort.compare(b, a)); }
@SuppressWarnings("deprecation") @Test public void testDetailedSynopsis() { Help help = new Help(CommandSpec.create(), new Help.ColorScheme(Help.Ansi.OFF)); String str = help.detailedSynopsis(new Help.SortByShortestOptionNameAlphabetically(), true); assertEquals(String.format("<main class>%n"), str); }