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

Xavier Chantry chantry.xavier at gmail.com
Thu Oct 14 08:18:50 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 |   27 ++++++++++++++++++++++++++-
 1 files changed, 26 insertions(+), 1 deletions(-)

diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c
index 7d51124..3bfe6ef 100644
--- a/src/pacman/pacman.c
+++ b/src/pacman/pacman.c
@@ -28,6 +28,7 @@
 
 #include <stdlib.h> /* atoi */
 #include <stdio.h>
+#include <ctype.h> /* isspace */
 #include <limits.h>
 #include <getopt.h>
 #include <string.h>
@@ -59,6 +60,30 @@ 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;
+	int ret = 0;
+
+	/* 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+1) != '-' && *(s2+1) == '-') {
+		ret = -1;
+	} else if (*(s2+1) != '-' && *(s1+1) == '-') {
+		ret = 1;
+	} else {
+		ret = strcmp(s1, s2);
+	}
+	return(ret);
+}
+
 /** Display usage/syntax for the specified operation.
  * @param op     the operation code requested
  * @param myname basename(argv[0])
@@ -166,7 +191,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