[Japi-cvs] SF.net SVN: japi: [459] libs/argparser/trunk/src/net/sf/japi/io/args
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2007-06-29 20:08:22
|
Revision: 459 http://svn.sourceforge.net/japi/?rev=459&view=rev Author: christianhujer Date: 2007-06-29 13:08:21 -0700 (Fri, 29 Jun 2007) Log Message: ----------- Improved fix for #1745284 --help options list is not sorted Now the short and long options are mixed when sorting. Modified Paths: -------------- libs/argparser/trunk/src/net/sf/japi/io/args/BasicCommand.java Added Paths: ----------- libs/argparser/trunk/src/net/sf/japi/io/args/MethodOptionComparator.java Modified: libs/argparser/trunk/src/net/sf/japi/io/args/BasicCommand.java =================================================================== --- libs/argparser/trunk/src/net/sf/japi/io/args/BasicCommand.java 2007-06-29 19:56:52 UTC (rev 458) +++ libs/argparser/trunk/src/net/sf/japi/io/args/BasicCommand.java 2007-06-29 20:08:21 UTC (rev 459) @@ -26,8 +26,7 @@ import java.util.MissingResourceException; import java.util.ResourceBundle; import java.util.Set; -import java.util.Map; -import java.util.HashMap; +import java.util.SortedSet; import java.util.TreeSet; import org.jetbrains.annotations.NotNull; @@ -96,12 +95,14 @@ maxLong = Math.max(maxLong, currentLong - ", ".length()); maxShort = Math.max(maxShort, currentShort - ", ".length()); } - final String formatString = "%s: %s%s%n"; + final String formatString = "%-" + maxShort + "s%s%-" + maxLong + "s: %s%s%n"; final Formatter format = new Formatter(System.err); format.format(getHelpHeader()); - final Map<String, Method> methodMap = new HashMap<String, Method>(); - for (final Method optionMethod : optionMethods) { + final SortedSet<Method> sortedMethods = new TreeSet<Method>(MethodOptionComparator.INSTANCE); + sortedMethods.addAll(optionMethods); + for (final Method optionMethod : sortedMethods) { final Option option = optionMethod.getAnnotation(Option.class); + final OptionType optionType = option.type(); final String[] names = option.value(); final List<String> shortNames = new ArrayList<String>(); final List<String> longNames = new ArrayList<String>(); @@ -113,13 +114,6 @@ } } final String delim = shortNames.size() > 0 && longNames.size() > 0 ? ", " : " "; - final String options = String.format("%-" + maxShort + "s%s%-" + maxLong + "s", StringJoiner.join(", ", shortNames), delim, StringJoiner.join(", ", longNames)); - methodMap.put(options, optionMethod); - } - for (final String options : new TreeSet<String>(methodMap.keySet())) { - final Method optionMethod = methodMap.get(options); - final Option option = optionMethod.getAnnotation(Option.class); - final OptionType optionType = option.type(); String description; try { final String optionKey = option.key().equals("") ? optionMethod.getName() : option.key(); @@ -127,8 +121,7 @@ } catch (final MissingResourceException ignore) { description = ""; } - format.format(formatString, options, description, optionType.getDescription()); - + format.format(formatString, StringJoiner.join(", ", shortNames), delim, StringJoiner.join(", ", longNames), description, optionType.getDescription()); } format.format(getHelpFooter()); format.flush(); Added: libs/argparser/trunk/src/net/sf/japi/io/args/MethodOptionComparator.java =================================================================== --- libs/argparser/trunk/src/net/sf/japi/io/args/MethodOptionComparator.java (rev 0) +++ libs/argparser/trunk/src/net/sf/japi/io/args/MethodOptionComparator.java 2007-06-29 20:08:21 UTC (rev 459) @@ -0,0 +1,46 @@ +/* + * JAPI libs-argparser is a library for parsing command line arguments. + * Copyright (C) 2007 Christian Hujer. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.sf.japi.io.args; + +import java.lang.reflect.Method; +import java.util.Comparator; +import java.util.Arrays; +import org.jetbrains.annotations.NotNull; + +/** Compares methods by their options. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public class MethodOptionComparator implements Comparator<Method> { + + /** Global instance. */ + @NotNull public static final Comparator<Method> INSTANCE = new MethodOptionComparator(); + + /** {@inheritDoc} */ + public int compare(@NotNull final Method o1, @NotNull final Method o2) { + final Option option1 = o1.getAnnotation(Option.class); + final Option option2 = o2.getAnnotation(Option.class); + final String[] names1 = option1.value(); + final String[] names2 = option2.value(); + Arrays.sort(names1); + Arrays.sort(names2); + return names1[0].compareTo(names2[0]); + } + +} // class MethodOptionComparator Property changes on: libs/argparser/trunk/src/net/sf/japi/io/args/MethodOptionComparator.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |