[Debtags-commits] [svn] r1634 - in tagcoll/trunk: . doc tagcoll tools

Enrico Zini enrico at costa.debian.org
Sat Mar 4 17:47:33 UTC 2006


Author: enrico
Date: Sat Mar  4 17:47:32 2006
New Revision: 1634

Removed:
   tagcoll/trunk/taggrep.1
Modified:
   tagcoll/trunk/Makefile.am
   tagcoll/trunk/doc/Makefile.am
   tagcoll/trunk/tagcoll/Commandline.cc
   tagcoll/trunk/tagcoll/Commandline.h
   tagcoll/trunk/tools/Makefile.am
   tagcoll/trunk/tools/TagcollOptions.h
   tagcoll/trunk/tools/manpage.cc
   tagcoll/trunk/tools/tagcoll.cc
   tagcoll/trunk/tools/taggrep.cc
Log:
Implemented manpage generator for OptionParsers as well
Converted taggrep to the new commandline parser


Modified: tagcoll/trunk/Makefile.am
==============================================================================
--- tagcoll/trunk/Makefile.am	(original)
+++ tagcoll/trunk/Makefile.am	Sat Mar  4 17:47:32 2006
@@ -8,9 +8,15 @@
 m4dir = $(datadir)/aclocal
 m4_DATA = libtagcoll.m4
 
-man_MANS = tagcoll.1 taggrep.1
+man_MANS = tagcoll.1 taggrep.1 tagidx.1
 
 tagcoll.1: tools/manpage doc/tagcoll-man-hooks
 	tools/manpage tagcoll doc/tagcoll-man-hooks > $@ || rm $@
 
+taggrep.1: tools/manpage doc/taggrep-man-hooks
+	tools/manpage taggrep doc/taggrep-man-hooks > $@ || rm $@
+
+tagidx.1: tools/manpage doc/tagidx-man-hooks
+	tools/manpage tagidx doc/tagidx-man-hooks > $@ || rm $@
+
 EXTRA_DIST = libtagcoll.m4 libtagcoll.pc.in COPYING.libtagcoll DONE 

Modified: tagcoll/trunk/doc/Makefile.am
==============================================================================
--- tagcoll/trunk/doc/Makefile.am	(original)
+++ tagcoll/trunk/doc/Makefile.am	Sat Mar  4 17:47:32 2006
@@ -36,7 +36,7 @@
 	touch $@
 endif
 
-EXTRA_DIST = libtagcoll.dox tagbk-draft.tex local.bib tagcoll-man-hooks
+EXTRA_DIST = libtagcoll.dox tagbk-draft.tex local.bib tagcoll-man-hooks taggrep-man-hooks tagidx-man-hooks
 
 CLEANFILES = libtagcoll.doxytags $(PAPER).aux $(PAPER).bbl $(PAPER).blg $(PAPER).ps $(PAPER).pdf $(PAPER).dvi $(PAPER).idx $(PAPER).log 
 

Modified: tagcoll/trunk/tagcoll/Commandline.cc
==============================================================================
--- tagcoll/trunk/tagcoll/Commandline.cc	(original)
+++ tagcoll/trunk/tagcoll/Commandline.cc	Sat Mar  4 17:47:32 2006
@@ -648,7 +648,7 @@
 	endSection(out);
 
 	startSection(out, "SYNOPSIS");
-	out << m_app << " [options] " << cp.usage << endl;
+	out << "\\fB" << cp.name() << "\\fP [options] " << cp.usage << endl;
 	endSection(out);
 
 	startSection(out, "DESCRIPTION");
@@ -726,6 +726,55 @@
 	endSection(out);
 }
 
