Jerabek Vladimir
2019-Nov-14 16:20 UTC
[PATCH 1/2] quest: Add support for quiet mode and data/ids suppression
This is especially useful when output of quest is further processed.
---
xapian-core/examples/quest.cc | 51 ++++++++++++++++++++++++++++-------
1 file changed, 42 insertions(+), 9 deletions(-)
diff --git a/xapian-core/examples/quest.cc b/xapian-core/examples/quest.cc
index ef2fb53a2223..9953861d8ce9 100644
--- a/xapian-core/examples/quest.cc
+++ b/xapian-core/examples/quest.cc
@@ -195,6 +195,9 @@ static void show_usage() {
pos += len + 2;
}
cout << "\n"
+" -i, --no-ids suppress output of document IDs and
weights\n"
+" -x, --no-data suppress output of documents from
output\n"
+" -q, --quiet output only matched documents without
headers\n"
" -h, --help display this help and exit\n"
" -v, --version output version information and
exit\n";
}
@@ -235,7 +238,7 @@ decode_wt(const char * s)
int
main(int argc, char **argv)
try {
- const char * opts = "d:m:c:s:p:b:f:o:w:hv";
+ const char * opts = "d:m:c:s:p:b:f:o:w:ixqhv";
static const struct option long_opts[] = {
{ "db", required_argument, 0, 'd' },
{ "msize", required_argument, 0, 'm' },
@@ -246,6 +249,9 @@ try {
{ "flags", required_argument, 0, 'f' },
{ "default-op", required_argument, 0, 'o' },
{ "weight", required_argument, 0, 'w' },
+ { "no-ids", no_argument, 0, 'i' },
+ { "no-data", no_argument, 0, 'x' },
+ { "quiet", no_argument, 0, 'q' },
{ "help", no_argument, 0, 'h' },
{ "version", no_argument, 0, 'v' },
{ NULL, 0, 0, 0}
@@ -263,6 +269,10 @@ try {
unsigned flags = parser.FLAG_DEFAULT|parser.FLAG_SPELLING_CORRECTION;
int weight = -1;
+ bool no_ids = false;
+ bool no_data = false;
+ bool quiet = false;
+
int c;
while ((c = gnu_getopt_long(argc, argv, opts, long_opts, 0)) != -1) {
switch (c) {
@@ -349,6 +359,18 @@ try {
}
break;
}
+ case 'i': {
+ no_ids = true;
+ break;
+ }
+ case 'x': {
+ no_data = true;
+ break;
+ }
+ case 'q': {
+ quiet = true;
+ break;
+ }
case 'v':
cout << PROG_NAME " - " PACKAGE_STRING << endl;
exit(0);
@@ -378,7 +400,9 @@ try {
if (!correction.empty())
cout << "Did you mean: " << correction <<
"\n\n";
- cout << "Parsed Query: " << query.get_description()
<< endl;
+ if (!quiet) {
+ cout << "Parsed Query: " << query.get_description()
<< endl;
+ }
if (!have_database) {
cout << "No database specified so not running the query."
<< endl;
@@ -441,18 +465,27 @@ try {
auto lower_bound = mset.get_matches_lower_bound();
auto estimate = mset.get_matches_estimated();
auto upper_bound = mset.get_matches_upper_bound();
- if (lower_bound == upper_bound) {
- cout << "Exactly " << estimate << "
matches" << endl;
- } else {
- cout << "Between " << lower_bound << " and
" << upper_bound
- << " matches, best estimate is " << estimate
<< endl;
+
+ if (!quiet) {
+ if (lower_bound == upper_bound) {
+ cout << "Exactly " << estimate << "
matches" << endl;
+ } else {
+ cout << "Between " << lower_bound << "
and " << upper_bound
+ << " matches, best estimate is " << estimate <<
endl;
+ }
+
+ cout << "MSet:" << endl;
}
- cout << "MSet:" << endl;
for (Xapian::MSetIterator i = mset.begin(); i != mset.end(); ++i) {
Xapian::Document doc = i.get_document();
string data = doc.get_data();
- cout << *i << ": [" << i.get_weight() <<
"]\n" << data << "\n";
+ if (!no_ids) {
+ cout << *i << ": [" << i.get_weight() <<
"]" << endl;
+ }
+ if (!no_data) {
+ cout << data << endl;
+ }
}
cout << flush;
} catch (const Xapian::QueryParserError & e) {
--
2.20.1
Jerabek Vladimir
2019-Nov-14 16:20 UTC
[PATCH 2/2] quest: Output errors and meta informations to stderr
---
xapian-core/examples/quest.cc | 18 ++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)
diff --git a/xapian-core/examples/quest.cc b/xapian-core/examples/quest.cc
index 9953861d8ce9..831d4a87c5cb 100644
--- a/xapian-core/examples/quest.cc
+++ b/xapian-core/examples/quest.cc
@@ -398,14 +398,14 @@ try {
Xapian::Query query = parser.parse_query(argv[optind], flags);
const string & correction = parser.get_corrected_query_string();
if (!correction.empty())
- cout << "Did you mean: " << correction <<
"\n\n";
+ cerr << "Did you mean: " << correction <<
"\n\n";
if (!quiet) {
- cout << "Parsed Query: " << query.get_description()
<< endl;
+ cerr << "Parsed Query: " << query.get_description()
<< endl;
}
if (!have_database) {
- cout << "No database specified so not running the query."
<< endl;
+ cerr << "No database specified so not running the query."
<< endl;
exit(0);
}
@@ -468,15 +468,17 @@ try {
if (!quiet) {
if (lower_bound == upper_bound) {
- cout << "Exactly " << estimate << "
matches" << endl;
+ cerr << "Exactly " << estimate << "
matches" << endl;
} else {
- cout << "Between " << lower_bound << "
and " << upper_bound
+ cerr << "Between " << lower_bound << "
and " << upper_bound
<< " matches, best estimate is " << estimate <<
endl;
}
- cout << "MSet:" << endl;
+ cerr << "MSet:" << endl;
}
+ cerr << flush;
+
for (Xapian::MSetIterator i = mset.begin(); i != mset.end(); ++i) {
Xapian::Document doc = i.get_document();
string data = doc.get_data();
@@ -489,9 +491,9 @@ try {
}
cout << flush;
} catch (const Xapian::QueryParserError & e) {
- cout << "Couldn't parse query: " << e.get_msg()
<< endl;
+ cerr << "Couldn't parse query: " << e.get_msg()
<< endl;
exit(1);
} catch (const Xapian::Error & err) {
- cout << err.get_description() << endl;
+ cerr << err.get_description() << endl;
exit(1);
}
--
2.20.1
Olly Betts
2019-Nov-28 08:29 UTC
[PATCH 2/2] quest: Output errors and meta informations to stderr
Thanks for the patches. On Thu, Nov 14, 2019 at 05:20:08PM +0100, Jerabek Vladimir wrote:> --- > xapian-core/examples/quest.cc | 18 ++++++++++-------- > 1 file changed, 10 insertions(+), 8 deletions(-) > > diff --git a/xapian-core/examples/quest.cc b/xapian-core/examples/quest.cc > index 9953861d8ce9..831d4a87c5cb 100644 > --- a/xapian-core/examples/quest.cc > +++ b/xapian-core/examples/quest.cc > @@ -398,14 +398,14 @@ try { > Xapian::Query query = parser.parse_query(argv[optind], flags); > const string & correction = parser.get_corrected_query_string(); > if (!correction.empty()) > - cout << "Did you mean: " << correction << "\n\n"; > + cerr << "Did you mean: " << correction << "\n\n"; > > if (!quiet) { > - cout << "Parsed Query: " << query.get_description() << endl; > + cerr << "Parsed Query: " << query.get_description() << endl; > } > > if (!have_database) { > - cout << "No database specified so not running the query." << endl; > + cerr << "No database specified so not running the query." << endl;I'm concerned this makes quest harder to use as a tool for manually trying out queries to debug problems, which is really its main purpose. Splitting the output over stdout and stderr would mean you could no longer just `quest ...|less` or `quest ... > quest.out`. At least extra tedious typing to redirect stderr to stdout would be needed, but you probably also need to worry about the relative ordering due to buffering. I take it your main aim here is to have a machine readable output, but there's a tension between supporting that and having a tool that's easy for humans to use with output that can evolve with time. I wonder if a separate tool which produces a machine readable format with an actual specification would be a better approach to this? If that's not it, what's the motivation behind these changes? Cheers, Olly