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 <mkrmdk@gmail.com>
This commit is contained in:
Taro Tanaka 2023-01-17 20:52:27 +09:00
parent d35728f924
commit 2b7517132f

View file

@ -49,52 +49,12 @@
/* list of targets specified on command line */ /* list of targets specified on command line */
static alpm_list_t *pm_targets; 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. /** Display usage/syntax for the specified operation.
* @param op the operation code requested * @param op the operation code requested
* @param myname basename(argv[0]) * @param myname basename(argv[0])
*/ */
static void usage(int op, const char * const myname) 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 /* prefetch some strings for usage below, which moves a lot of calls
* out of gettext. */ * out of gettext. */
char const *const str_opt = _("options"); 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_pkg = _("package(s)");
char const *const str_usg = _("usage"); char const *const str_usg = _("usage");
char const *const str_opr = _("operation"); char const *const str_opr = _("operation");
char const *const str_mopt = _("main options");
/* please limit your strings to 80 characters in width */ /* please limit your strings to 80 characters in width */
if(op == PM_OP_MAIN) { if(op == PM_OP_MAIN) {
printf("%s: %s <%s> [...]\n", str_usg, myname, str_opr); printf("%s: %s <%s> [...]\n", str_usg, myname, str_opr);
printf("\n");
printf(_("operations:\n")); printf(_("operations:\n"));
printf(" %s {-h --help}\n", myname); printf(" %s {-h --help}\n", myname);
printf(" %s {-V --version}\n", myname); printf(" %s {-V --version}\n", myname);
@ -121,118 +83,120 @@ static void usage(int op, const char * const myname)
} else { } else {
if(op == PM_OP_REMOVE) { if(op == PM_OP_REMOVE) {
printf("%s: %s {-R --remove} [%s] <%s>\n", str_usg, myname, str_opt, str_pkg); printf("%s: %s {-R --remove} [%s] <%s>\n", str_usg, myname, str_opt, str_pkg);
printf("%s:\n", str_opt); printf("\n");
addlist(_(" -c, --cascade remove packages and all packages that depend on them\n")); printf("%s:\n", str_mopt);
addlist(_(" -n, --nosave remove configuration files\n")); printf(_(" -c, --cascade remove packages and all packages that depend on them\n"));
addlist(_(" -s, --recursive remove unnecessary dependencies\n" printf(_(" -n, --nosave remove configuration files\n"));
printf(_(" -s, --recursive remove unnecessary dependencies\n"
" (-ss includes explicitly installed dependencies)\n")); " (-ss includes explicitly installed dependencies)\n"));
addlist(_(" -u, --unneeded remove unneeded packages\n")); printf(_(" -u, --unneeded remove unneeded packages\n"));
} else if(op == PM_OP_UPGRADE) { } else if(op == PM_OP_UPGRADE) {
printf("%s: %s {-U --upgrade} [%s] <%s>\n", str_usg, myname, str_opt, str_file); 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) { } else if(op == PM_OP_QUERY) {
printf("%s: %s {-Q --query} [%s] [%s]\n", str_usg, myname, str_opt, str_pkg); printf("%s: %s {-Q --query} [%s] [%s]\n", str_usg, myname, str_opt, str_pkg);
printf("%s:\n", str_opt); printf("\n");
addlist(_(" -c, --changelog view the changelog of a package\n")); printf("%s:\n", str_mopt);
addlist(_(" -d, --deps list packages installed as dependencies [filter]\n")); printf(_(" -c, --changelog view the changelog of a package\n"));
addlist(_(" -e, --explicit list packages explicitly installed [filter]\n")); printf(_(" -d, --deps list packages installed as dependencies [filter]\n"));
addlist(_(" -g, --groups view all members of a package group\n")); printf(_(" -e, --explicit list packages explicitly installed [filter]\n"));
addlist(_(" -i, --info view package information (-ii for backup files)\n")); printf(_(" -g, --groups view all members of a package group\n"));
addlist(_(" -k, --check check that package files exist (-kk for file properties)\n")); printf(_(" -i, --info view package information (-ii for backup files)\n"));
addlist(_(" -l, --list list the files owned by the queried package\n")); printf(_(" -k, --check check that package files exist (-kk for file properties)\n"));
addlist(_(" -m, --foreign list installed packages not found in sync db(s) [filter]\n")); printf(_(" -l, --list list the files owned by the queried package\n"));
addlist(_(" -n, --native list installed packages only found in sync db(s) [filter]\n")); printf(_(" -m, --foreign list installed packages not found in sync db(s) [filter]\n"));
addlist(_(" -o, --owns <file> query the package that owns <file>\n")); printf(_(" -n, --native list installed packages only found in sync db(s) [filter]\n"));
addlist(_(" -p, --file <package> query a package file instead of the database\n")); printf(_(" -o, --owns <file> query the package that owns <file>\n"));
addlist(_(" -q, --quiet show less information for query and search\n")); printf(_(" -p, --file <package> query a package file instead of the database\n"));
addlist(_(" -s, --search <regex> search locally-installed packages for matching strings\n")); printf(_(" -q, --quiet show less information for query and search\n"));
addlist(_(" -t, --unrequired list packages not (optionally) required by any\n" printf(_(" -s, --search <regex> 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")); " package (-tt to ignore optdepends) [filter]\n"));
addlist(_(" -u, --upgrades list outdated packages [filter]\n")); printf(_(" -u, --upgrades list outdated packages [filter]\n"));
} else if(op == PM_OP_SYNC) { } else if(op == PM_OP_SYNC) {
printf("%s: %s {-S --sync} [%s] [%s]\n", str_usg, myname, str_opt, str_pkg); printf("%s: %s {-S --sync} [%s] [%s]\n", str_usg, myname, str_opt, str_pkg);
printf("%s:\n", str_opt); printf("\n");
addlist(_(" -c, --clean remove old packages from cache directory (-cc for all)\n")); printf("%s:\n", str_mopt);
addlist(_(" -g, --groups view all members of a package group\n" 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")); " (-gg to view all groups and members)\n"));
addlist(_(" -i, --info view package information (-ii for extended information)\n")); printf(_(" -i, --info view package information (-ii for extended information)\n"));
addlist(_(" -l, --list <repo> view a list of packages in a repo\n")); printf(_(" -l, --list <repo> view a list of packages in a repo\n"));
addlist(_(" -q, --quiet show less information for query and search\n")); printf(_(" -q, --quiet show less information for query and search\n"));
addlist(_(" -s, --search <regex> search remote repositories for matching strings\n")); printf(_(" -s, --search <regex> search remote repositories for matching strings\n"));
addlist(_(" -u, --sysupgrade upgrade installed packages (-uu enables downgrades)\n")); printf(_(" -u, --sysupgrade upgrade installed packages (-uu enables downgrades)\n"));
addlist(_(" -y, --refresh download fresh package databases from the server\n" printf(_(" -y, --refresh download fresh package databases from the server\n"
" (-yy to force a refresh even if up to date)\n")); " (-yy to force a refresh even if up to date)\n"));
addlist(_(" --needed do not reinstall up to date packages\n"));
} else if(op == PM_OP_DATABASE) { } else if(op == PM_OP_DATABASE) {
printf("%s: %s {-D --database} <%s> <%s>\n", str_usg, myname, str_opt, str_pkg); printf("%s: %s {-D --database} <%s> <%s>\n", str_usg, myname, str_opt, str_pkg);
printf("%s:\n", str_opt); printf("\n");
addlist(_(" --asdeps mark packages as non-explicitly installed\n")); printf("%s:\n", str_mopt);
addlist(_(" --asexplicit mark packages as explicitly installed\n")); printf(_(" --asdeps mark packages as non-explicitly installed\n"));
addlist(_(" -k, --check test local database for validity (-kk for sync databases)\n")); printf(_(" --asexplicit mark packages as explicitly installed\n"));
addlist(_(" -q, --quiet suppress output of success messages\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) { } else if(op == PM_OP_DEPTEST) {
printf("%s: %s {-T --deptest} [%s] [%s]\n", str_usg, myname, str_opt, str_pkg); 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) { } else if(op == PM_OP_FILES) {
printf("%s: %s {-F --files} [%s] [%s]\n", str_usg, myname, str_opt, str_file); printf("%s: %s {-F --files} [%s] [%s]\n", str_usg, myname, str_opt, str_file);
printf("%s:\n", str_opt); printf("\n");
addlist(_(" -l, --list list the files owned by the queried package\n")); printf("%s:\n", str_mopt);
addlist(_(" -q, --quiet show less information for query and search\n")); printf(_(" -y, --refresh download fresh package databases from the server\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")); " (-yy to force a refresh even if up to date)\n"));
addlist(_(" --machinereadable\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")); " produce machine-readable output\n"));
} }
switch(op) { switch(op) {
case PM_OP_SYNC: case PM_OP_SYNC:
case PM_OP_UPGRADE: case PM_OP_UPGRADE:
addlist(_(" -w, --downloadonly download packages but do not install/upgrade anything\n")); printf("\n");
addlist(_(" --overwrite <glob>\n" printf(_("upgrade options:\n"));
" overwrite conflicting files (can be used more than once)\n")); printf(_(" -w, --downloadonly download packages but do not install/upgrade anything\n"));
addlist(_(" --asdeps install packages as non-explicitly installed\n")); printf(_(" --asdeps install packages as non-explicitly installed\n"));
addlist(_(" --asexplicit install packages as explicitly installed\n")); printf(_(" --asexplicit install packages as explicitly installed\n"));
addlist(_(" --ignore <pkg> ignore a package upgrade (can be used more than once)\n")); printf(_(" --ignore <pkg> ignore a package upgrade (can be used more than once)\n"));
addlist(_(" --ignoregroup <grp>\n" printf(_(" --ignoregroup <grp>\n"
" ignore a group upgrade (can be used more than once)\n")); " ignore a group upgrade (can be used more than once)\n"));
printf(_(" --needed do not reinstall up to date packages\n"));
printf(_(" --overwrite <glob>\n"
" overwrite conflicting files (can be used more than once)\n"));
__attribute__((fallthrough)); __attribute__((fallthrough));
case PM_OP_REMOVE: case PM_OP_REMOVE:
addlist(_(" -d, --nodeps skip dependency version checks (-dd to skip all checks)\n")); printf("\n");
addlist(_(" --assume-installed <package=version>\n" printf(_("transaction options:\n"));
printf(_(" -d, --nodeps skip dependency version checks (-dd to skip all checks)\n"));
printf(_(" --assume-installed <package=version>\n"
" add a virtual package to satisfy dependencies\n")); " add a virtual package to satisfy dependencies\n"));
addlist(_(" --dbonly only modify database entries, not package files\n")); printf(_(" --dbonly only modify database entries, not package files\n"));
addlist(_(" --noprogressbar do not show a progress bar when downloading files\n")); printf(_(" --noprogressbar do not show a progress bar when downloading files\n"));
addlist(_(" --noscriptlet do not execute the install scriptlet if one exists\n")); printf(_(" --noscriptlet do not execute the install scriptlet if one exists\n"));
addlist(_(" -p, --print print the targets instead of performing the operation\n")); printf(_(" -p, --print print the targets instead of performing the operation\n"));
addlist(_(" --print-format <string>\n" printf(_(" --print-format <string>\n"
" specify how the targets should be printed\n")); " specify how the targets should be printed\n"));
break; break;
} }
addlist(_(" -b, --dbpath <path> set an alternate database location\n")); printf("\n");
addlist(_(" -r, --root <path> set an alternate installation root\n")); printf(_("general options:\n"));
addlist(_(" -v, --verbose be verbose\n")); printf(_(" -b, --dbpath <path> set an alternate database location\n"));
addlist(_(" --arch <arch> set an alternate architecture\n")); printf(_(" -r, --root <path> set an alternate installation root\n"));
addlist(_(" --sysroot operate on a mounted guest system (root-only)\n")); printf(_(" -v, --verbose be verbose\n"));
addlist(_(" --cachedir <dir> set an alternate package cache location\n")); printf(_(" --arch <arch> set an alternate architecture\n"));
addlist(_(" --hookdir <dir> set an alternate hook location\n")); printf(_(" --cachedir <dir> set an alternate package cache location\n"));
addlist(_(" --color <when> colorize the output\n")); printf(_(" --color <when> colorize the output\n"));
addlist(_(" --config <path> set an alternate configuration file\n")); printf(_(" --config <path> set an alternate configuration file\n"));
addlist(_(" --debug display debug messages\n")); printf(_(" --debug display debug messages\n"));
addlist(_(" --gpgdir <path> set an alternate home directory for GnuPG\n")); printf(_(" --gpgdir <path> set an alternate home directory for GnuPG\n"));
addlist(_(" --logfile <path> set an alternate log file\n")); printf(_(" --hookdir <dir> set an alternate hook location\n"));
addlist(_(" --noconfirm do not ask for any confirmation\n")); printf(_(" --logfile <path> set an alternate log file\n"));
addlist(_(" --confirm always ask for confirmation\n")); printf(_(" --noconfirm do not ask for any confirmation\n"));
addlist(_(" --disable-download-timeout\n" printf(_(" --confirm always ask for confirmation\n"));
printf(_(" --disable-download-timeout\n"
" use relaxed timeouts for download\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. /** Output pacman version and copyright.