+void Manpage::output(std::ostream& out, const OptionParser& o, int section)
+{
+	// Manpage header
+	out << ".TH " << toupper(m_app) << " " << section << " \"" << man_date() << "\" \"" << m_ver << "\"" << endl;
+
+	startSection(out, "NAME");
+	out << o.name() << " \\- " << o.description << endl;
+	endSection(out);
+
+	startSection(out, "SYNOPSIS");
+	out << "\\fB" << m_app << "\\fP [options] " << o.usage << endl;
+	endSection(out);
+
+	startSection(out, "DESCRIPTION");
+	if (!o.longDescription.empty())
+		outputParagraph(out, o.longDescription);
+	endSection(out);
+
+	startSection(out, "OPTIONS");
+	out << "This program follows the usual GNU command line syntax, with long options starting with two dashes (`\\-')." << endl << endl;
+
+	// Harvest merged option informations
+	for (vector<OptionGroup*>::const_iterator i = o.groups().begin();
+			i != o.groups().end(); i++)
+	{
+		if (!(*i)->description.empty())
+			out << endl << (*i)->description << ":" << endl;
+		for (vector<Option*>::const_iterator j = (*i)->options.begin();
+				j != (*i)->options.end(); ++j)
+			outputOption(out, *j);
+		out << ".PP" << endl;
+	}
+
+	if (!o.options().empty())
+	{
+		out << endl;
+		out << "Other options:" << endl;
+		for (vector<Option*>::const_iterator j = o.options().begin();
+				j != o.options().end(); ++j)
+			outputOption(out, *j);
+	}
+	endSection(out);
+
+	startSection(out, "AUTHOR");
+	out << "\\fB" << o.name() << "\\fP is maintained by " << PACKAGE_BUGREPORT << "." << endl << endl;
+	out << "This manpage has been automatically generated by the " << m_app << " program." << endl;
+	endSection(out);
+}
+
 static string readline(FILE* in)
 {
 	string res;

Modified: tagcoll/trunk/tagcoll/Commandline.h
==============================================================================
--- tagcoll/trunk/tagcoll/Commandline.h	(original)
+++ tagcoll/trunk/tagcoll/Commandline.h	Sat Mar  4 17:47:32 2006
@@ -267,6 +267,40 @@
 	std::string longDescription;
 };
 
+/**
+ * Main parser for commandline arguments.
+ *
+ * It is implemented as a template, to allow to base it on top of any available
+ * commandline parser.
+ */
+template<class Base>
+class MainParser : public Base
+{
+	arglist args;
+
+public:
+	MainParser(const std::string& name) : Base(name) {}
+
+	arglist parse(int argc, const char* argv[])
+	{
+		for (int i = 1; i < argc; i++)
+			args.push_back(argv[i]);
+		this->parseList(args);
+		return args;
+	}
+
+	bool hasNext() const { return !args.empty(); }
+
+	std::string next()
+	{
+		if (args.empty())
+			return std::string();
+		std::string res(*args.begin());
+		args.erase(args.begin());
+		return res;
+	}
+};
+
 class DocMaker
 {
 protected:
@@ -326,7 +360,7 @@
 	void readHooks(const std::string& file);
 
 	void output(std::ostream& out, const CommandParser& cp, int section);
-	//void outputMan(std::ostream& out, const OptionParser& cp, int section);
+	void output(std::ostream& out, const OptionParser& cp, int section);
 };
 
 }

Modified: tagcoll/trunk/tools/Makefile.am
==============================================================================
--- tagcoll/trunk/tools/Makefile.am	(original)
+++ tagcoll/trunk/tools/Makefile.am	Sat Mar  4 17:47:32 2006
@@ -17,4 +17,4 @@
 
 INCLUDES = -I$(top_srcdir)
 
-EXTRA_DIST = CommandlineParser.h
+EXTRA_DIST = CommandlineParser.h TagcollOptions.h TaggrepOptions.h TagidxOptions.h

Modified: tagcoll/trunk/tools/TagcollOptions.h
==============================================================================
--- tagcoll/trunk/tools/TagcollOptions.h	(original)
+++ tagcoll/trunk/tools/TagcollOptions.h	Sat Mar  4 17:47:32 2006
@@ -26,7 +26,7 @@
 namespace Tagcoll {
 namespace commandline {
 
-struct TagcollOptions : public CommandParser
+struct TagcollOptions : public MainParser<CommandParser>
 {
 	struct HelpGroup : public OptionGroup
 	{
@@ -44,7 +44,7 @@
 		}
 		~HelpGroup()
 		{
-			delete help;
+			delete help; delete version;
 		}
 	} helpGroup;
 
@@ -175,6 +175,8 @@
 				"associating to each tag the list of items associated to it in the input.\n"
 				"The --untagged-tag switch can be used to provide a name to which untagged "
 				"items will be associated in the output.";
+			add(&cp->inputGroup);
+			add(&cp->outputGroup);
 			add(&cp->helpGroup);
 		}
 		~Reverse()
@@ -214,6 +216,7 @@
 				"items shold be from the item(s) specified.";
 			examples = "tagcoll related mutt,mozilla-browser -";
 
+			add(&cp->inputGroup);
 			add(&cp->helpGroup);
 		}
 		~Related()
@@ -240,6 +243,7 @@
 				"ada:devel,languages\n"
 				"apachemodules:net,servers,web\n"
 				"browsers:net,web\n";
