Skip to content

Commit 2488468

Browse files
classabbyampDuncaen
authored andcommitted
bin/xbps-query: allow PKG argument to appear anywhere in argv
this lets things be a bit more flexible for users, for example: xbps-query -Rx --fulldeptree foo is now allowed
1 parent 74f2a92 commit 2488468

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

bin/xbps-query/main.c

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ usage(bool fail)
7777
int
7878
main(int argc, char **argv)
7979
{
80-
const char *shortopts = "C:c:df:hHiLlMmOo:p:Rr:s:S:VvX:x:";
80+
const char *shortopts = "C:c:dfhHiLlMmOop:Rr:sSVvXx";
8181
const struct option longopts[] = {
8282
{ "config", required_argument, NULL, 'C' },
8383
{ "cachedir", required_argument, NULL, 'c' },
@@ -91,17 +91,17 @@ main(int argc, char **argv)
9191
{ "memory-sync", no_argument, NULL, 'M' },
9292
{ "list-manual-pkgs", no_argument, NULL, 'm' },
9393
{ "list-orphans", no_argument, NULL, 'O' },
94-
{ "ownedby", required_argument, NULL, 'o' },
94+
{ "ownedby", no_argument, NULL, 'o' },
9595
{ "property", required_argument, NULL, 'p' },
9696
{ "repository", optional_argument, NULL, 'R' },
9797
{ "rootdir", required_argument, NULL, 'r' },
98-
{ "show", required_argument, NULL, 'S' },
99-
{ "search", required_argument, NULL, 's' },
98+
{ "show", no_argument, NULL, 'S' },
99+
{ "search", no_argument, NULL, 's' },
100100
{ "version", no_argument, NULL, 'V' },
101101
{ "verbose", no_argument, NULL, 'v' },
102-
{ "files", required_argument, NULL, 'f' },
103-
{ "deps", required_argument, NULL, 'x' },
104-
{ "revdeps", required_argument, NULL, 'X' },
102+
{ "files", no_argument, NULL, 'f' },
103+
{ "deps", no_argument, NULL, 'x' },
104+
{ "revdeps", no_argument, NULL, 'X' },
105105
{ "regex", no_argument, NULL, 0 },
106106
{ "fulldeptree", no_argument, NULL, 1 },
107107
{ "cat", required_argument, NULL, 2 },
@@ -135,7 +135,6 @@ main(int argc, char **argv)
135135
flags |= XBPS_FLAG_DEBUG;
136136
break;
137137
case 'f':
138-
pkg = optarg;
139138
show_files = opmode = true;
140139
break;
141140
case 'H':
@@ -163,7 +162,6 @@ main(int argc, char **argv)
163162
orphans = opmode = true;
164163
break;
165164
case 'o':
166-
pkg = optarg;
167165
own = opmode = true;
168166
break;
169167
case 'p':
@@ -180,11 +178,9 @@ main(int argc, char **argv)
180178
rootdir = optarg;
181179
break;
182180
case 'S':
183-
pkg = optarg;
184181
show = opmode = true;
185182
break;
186183
case 's':
187-
pkg = optarg;
188184
pkg_search = opmode = true;
189185
break;
190186
case 'v':
@@ -194,11 +190,9 @@ main(int argc, char **argv)
194190
printf("%s\n", XBPS_RELVER);
195191
exit(EXIT_SUCCESS);
196192
case 'x':
197-
pkg = optarg;
198193
show_deps = opmode = true;
199194
break;
200195
case 'X':
201-
pkg = optarg;
202196
show_rdeps = opmode = true;
203197
break;
204198
case 0:
@@ -222,20 +216,42 @@ main(int argc, char **argv)
222216
argc -= optind;
223217
argv += optind;
224218

225-
if (!argc && !opmode) {
226-
usage(true);
227-
/* NOTREACHED */
228-
} else if (!opmode) {
219+
if (!opmode) {
220+
if (argc) {
229221
/* show mode by default */
230222
show = opmode = true;
231-
pkg = *(argv++);
232-
argc--;
223+
} else {
224+
/* no arguments */
225+
usage(true);
226+
/* NOTREACHED */
227+
}
228+
}
229+
230+
if (own || pkg_search || catfile || show || show_prop ||
231+
show_files || show_deps || show_rdeps) {
232+
/* modes that require a PKG argument */
233+
if (argc) {
234+
pkg = *(argv++);
235+
argc--;
236+
} else {
237+
xbps_error_printf("xbps-query: missing required argument PKG\n");
238+
exit(EXIT_FAILURE);
239+
/* NOTREACHED */
240+
}
233241
}
242+
234243
if (argc) {
235244
/* trailing parameters */
236-
usage(true);
245+
xbps_error_printf("xbps-query: too many arguments: ");
246+
while (argc) {
247+
printf("%s ", *(argv++));
248+
argc--;
249+
}
250+
printf("\n");
251+
exit(EXIT_FAILURE);
237252
/* NOTREACHED */
238253
}
254+
239255
/*
240256
* Initialize libxbps.
241257
*/

0 commit comments

Comments
 (0)