[pacman-dev] [PATCH] Sort short options before long options in --help

Xavier Chantry chantry.xavier at gmail.com
Thu Oct 14 09:36:04 EDT 2010


$ pacman -Uh
options:
  -b, --dbpath <path>  set an alternate database location
  -d, --nodeps         skip dependency checks
  -f, --force          force install, overwrite conflicting files
  -k, --dbonly         only modify database entries, not package files
  -r, --root <path>    set an alternate installation root
  -v, --verbose        be verbose
      --arch <arch>    set an alternate architecture
      --asdeps         install packages as non-explicitly installed
      --asexplicit     install packages as explicitly installed
      --cachedir <dir> set an alternate package cache location
      --config <path>  set an alternate configuration file
      --debug          display debug messages
      --ignore <pkg>   ignore a package upgrade (can be used more than once)
      --ignoregroup <grp>
                       ignore a group upgrade (can be used more than once)
      --logfile <path> set an alternate log file
      --noconfirm      do not ask for any confirmation
      --noprogressbar  do not show a progress bar when downloading files
      --noscriptlet    do not execute the install scriptlet if one exists
      --print          only print the targets instead of performing the operation
      --print-format <string>
                       specify how the targets should be printed

Signed-off-by: Xavier Chantry <chantry.xavier at gmail.com>
---
 src/pacman/pacman.c |   39 ++++++++++++++++++++++++++++++++++++++-
 1 files changed, 38 insertions(+), 1 deletions(-)

diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c
index 7d51124..38d7f6a 100644
--- a/src/pacman/pacman.c
+++ b/src/pacman/pacman.c
@@ -26,8 +26,10 @@
 #define PACKAGE_VERSION GIT_VERSION
 #endif
 
+#include <assert.h>
 #include <stdlib.h> /* atoi */
 #include <stdio.h>
+#include <ctype.h> /* isspace */
 #include <limits.h>
 #include <getopt.h>
 #include <string.h>
@@ -59,6 +61,41 @@ pmdb_t *db_local;
 /* list of targets specified on command line */
 static alpm_list_t *pm_targets;
 
+/* Used to sort the options in --help */
+static int options_cmp(const void *p1, const void *p2)
+{
+	const char *s1 = p1;
+	const char *s2 = p2;
+
+	assert(s1);
+	assert(s2);
+	/* First skip all spaces in both strings */
+	while(isspace((unsigned char)*s1))
+		s1++;
+	while(isspace((unsigned char)*s2))
+		s2++;
+	/* If we compare a long option (--abcd) and a short one (-a),
+	 * the short one always wins */
+	if(*s1 == '-' && *s2 == '-') {
+		s1++;
+		s2++;
+		if(*s1 == '-' && *s2 == '-') {
+			/* two long -> strcmp */
+			s1++;
+			s2++;
+		} else if(*s2 == '-') {
+			/* s1 short, s2 long */
+			return(-1);
+		} else if(*s1 == '-') {
+			/* s1 long, s2 short */
+			return(1);
+		}
+		/* two short -> strcmp */
+	}
+
+	return(strcmp(s1, s2));
+}
+
 /** Display usage/syntax for the specified operation.
  * @param op     the operation code requested
  * @param myname basename(argv[0])
@@ -166,7 +203,7 @@ static void usage(int op, const char * const myname)
 		addlist(_("      --logfile <path> set an alternate log file\n"));
 		addlist(_("      --noconfirm      do not ask for any confirmation\n"));
 	}
-	list = alpm_list_msort(list, alpm_list_count(list), str_cmp);
+	list = alpm_list_msort(list, alpm_list_count(list), options_cmp);
 	for (i = list; i; i = alpm_list_next(i)) {
 		printf("%s", (char *)alpm_list_getdata(i));
 	}
-- 
1.7.3.1



More information about the pacman-dev mailing list