+			add(&cp->inputGroup);
 			add(&cp->helpGroup);
 		}
 	} implications;
@@ -260,6 +264,7 @@
 			examples =
 				"/net/clients/mail: mutt\n"
 				"/net/filters/mail: procmail\n";
+			add(&cp->inputGroup);
 			add(&cp->hierarchyGroup);
 			add(&cp->helpGroup);
 		}
@@ -274,6 +279,7 @@
 			longDescription =
 				"Like hiearchy, but in every node it merges tags which are attached to the "
 				"same set of items.";
+			add(&cp->inputGroup);
 			add(&cp->hierarchyGroup);
 			add(&cp->helpGroup);
 		}
@@ -287,6 +293,7 @@
 				"generate a smart hierarchy and print, for each toplevel tag, "
 				"what are the items that make it toplevel instead of going below "
 				"another tag";
+			add(&cp->inputGroup);
 			add(&cp->hierarchyGroup);
 			add(&cp->helpGroup);
 		}
@@ -297,6 +304,8 @@
 		{
 			usage = "<expression> [files...]";
 			description = "output the collection of tags that match the given tag expression";
+			add(&cp->inputGroup);
+			add(&cp->outputGroup);
 			add(&cp->helpGroup);
 		}
 	} grep;
@@ -306,13 +315,12 @@
 		{
 			usage = "[files...]";
 			description = "output only the items of the input collection";
+			add(&cp->inputGroup);
 			add(&cp->helpGroup);
 		}
 	} items;
 
-	arglist args;
-
-	TagcollOptions() : CommandParser("tagcoll"),
+	TagcollOptions() : MainParser<CommandParser>("tagcoll"),
 		generic(this),
 		help(this), copy(this), reverse(this), diff(this), related(this), implications(this),
 		hierarchy(this), cleanhierarchy(this), findspecials(this), grep(this), items(this)
@@ -334,26 +342,6 @@
 		description = "Perform various operations on a tagged collection";
 	}
 
-	arglist parse(int argc, const char* argv[])
-	{
-		for (int i = 1; i < argc; i++)
-			args.push_back(argv[i]);
-		parseList(args);
-		if (!lastCommand())
-			throw commandline::BadOption("could not understand the command to execute");
-		return args;
-	}
-
-	bool hasNext() const { return !args.empty(); }
-	std::string next()
-	{
-		if (args.empty())
-			return std::string();
-		std::string res(*args.begin());
-		args.erase(args.begin());
-		return res;
-	}
-
 #if 0
 		//opts.add("verbose", 'v', "verbose", "enable verbose output");
 		//opts.add("debug", 0, "debug", "enable debugging output (including verbose output)");

Modified: tagcoll/trunk/tools/manpage.cc
==============================================================================
--- tagcoll/trunk/tools/manpage.cc	(original)
+++ tagcoll/trunk/tools/manpage.cc	Sat Mar  4 17:47:32 2006
@@ -20,6 +20,8 @@
 
 #include <config.h>
 #include "TagcollOptions.h"
+#include "TaggrepOptions.h"
+#include "TagidxOptions.h"
 #include <iostream>
 
 using namespace std;
@@ -40,7 +42,22 @@
 			commandline::Manpage help("tagcoll", VERSION);
 			if (!hooks.empty())
 				help.readHooks(hooks);
-
+			help.output(cout, opts, 1);
+		}
+		else if (cmd == "taggrep")
+		{
+			commandline::TaggrepOptions opts;
+			commandline::Manpage help("taggrep", VERSION);
+			if (!hooks.empty())
+				help.readHooks(hooks);
+			help.output(cout, opts, 1);
+		}
+		else if (cmd == "tagidx")
+		{
+			commandline::TagcollOptions opts;
+			commandline::Manpage help("tagidx", VERSION);
+			if (!hooks.empty())
+				help.readHooks(hooks);
 			help.output(cout, opts, 1);
 		}
 		else

Modified: tagcoll/trunk/tools/tagcoll.cc
==============================================================================
--- tagcoll/trunk/tools/tagcoll.cc	(original)
+++ tagcoll/trunk/tools/tagcoll.cc	Sat Mar  4 17:47:32 2006
@@ -452,6 +452,8 @@
 
 	try {
 		opts.parse(argc, argv);
+		if (!opts.lastCommand())
+			throw commandline::BadOption("could not understand the command to execute");
 
 		Reader reader(opts);
 		

Modified: tagcoll/trunk/tools/taggrep.cc
==============================================================================
--- tagcoll/trunk/tools/taggrep.cc	(original)
+++ tagcoll/trunk/tools/taggrep.cc	Sat Mar  4 17:47:32 2006
@@ -27,7 +27,7 @@
 #define VERSION "unknown"
 #endif
 
-#include "CommandlineParser.h"
+#include "TaggrepOptions.h"
 
 #include <stdio.h>
 
@@ -43,6 +43,7 @@
 #include <tagcoll/Expression.h>
 
 #include <algorithm>
+#include <iostream>
 
 using namespace std;
 using namespace Tagcoll;
@@ -64,87 +65,61 @@
 	}
 }
 
