Skip to content

Commit

Permalink
bin/xbps-query: allow PKG argument to appear anywhere in argv
Browse files Browse the repository at this point in the history
this lets things be a bit more flexible for users, for example:
xbps-query -Rx --fulldeptree foo is now allowed
  • Loading branch information
classabbyamp committed Feb 27, 2024
1 parent e82437f commit 41c6620
Showing 1 changed file with 36 additions and 20 deletions.
56 changes: 36 additions & 20 deletions bin/xbps-query/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ usage(bool fail)
int
main(int argc, char **argv)
{
const char *shortopts = "C:c:df:hHiLlMmOo:p:Rr:s:S:VvX:x:";
const char *shortopts = "C:c:dfhHiLlMmOop:Rr:sSVvXx";
const struct option longopts[] = {
{ "config", required_argument, NULL, 'C' },
{ "cachedir", required_argument, NULL, 'c' },
Expand All @@ -91,17 +91,17 @@ main(int argc, char **argv)
{ "memory-sync", no_argument, NULL, 'M' },
{ "list-manual-pkgs", no_argument, NULL, 'm' },
{ "list-orphans", no_argument, NULL, 'O' },
{ "ownedby", required_argument, NULL, 'o' },
{ "ownedby", no_argument, NULL, 'o' },
{ "property", required_argument, NULL, 'p' },
{ "repository", optional_argument, NULL, 'R' },
{ "rootdir", required_argument, NULL, 'r' },
{ "show", required_argument, NULL, 'S' },
{ "search", required_argument, NULL, 's' },
{ "show", no_argument, NULL, 'S' },
{ "search", no_argument, NULL, 's' },
{ "version", no_argument, NULL, 'V' },
{ "verbose", no_argument, NULL, 'v' },
{ "files", required_argument, NULL, 'f' },
{ "deps", required_argument, NULL, 'x' },
{ "revdeps", required_argument, NULL, 'X' },
{ "files", no_argument, NULL, 'f' },
{ "deps", no_argument, NULL, 'x' },
{ "revdeps", no_argument, NULL, 'X' },
{ "regex", no_argument, NULL, 0 },
{ "fulldeptree", no_argument, NULL, 1 },
{ "cat", required_argument, NULL, 2 },
Expand Down Expand Up @@ -135,7 +135,6 @@ main(int argc, char **argv)
flags |= XBPS_FLAG_DEBUG;
break;
case 'f':
pkg = optarg;
show_files = opmode = true;
break;
case 'H':
Expand Down Expand Up @@ -163,7 +162,6 @@ main(int argc, char **argv)
orphans = opmode = true;
break;
case 'o':
pkg = optarg;
own = opmode = true;
break;
case 'p':
Expand All @@ -180,11 +178,9 @@ main(int argc, char **argv)
rootdir = optarg;
break;
case 'S':
pkg = optarg;
show = opmode = true;
break;
case 's':
pkg = optarg;
pkg_search = opmode = true;
break;
case 'v':
Expand All @@ -194,11 +190,9 @@ main(int argc, char **argv)
printf("%s\n", XBPS_RELVER);
exit(EXIT_SUCCESS);
case 'x':
pkg = optarg;
show_deps = opmode = true;
break;
case 'X':
pkg = optarg;
show_rdeps = opmode = true;
break;
case 0:
Expand All @@ -222,20 +216,42 @@ main(int argc, char **argv)
argc -= optind;
argv += optind;

if (!argc && !opmode) {
usage(true);
/* NOTREACHED */
} else if (!opmode) {
if (!opmode) {
if (argc) {
/* show mode by default */
show = opmode = true;
pkg = *(argv++);
argc--;
} else {
/* no arguments */
usage(true);
/* NOTREACHED */
}
}

if (own || pkg_search || catfile || show || show_prop ||
show_files || show_deps || show_rdeps) {
/* modes that require a PKG argument */
if (argc) {
pkg = *(argv++);
argc--;
} else {
xbps_error_printf("xbps-query: missing required argument PKG\n");
exit(EXIT_FAILURE);
/* NOTREACHED */
}
}

if (argc) {
/* trailing parameters */
usage(true);
xbps_error_printf("xbps-query: too many arguments: ");
while (argc) {
printf("%s ", *(argv++));
argc--;
}
printf("\n");
exit(EXIT_FAILURE);
/* NOTREACHED */
}

/*
* Initialize libxbps.
*/
Expand Down

0 comments on commit 41c6620

Please sign in to comment.