From 2b7517132fc069f971d0be46de696c7ea1c5c026 Mon Sep 17 00:00:00 2001 From: Taro Tanaka Date: Tue, 17 Jan 2023 20:52:27 +0900 Subject: [PATCH] usage: Divide options into different sections based on the man page This makes it easier to find operation-specific options and distinguish between common and advanced options. Signed-off-by: Taro Tanaka --- src/pacman/pacman.c | 224 +++++++++++++++++++------------------------- 1 file changed, 94 insertions(+), 130 deletions(-) diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c index e5c6e420..cea86cdf 100644 --- a/src/pacman/pacman.c +++ b/src/pacman/pacman.c @@ -49,52 +49,12 @@ /* 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; - - if(s1 == s2) return 0; - if(!s1) return -1; - if(!s2) return 1; - /* 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]) */ static void usage(int op, const char * const myname) { -#define addlist(s) (list = alpm_list_add(list, s)) - alpm_list_t *list = NULL, *i; /* prefetch some strings for usage below, which moves a lot of calls * out of gettext. */ char const *const str_opt = _("options"); @@ -102,10 +62,12 @@ static void usage(int op, const char * const myname) char const *const str_pkg = _("package(s)"); char const *const str_usg = _("usage"); char const *const str_opr = _("operation"); + char const *const str_mopt = _("main options"); /* please limit your strings to 80 characters in width */ if(op == PM_OP_MAIN) { printf("%s: %s <%s> [...]\n", str_usg, myname, str_opr); + printf("\n"); printf(_("operations:\n")); printf(" %s {-h --help}\n", myname); printf(" %s {-V --version}\n", myname); @@ -121,118 +83,120 @@ static void usage(int op, const char * const myname) } else { if(op == PM_OP_REMOVE) { printf("%s: %s {-R --remove} [%s] <%s>\n", str_usg, myname, str_opt, str_pkg); - printf("%s:\n", str_opt); - addlist(_(" -c, --cascade remove packages and all packages that depend on them\n")); - addlist(_(" -n, --nosave remove configuration files\n")); - addlist(_(" -s, --recursive remove unnecessary dependencies\n" - " (-ss includes explicitly installed dependencies)\n")); - addlist(_(" -u, --unneeded remove unneeded packages\n")); + printf("\n"); + printf("%s:\n", str_mopt); + printf(_(" -c, --cascade remove packages and all packages that depend on them\n")); + printf(_(" -n, --nosave remove configuration files\n")); + printf(_(" -s, --recursive remove unnecessary dependencies\n" + " (-ss includes explicitly installed dependencies)\n")); + printf(_(" -u, --unneeded remove unneeded packages\n")); } else if(op == PM_OP_UPGRADE) { printf("%s: %s {-U --upgrade} [%s] <%s>\n", str_usg, myname, str_opt, str_file); - addlist(_(" --needed do not reinstall up to date packages\n")); - printf("%s:\n", str_opt); } else if(op == PM_OP_QUERY) { printf("%s: %s {-Q --query} [%s] [%s]\n", str_usg, myname, str_opt, str_pkg); - printf("%s:\n", str_opt); - addlist(_(" -c, --changelog view the changelog of a package\n")); - addlist(_(" -d, --deps list packages installed as dependencies [filter]\n")); - addlist(_(" -e, --explicit list packages explicitly installed [filter]\n")); - addlist(_(" -g, --groups view all members of a package group\n")); - addlist(_(" -i, --info view package information (-ii for backup files)\n")); - addlist(_(" -k, --check check that package files exist (-kk for file properties)\n")); - addlist(_(" -l, --list list the files owned by the queried package\n")); - addlist(_(" -m, --foreign list installed packages not found in sync db(s) [filter]\n")); - addlist(_(" -n, --native list installed packages only found in sync db(s) [filter]\n")); - addlist(_(" -o, --owns query the package that owns \n")); - addlist(_(" -p, --file query a package file instead of the database\n")); - addlist(_(" -q, --quiet show less information for query and search\n")); - addlist(_(" -s, --search search locally-installed packages for matching strings\n")); - addlist(_(" -t, --unrequired list packages not (optionally) required by any\n" - " package (-tt to ignore optdepends) [filter]\n")); - addlist(_(" -u, --upgrades list outdated packages [filter]\n")); + printf("\n"); + printf("%s:\n", str_mopt); + printf(_(" -c, --changelog view the changelog of a package\n")); + printf(_(" -d, --deps list packages installed as dependencies [filter]\n")); + printf(_(" -e, --explicit list packages explicitly installed [filter]\n")); + printf(_(" -g, --groups view all members of a package group\n")); + printf(_(" -i, --info view package information (-ii for backup files)\n")); + printf(_(" -k, --check check that package files exist (-kk for file properties)\n")); + printf(_(" -l, --list list the files owned by the queried package\n")); + printf(_(" -m, --foreign list installed packages not found in sync db(s) [filter]\n")); + printf(_(" -n, --native list installed packages only found in sync db(s) [filter]\n")); + printf(_(" -o, --owns query the package that owns \n")); + printf(_(" -p, --file query a package file instead of the database\n")); + printf(_(" -q, --quiet show less information for query and search\n")); + printf(_(" -s, --search search locally-installed packages for matching strings\n")); + printf(_(" -t, --unrequired list packages not (optionally) required by any\n" + " package (-tt to ignore optdepends) [filter]\n")); + printf(_(" -u, --upgrades list outdated packages [filter]\n")); } else if(op == PM_OP_SYNC) { printf("%s: %s {-S --sync} [%s] [%s]\n", str_usg, myname, str_opt, str_pkg); - printf("%s:\n", str_opt); - addlist(_(" -c, --clean remove old packages from cache directory (-cc for all)\n")); - addlist(_(" -g, --groups view all members of a package group\n" - " (-gg to view all groups and members)\n")); - addlist(_(" -i, --info view package information (-ii for extended information)\n")); - addlist(_(" -l, --list view a list of packages in a repo\n")); - addlist(_(" -q, --quiet show less information for query and search\n")); - addlist(_(" -s, --search search remote repositories for matching strings\n")); - addlist(_(" -u, --sysupgrade upgrade installed packages (-uu enables downgrades)\n")); - addlist(_(" -y, --refresh download fresh package databases from the server\n" - " (-yy to force a refresh even if up to date)\n")); - addlist(_(" --needed do not reinstall up to date packages\n")); + printf("\n"); + printf("%s:\n", str_mopt); + printf(_(" -c, --clean remove old packages from cache directory (-cc for all)\n")); + printf(_(" -g, --groups view all members of a package group\n" + " (-gg to view all groups and members)\n")); + printf(_(" -i, --info view package information (-ii for extended information)\n")); + printf(_(" -l, --list view a list of packages in a repo\n")); + printf(_(" -q, --quiet show less information for query and search\n")); + printf(_(" -s, --search search remote repositories for matching strings\n")); + printf(_(" -u, --sysupgrade upgrade installed packages (-uu enables downgrades)\n")); + printf(_(" -y, --refresh download fresh package databases from the server\n" + " (-yy to force a refresh even if up to date)\n")); } else if(op == PM_OP_DATABASE) { printf("%s: %s {-D --database} <%s> <%s>\n", str_usg, myname, str_opt, str_pkg); - printf("%s:\n", str_opt); - addlist(_(" --asdeps mark packages as non-explicitly installed\n")); - addlist(_(" --asexplicit mark packages as explicitly installed\n")); - addlist(_(" -k, --check test local database for validity (-kk for sync databases)\n")); - addlist(_(" -q, --quiet suppress output of success messages\n")); + printf("\n"); + printf("%s:\n", str_mopt); + printf(_(" --asdeps mark packages as non-explicitly installed\n")); + printf(_(" --asexplicit mark packages as explicitly installed\n")); + printf(_(" -k, --check test local database for validity (-kk for sync databases)\n")); + printf(_(" -q, --quiet suppress output of success messages\n")); } else if(op == PM_OP_DEPTEST) { printf("%s: %s {-T --deptest} [%s] [%s]\n", str_usg, myname, str_opt, str_pkg); - printf("%s:\n", str_opt); } else if(op == PM_OP_FILES) { printf("%s: %s {-F --files} [%s] [%s]\n", str_usg, myname, str_opt, str_file); - printf("%s:\n", str_opt); - addlist(_(" -l, --list list the files owned by the queried package\n")); - addlist(_(" -q, --quiet show less information for query and search\n")); - addlist(_(" -x, --regex enable searching using regular expressions\n")); - addlist(_(" -y, --refresh download fresh package databases from the server\n" - " (-yy to force a refresh even if up to date)\n")); - addlist(_(" --machinereadable\n" - " produce machine-readable output\n")); + printf("\n"); + printf("%s:\n", str_mopt); + printf(_(" -y, --refresh download fresh package databases from the server\n" + " (-yy to force a refresh even if up to date)\n")); + printf(_(" -l, --list list the files owned by the queried package\n")); + printf(_(" -x, --regex enable searching using regular expressions\n")); + printf(_(" -q, --quiet show less information for query and search\n")); + printf(_(" --machinereadable\n" + " produce machine-readable output\n")); } switch(op) { case PM_OP_SYNC: case PM_OP_UPGRADE: - addlist(_(" -w, --downloadonly download packages but do not install/upgrade anything\n")); - addlist(_(" --overwrite \n" - " overwrite conflicting files (can be used more than once)\n")); - addlist(_(" --asdeps install packages as non-explicitly installed\n")); - addlist(_(" --asexplicit install packages as explicitly installed\n")); - addlist(_(" --ignore ignore a package upgrade (can be used more than once)\n")); - addlist(_(" --ignoregroup \n" - " ignore a group upgrade (can be used more than once)\n")); + printf("\n"); + printf(_("upgrade options:\n")); + printf(_(" -w, --downloadonly download packages but do not install/upgrade anything\n")); + printf(_(" --asdeps install packages as non-explicitly installed\n")); + printf(_(" --asexplicit install packages as explicitly installed\n")); + printf(_(" --ignore ignore a package upgrade (can be used more than once)\n")); + printf(_(" --ignoregroup \n" + " ignore a group upgrade (can be used more than once)\n")); + printf(_(" --needed do not reinstall up to date packages\n")); + printf(_(" --overwrite \n" + " overwrite conflicting files (can be used more than once)\n")); __attribute__((fallthrough)); case PM_OP_REMOVE: - addlist(_(" -d, --nodeps skip dependency version checks (-dd to skip all checks)\n")); - addlist(_(" --assume-installed \n" - " add a virtual package to satisfy dependencies\n")); - addlist(_(" --dbonly only modify database entries, not package files\n")); - addlist(_(" --noprogressbar do not show a progress bar when downloading files\n")); - addlist(_(" --noscriptlet do not execute the install scriptlet if one exists\n")); - addlist(_(" -p, --print print the targets instead of performing the operation\n")); - addlist(_(" --print-format \n" - " specify how the targets should be printed\n")); + printf("\n"); + printf(_("transaction options:\n")); + printf(_(" -d, --nodeps skip dependency version checks (-dd to skip all checks)\n")); + printf(_(" --assume-installed \n" + " add a virtual package to satisfy dependencies\n")); + printf(_(" --dbonly only modify database entries, not package files\n")); + printf(_(" --noprogressbar do not show a progress bar when downloading files\n")); + printf(_(" --noscriptlet do not execute the install scriptlet if one exists\n")); + printf(_(" -p, --print print the targets instead of performing the operation\n")); + printf(_(" --print-format \n" + " specify how the targets should be printed\n")); break; } - addlist(_(" -b, --dbpath set an alternate database location\n")); - addlist(_(" -r, --root set an alternate installation root\n")); - addlist(_(" -v, --verbose be verbose\n")); - addlist(_(" --arch set an alternate architecture\n")); - addlist(_(" --sysroot operate on a mounted guest system (root-only)\n")); - addlist(_(" --cachedir set an alternate package cache location\n")); - addlist(_(" --hookdir set an alternate hook location\n")); - addlist(_(" --color colorize the output\n")); - addlist(_(" --config set an alternate configuration file\n")); - addlist(_(" --debug display debug messages\n")); - addlist(_(" --gpgdir set an alternate home directory for GnuPG\n")); - addlist(_(" --logfile set an alternate log file\n")); - addlist(_(" --noconfirm do not ask for any confirmation\n")); - addlist(_(" --confirm always ask for confirmation\n")); - addlist(_(" --disable-download-timeout\n" - " use relaxed timeouts for download\n")); + printf("\n"); + printf(_("general options:\n")); + printf(_(" -b, --dbpath set an alternate database location\n")); + printf(_(" -r, --root set an alternate installation root\n")); + printf(_(" -v, --verbose be verbose\n")); + printf(_(" --arch set an alternate architecture\n")); + printf(_(" --cachedir set an alternate package cache location\n")); + printf(_(" --color colorize the output\n")); + printf(_(" --config set an alternate configuration file\n")); + printf(_(" --debug display debug messages\n")); + printf(_(" --gpgdir set an alternate home directory for GnuPG\n")); + printf(_(" --hookdir set an alternate hook location\n")); + printf(_(" --logfile set an alternate log file\n")); + printf(_(" --noconfirm do not ask for any confirmation\n")); + printf(_(" --confirm always ask for confirmation\n")); + printf(_(" --disable-download-timeout\n" + " use relaxed timeouts for download\n")); + printf(_(" --sysroot operate on a mounted guest system (root-only)\n")); } - list = alpm_list_msort(list, alpm_list_count(list), options_cmp); - for(i = list; i; i = alpm_list_next(i)) { - fputs((const char *)i->data, stdout); - } - alpm_list_free(list); -#undef addlist } /** Output pacman version and copyright.