-class CommandlineArgs
-{
-protected:
-	int argc;
-	const char** argv;
-	int _next;
-
-public:
-	CommandlineArgs(int argc, const char* argv[]) throw () : argc(argc), argv(argv), _next(1) {}
-
-	// Return true if there is another argument left in the list
-	bool hasNext() const throw () { return argc >= _next + 1; }
-
-	// Return the next argument in the list
-	string next() throw ()
-	{
-		if (hasNext())
-		{
-			return argv[_next++];
-		} else {
-			return "-";
-		}
-	}
-};
-
 int main(int argc, const char* argv[])
 {
+	commandline::TaggrepOptions opts;
+
 	try {
-		CommandlineParser opts(APPNAME, "[options] <tag-expression> [file1 [file2 [...]]]",
-				"Filter the contents of a tagged collection\n");
-		opts.add("version", 0, "version", "print the program version, then exit");
-		opts.add("invert-match", 'v', "invert-match", "invert the sense of matching, to select non-matching lines");
-		opts.add("quiet", 'q', "quiet", "do not write anything to standard output, but exit with 0 if any match is found");
+		opts.parse(argc, argv);
 
-		// Process the commandline
-		if (!opts.parse(argc, argv) || argc == 1)
+		if (opts.helpGroup.help->boolValue())
 		{
-			opts.printHelp();
-			return 1;
+			// Provide help as requested
+			commandline::Help help("taggrep", VERSION);
+			help.outputHelp(cout, opts);
 		}
-		if (opts.get("help").defined())
+		else if (opts.helpGroup.version->boolValue())
 		{
-			opts.printHelp();
-			return 0;
+			// Print the program version
+			commandline::Help help("taggrep", VERSION);
+			help.outputVersion(cout);
 		}
-		if (opts.get("version").defined())
+		else
 		{
-			printf("%s ver." VERSION "\n", APPNAME);
-			return 0;
-		}
+			string expression = opts.next();
 
-		CommandlineArgs args(argc, argv);
-		
-		string expression = args.next();
-
-		FilterChain<string, string> filters;
-
-		FilterItemsByExpression<string, string> filter(expression);
-		filters.appendFilter(filter);
-
-		if (opts.get("invert-match").defined())
-			filter.setMatchType(FilterItemsByExpression<string, string>::INVERTED);
-
-		TrivialConverter<string, string> conv;
-		Sink<string, string> sink;
-		TextFormat<string, string> output(conv, conv, stdout);
-		if (opts.get("quiet").defined())
-			filters.setConsumer(sink);
-		else
-			filters.setConsumer(output);
+			FilterChain<string, string> filters;
 
-		do
-		{
-			string file = args.next();
-			readCollection(file, filters);
-		}
-		while (args.hasNext());
+			FilterItemsByExpression<string, string> filter(expression);
+			filters.appendFilter(filter);
 
-		return filter.countMatched() > 0 ? 0 : 1;
-	}
-	catch (Exception& e)
+			if (opts.invert->boolValue())
+				filter.setMatchType(FilterItemsByExpression<string, string>::INVERTED);
+
+			TrivialConverter<string, string> conv;
+			Sink<string, string> sink;
+			TextFormat<string, string> output(conv, conv, stdout);
+			if (opts.quiet->boolValue())
+				filters.setConsumer(sink);
+			else
+				filters.setConsumer(output);
+
+			do
+			{
+				string file = opts.next();
+				readCollection(file, filters);
+			}
+			while (opts.hasNext());
+
+			return filter.countMatched() > 0 ? 0 : 1;
+		}
+		return 0;
+	} catch (commandline::BadOption& e) {
+		cerr << e.desc() << endl;
+		commandline::Help help(APPNAME, VERSION);
+		help.outputHelp(cerr, opts);
+		exit(1);
+	} catch (Exception& e)
 	{
 		fprintf(stderr, "%s: %.*s\n", e.type(), PFSTR(e.desc()));
 		return 1;



More information about the Debtags-commits mailing list