[PATCH 1/2] quest: Add support for quiet mode and data/ids suppression

Jerabek Vladimir vladimir.jerabek at fio.cz
Thu Nov 14 16:20:07 GMT 2019


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




More information about the Xapian-devel mailing list