Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bin/xbps-query: improve argument parsing #588

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 43 additions & 26 deletions bin/xbps-query/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ usage(bool fail)
" -M, --memory-sync Remote repository data is fetched and stored\n"
" in memory, ignoring on-disk repodata archives\n"
" -p, --property PROP[,...] Show properties for PKGNAME\n"
" -R, --repository Enable repository mode. This mode explicitly\n"
" -R Enable repository mode. This mode explicitly\n"
" looks for packages in repositories\n"
" --repository=<url> Enable repository mode and add repository\n"
" --repository <url> Enable repository mode and add repository\n"
" to the top of the list. This option can be\n"
" specified multiple times\n"
" --regex Use Extended Regular Expressions to match\n"
Expand Down 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' },
{ "repository", required_argument, NULL, 4 },
{ "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,28 +162,22 @@ main(int argc, char **argv)
orphans = opmode = true;
break;
case 'o':
pkg = optarg;
own = opmode = true;
break;
case 'p':
props = optarg;
show_prop = true;
break;
case 'R':
if (optarg != NULL) {
xbps_repo_store(&xh, optarg);
}
repo_mode = true;
break;
case 'r':
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 +187,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 @@ -213,6 +204,10 @@ main(int argc, char **argv)
case 3:
list_repolock = opmode = true;
break;
case 4:
xbps_repo_store(&xh, optarg);
repo_mode = true;
break;
case '?':
default:
usage(true);
Expand All @@ -222,20 +217,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
26 changes: 13 additions & 13 deletions bin/xbps-query/xbps-query.1
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ section.
Enable repository mode.
This mode explicitly looks in repositories, rather
than looking in the target root directory.
.It Fl -repository=url
Appends the specified repository to the top of the list.
.It Fl -repository Ar url
Enables repository mode and adds the specified repository to the top of the list.
The
.Ar url
argument expects a URL to the repository for remote repositories or
Expand Down Expand Up @@ -190,7 +190,7 @@ Lists packages that are in repolock mode.
See the
.Em PACKAGE MODES
section for more information.
.It Fl o, Fl -ownedby Ar PATTERN [ Fl -repository ] [ Fl -regex ]
.It Fl o, Fl -ownedby Ar PATTERN [ Fl R ] [ Fl -regex ]
Search for installed package files by matching
.Ar PATTERN .
The
Expand All @@ -203,22 +203,22 @@ or an Extended Regular Expression as explained in
.Fl -regex
option is set).
If the
.Fl -repository
.Fl R
option is set, the matched
.Ar PATTERN
in repositories will be shown.
.It Fl S, Fl -show Ar PKG [ Fl -repository ] [ Fl -property Ar PROP ]
.It Fl S, Fl -show Ar PKG [ Fl R ] [ Fl -property Ar PROP ]
Shows information of an installed package.
This is the default mode if no other mode is set.
If the
.Fl -repository
.Fl R
option is set, the matched
.Ar PKG
in repositories will be shown.
If a package property is specified with
.Fl -property,
only that property will be shown.
.It Fl s, Fl -search Ar PATTERN [ Fl -repository ] [ Fl -property Ar PROP ] [ Fl -regex ]
.It Fl s, Fl -search Ar PATTERN [ Fl R ] [ Fl -property Ar PROP ] [ Fl -regex ]
Search for packages by matching
.Ar PATTERN
on its
Expand All @@ -238,30 +238,30 @@ all packages matching
against
.Ar PROP
will be shown.
.It Fl f, Fl -files Ar PKG [ Fl -repository ]
.It Fl f, Fl -files Ar PKG [ Fl R ]
Show the package files for
.Ar PKG .
If the
.Fl -repository
.Fl R
option is set, the matched
.Ar PKG
in repositories will be shown.
.It Fl x, Fl -deps Ar PKG [ Fl -repository ]
.It Fl x, Fl -deps Ar PKG [ Fl R ]
Show the required dependencies for
.Ar PKG .
Only direct dependencies are shown.
To see a full dependency tree, also set
.Fl -fulldeptree .
If the
.Fl -repository
.Fl R
option is set, the matched
.Ar PKG
in repositories will be shown.
.It Fl X, Fl -revdeps Ar PKG [ Fl -repository ]
.It Fl X, Fl -revdeps Ar PKG [ Fl R ]
Show the reverse dependencies for
.Ar PKG .
If the
.Fl -repository
.Fl R
option is set, the matched
.Ar PKG
in repositories will be shown.
Expand Down
2 changes: 1 addition & 1 deletion data/_xbps
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ _xbps_query() {
{-p,--property=-}'[Show properties]:property:($_xbps_properties)' \
--regex'[Use Extended Regular Expressions to match]' \
--fulldeptree'[Full dependency tree for -x/--deps]' \
{-R,--repository}'[Enable repository mode]' \
-R'[Enable repository mode]' \
'*'--repository=-'[Add repository to the top of the list]:repository url:_files -/' \
- '(mode)' \
{-l,--list-pkgs}'[List installed packages]' \
Expand Down
Loading