diff options
Diffstat (limited to '0.8.0/paludis/args')
-rw-r--r-- | 0.8.0/paludis/args/Makefile.am | 53 | ||||
-rw-r--r-- | 0.8.0/paludis/args/args.cc | 27 | ||||
-rw-r--r-- | 0.8.0/paludis/args/args.hh | 35 | ||||
-rw-r--r-- | 0.8.0/paludis/args/args_TEST.cc | 176 | ||||
-rw-r--r-- | 0.8.0/paludis/args/args_dumper.cc | 81 | ||||
-rw-r--r-- | 0.8.0/paludis/args/args_dumper.hh | 80 | ||||
-rw-r--r-- | 0.8.0/paludis/args/args_error.cc | 45 | ||||
-rw-r--r-- | 0.8.0/paludis/args/args_error.hh | 87 | ||||
-rw-r--r-- | 0.8.0/paludis/args/args_group.cc | 78 | ||||
-rw-r--r-- | 0.8.0/paludis/args/args_group.hh | 117 | ||||
-rw-r--r-- | 0.8.0/paludis/args/args_handler.cc | 231 | ||||
-rw-r--r-- | 0.8.0/paludis/args/args_handler.hh | 181 | ||||
-rw-r--r-- | 0.8.0/paludis/args/args_option.cc | 226 | ||||
-rw-r--r-- | 0.8.0/paludis/args/args_option.hh | 361 | ||||
-rw-r--r-- | 0.8.0/paludis/args/args_visitor.cc | 97 | ||||
-rw-r--r-- | 0.8.0/paludis/args/args_visitor.hh | 100 | ||||
-rw-r--r-- | 0.8.0/paludis/args/bad_argument.cc | 35 | ||||
-rw-r--r-- | 0.8.0/paludis/args/bad_argument.hh | 54 | ||||
-rw-r--r-- | 0.8.0/paludis/args/man.cc | 81 | ||||
-rw-r--r-- | 0.8.0/paludis/args/man.hh | 37 |
20 files changed, 2182 insertions, 0 deletions
diff --git a/0.8.0/paludis/args/Makefile.am b/0.8.0/paludis/args/Makefile.am new file mode 100644 index 000000000..184ed7c73 --- /dev/null +++ b/0.8.0/paludis/args/Makefile.am @@ -0,0 +1,53 @@ +CLEANFILES = *~ gmon.out *.gcov *.gcno *.gcda +MAINTAINERCLEANFILES = Makefile.in +AM_CXXFLAGS = -I$(top_srcdir) @PALUDIS_CXXFLAGS@ @PALUDIS_CXXFLAGS_VISIBILITY@ + +TESTS = \ + args_TEST + +paludis_args_includedir = $(includedir)/paludis/args/ + +paludis_args_include_HEADERS = \ + args.hh \ + args_error.hh \ + args_group.hh \ + args_handler.hh \ + args_option.hh \ + bad_argument.hh \ + args_visitor.hh \ + args_dumper.hh \ + man.hh + +libpaludisargs_la_SOURCES = $(paludis_args_include_HEADERS) \ + args.cc \ + args_error.cc \ + args_group.cc \ + args_handler.cc \ + args_option.cc \ + bad_argument.cc \ + args_visitor.cc \ + args_dumper.cc \ + man.cc + +libpaludisargs_la_LDFLAGS = -version-info @VERSION_LIB_CURRENT@:@VERSION_LIB_REVISION@:0 +libpaludisargs_la_LIBADD = \ + $(top_builddir)/paludis/util/libpaludisutil.la + +TESTS_ENVIRONMENT = env -u PALUDIS_OPTIONS \ + TEST_SCRIPT_DIR="$(srcdir)/" \ + PALUDIS_REPOSITORY_SO_DIR="$(top_builddir)/paludis/repositories" \ + bash $(top_srcdir)/test/run_test.sh + +lib_LTLIBRARIES = libpaludisargs.la +check_PROGRAMS = $(TESTS) + +test_ldadd = \ + $(top_builddir)/test/libtest.a \ + libpaludisargs.la \ + $(top_builddir)/paludis/libpaludis.la \ + $(top_builddir)/paludis/util/libpaludisutil.la \ + $(DYNAMIC_LD_LIBS) + +args_TEST_SOURCES = args_TEST.cc +args_TEST_LDADD = $(test_ldadd) + diff --git a/0.8.0/paludis/args/args.cc b/0.8.0/paludis/args/args.cc new file mode 100644 index 000000000..510351dd2 --- /dev/null +++ b/0.8.0/paludis/args/args.cc @@ -0,0 +1,27 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2005, 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "args.hh" + +/** \file + * Implementation for args.hh . + * + * \ingroup grplibpaludisargs + */ + diff --git a/0.8.0/paludis/args/args.hh b/0.8.0/paludis/args/args.hh new file mode 100644 index 000000000..8aad3bd9e --- /dev/null +++ b/0.8.0/paludis/args/args.hh @@ -0,0 +1,35 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2005, 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef PALUDIS_GUARD_ARGS_ARGS_HH +#define PALUDIS_GUARD_ARGS_ARGS_HH 1 + +/** \file + * Master include file for command line argument handling. + * + * \ingroup grplibpaludisargs + */ + +#include <paludis/args/args_error.hh> +#include <paludis/args/args_group.hh> +#include <paludis/args/args_handler.hh> +#include <paludis/args/args_option.hh> +#include <paludis/args/bad_argument.hh> + +#endif diff --git a/0.8.0/paludis/args/args_TEST.cc b/0.8.0/paludis/args/args_TEST.cc new file mode 100644 index 000000000..56193ae1a --- /dev/null +++ b/0.8.0/paludis/args/args_TEST.cc @@ -0,0 +1,176 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2005, 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <paludis/args/args.hh> +#include <paludis/args/args_error.hh> +#include <test/test_framework.hh> +#include <test/test_runner.hh> + +using namespace paludis; +using namespace paludis::args; +using namespace test; + +/** \file + * Test cases for paludis::args things. + * + * \ingroup grptestcases + */ + +#ifndef DOXYGEN + +struct CommandLine : public ArgsHandler +{ + ArgsGroup group_one; + SwitchArg arg_foo; + SwitchArg arg_bar; + SwitchArg arg_dummy; + + ArgsGroup group_two; + SwitchArg arg_baz; + AliasArg arg_other_baz; + StringArg arg_something; + IntegerArg arg_somenum; + EnumArg arg_enum; + + ArgsGroup group_three; + EnumArg arg_other_enum; + StringSetArg arg_stringset; + + CommandLine(); + ~CommandLine(); + + std::string app_name() const + { + return "args_TEST"; + } + + std::string app_synopsis() const + { + return "tests for args"; + } + + std::string app_description() const + { + return "Tests args"; + } +}; + +CommandLine::CommandLine() : + group_one(this, "Group one", "Description of group one"), + arg_foo(&group_one, "foo", 'f', "Enable foo"), + arg_bar(&group_one, "bar", 'b', "Enable bar"), + arg_dummy(&group_one, "dummy", 'd', "Enable something else"), + + group_two(this, "Group two", "Description of group two"), + arg_baz(&group_two, "baz", 'z', "Enable baz"), + arg_other_baz(&arg_baz, "other-baz"), + arg_something(&group_two, "something", 's', "Value of something"), + arg_somenum(&group_two, "num", 'n', "Some number"), + arg_enum(&group_two, "enum", 'e', "One of three", EnumArg::EnumArgOptions("one", "Option one")("two", "option two")("three", "option three"), "two"), + + group_three(this, "Group three", "Description of group three"), + arg_other_enum(&group_three, "something", '\0', "Blah.", EnumArg::EnumArgOptions("a", "A")("b", "B")("c", "C"), "b"), + arg_stringset(&group_three, "stringset", 't', "A StringSet.") +{ +} + +CommandLine::~CommandLine() +{ +} + +#endif + +namespace test_cases +{ + /** + * \test Simple args tests. + * + * \ingroup grptestcases + */ + struct ArgsTestSimple : TestCase + { + ArgsTestSimple() : TestCase("simple") { } + + void run() + { + char * args[] = { "program-name", "--other-baz", "-fsne", "blah", "7", "three", "--", "--dummy", + "one", "two" }; + CommandLine c1; + c1.run(10, args); + TEST_CHECK(c1.arg_foo.specified()); + TEST_CHECK(! c1.arg_bar.specified()); + TEST_CHECK(c1.arg_baz.specified()); + TEST_CHECK(c1.arg_other_baz.specified()); + TEST_CHECK(c1.arg_something.specified()); + TEST_CHECK(c1.arg_something.argument() == "blah"); + TEST_CHECK(c1.arg_somenum.specified()); + TEST_CHECK(c1.arg_somenum.argument() == 7); + TEST_CHECK(c1.arg_enum.specified()); + TEST_CHECK(c1.arg_enum.argument() == "three"); + TEST_CHECK(! c1.arg_dummy.specified()); + TEST_CHECK(! c1.arg_other_enum.specified()); + TEST_CHECK(c1.arg_other_enum.argument() == "b"); + + TEST_CHECK_EQUAL(std::distance(c1.begin_parameters(), c1.end_parameters()), 3); + TEST_CHECK_EQUAL(*c1.begin_parameters(), "--dummy"); + TEST_CHECK_EQUAL(*++c1.begin_parameters(), "one"); + TEST_CHECK_EQUAL(*++(++(c1.begin_parameters())), "two"); + } + } test_args_simple; + + /** + * \test Missing parameters tests. + * + * \ingroup grptestcases + */ + struct ArgsTestNoParam : TestCase + { + ArgsTestNoParam() : TestCase("Missing parameters") { } + + void run() + { + char *args[] = { "program-name", "-e" }; + CommandLine c1; + TEST_CHECK_THROWS(c1.run(2, args), MissingValue); + } + } test_args_no_param; + + /** + * \test String tests. + * + * \ingroup grptestcases + */ + struct ArgsTestStringSet : TestCase + { + ArgsTestStringSet() : TestCase("StringSet") { } + + void run() + { + char *args[] = { "program-name", "--stringset", "one", "-t", "two", "-t", "three", "fnord" }; + CommandLine c1; + c1.run(8, args); + TEST_CHECK(c1.arg_stringset.specified()); + TEST_CHECK(std::find(c1.arg_stringset.args_begin(), c1.arg_stringset.args_end(), "one") != c1.arg_stringset.args_end()); + TEST_CHECK(std::find(c1.arg_stringset.args_begin(), c1.arg_stringset.args_end(), "two") != c1.arg_stringset.args_end()); + TEST_CHECK(std::find(c1.arg_stringset.args_begin(), c1.arg_stringset.args_end(), "three") != c1.arg_stringset.args_end()); + TEST_CHECK(std::find(c1.arg_stringset.args_begin(), c1.arg_stringset.args_end(), "fnord") == c1.arg_stringset.args_end()); + } + } test_args_string_set; +} + diff --git a/0.8.0/paludis/args/args_dumper.cc b/0.8.0/paludis/args/args_dumper.cc new file mode 100644 index 000000000..afdc747c0 --- /dev/null +++ b/0.8.0/paludis/args/args_dumper.cc @@ -0,0 +1,81 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2006 Stephen Bennett <spb@gentoo.org> + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version + * 2 of the License, or (at your option) any later version. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <paludis/args/args_dumper.hh> +#include <paludis/args/args_option.hh> + +#include <sstream> + +/** \file + * Implementation of ArgsDumper. + * + * \ingroup grplibpaludisargs + */ + +using namespace paludis; +using namespace paludis::args; + +ArgsDumper::ArgsDumper(std::ostream & os) : + _os(os) +{ +} + +void ArgsDumper::visit(const ArgsOption * const a) +{ + std::stringstream p; + p << " --" << a->long_name(); + if (a->short_name()) + p << ", -" << a->short_name(); + if (p.str().length() < 24) + p << std::string(24 - p.str().length(), ' '); + else + p << std::endl << std::string(24, ' '); + _os << p.str(); + _os << " " << a->description() << std::endl; +} + +#ifndef DOXYGEN +#define VISIT(type) void ArgsDumper::visit(const type * const a) \ + { visit(static_cast<const ArgsOption *>(a)); } + +VISIT(SwitchArg) +VISIT(StringArg) +VISIT(IntegerArg) +VISIT(AliasArg) +VISIT(StringSetArg) +#endif + +void ArgsDumper::visit(const EnumArg * const a) +{ + visit(static_cast<const ArgsOption *>(a)); + for (EnumArg::AllowedArgIterator it = a->begin_allowed_args(), it_end = a->end_allowed_args(); + it != it_end; ++it) + { + std::stringstream p; + p << " " << (*it).first; + if (p.str().length() < 26) + p << std::string(26 - p.str().length(), ' '); + _os << p.str(); + _os << " " << (*it).second; + if ((*it).first == a->default_arg()) + _os << " (default)"; + _os << std::endl; + } +} diff --git a/0.8.0/paludis/args/args_dumper.hh b/0.8.0/paludis/args/args_dumper.hh new file mode 100644 index 000000000..c53a3b32c --- /dev/null +++ b/0.8.0/paludis/args/args_dumper.hh @@ -0,0 +1,80 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2006 Stephen Bennett <spb@gentoo.org> + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version + * 2 of the License, or (at your option) any later version. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef PALUDIS_GUARD_PALUDIS_ARGS_ARGS_DUMPER_HH +#define PALUDIS_GUARD_PALUDIS_ARGS_ARGS_DUMPER_HH 1 + +#include <iosfwd> +#include <paludis/args/args_visitor.hh> +#include <paludis/util/visitor.hh> + +namespace paludis +{ + namespace args + { + class ArgsOption; + class SwitchArg; + class StringArg; + class IntegerArg; + class AliasArg; + class EnumArg; + + /** + * Visitor class. Prints help text appropriate to each command line option. + * + * \ingroup grplibpaludisargs + */ + class PALUDIS_VISIBLE ArgsDumper : + public ArgsVisitorTypes::ConstVisitor + { + private: + std::ostream & _os; + + public: + /** + * Constructor. + */ + ArgsDumper(std::ostream & os); + + /// Visit an ArgsOption. + void visit(const ArgsOption * const); + + /// Visit a SwitchArg. + void visit(const SwitchArg * const); + + /// Visit a StringArg. + void visit(const StringArg * const); + + /// Visit an IntegerArg. + void visit(const IntegerArg * const); + + /// Visit an AliasArg. + void visit(const AliasArg * const); + + /// Visit an EnumArg. + void visit(const EnumArg * const); + + /// Visit a StringSetArg. + void visit(const StringSetArg * const); + }; + } +} + +#endif diff --git a/0.8.0/paludis/args/args_error.cc b/0.8.0/paludis/args/args_error.cc new file mode 100644 index 000000000..36f9ee4aa --- /dev/null +++ b/0.8.0/paludis/args/args_error.cc @@ -0,0 +1,45 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2005, 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "args_error.hh" + +/** \file + * Implementation for ArgsError. + * + * \ingroup grplibpaludisargs + * \ingroup grpexceptions + */ + +using namespace paludis::args; + +ArgsError::ArgsError(const std::string & our_message) throw () : + paludis::Exception("Error handling command line: " + our_message) +{ +} + +BadValue::BadValue(const std::string& option, const std::string& value) throw () : + ArgsError("Invalid parameter '" + value + "' for argument '" + option + "'") +{ +} + +MissingValue::MissingValue(const std::string & arg) throw() : + ArgsError("No parameter given for '" + arg + "'") +{ +} + diff --git a/0.8.0/paludis/args/args_error.hh b/0.8.0/paludis/args/args_error.hh new file mode 100644 index 000000000..2f5b63189 --- /dev/null +++ b/0.8.0/paludis/args/args_error.hh @@ -0,0 +1,87 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2005, 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef PALUDIS_GUARD_ARGS_ARGS_ERROR_HH +#define PALUDIS_GUARD_ARGS_ARGS_ERROR_HH 1 + +#include <paludis/util/exception.hh> +#include <string> + +/** \file + * Declaration for ArgsError. + * + * \ingroup grplibpaludisargs + */ + +namespace paludis +{ + namespace args + { + /** + * Thrown if an invalid command line argument is provided. + * + * \ingroup grplibpaludisargs + * \ingroup grpexceptions + */ + class PALUDIS_VISIBLE ArgsError : + public paludis::Exception + { + protected: + /** + * Constructor. + */ + ArgsError(const std::string & message) throw (); + }; + + /** + * Thrown if an invalid parameter is passed to a valid command line argument. + * + * \ingroup grplibpaludisargs + * \ingroup grpexceptions + */ + class PALUDIS_VISIBLE BadValue : + public ArgsError + { + public: + /** + * Constructor + */ + BadValue(const std::string& option, const std::string& value) throw(); + }; + + /** + * Thrown if an argument is specified that needs a parameter, + * but no parameter is given. + * + * \ingroup grplibpaludisargs + * \ingroup grpexceptions + */ + class PALUDIS_VISIBLE MissingValue : + public ArgsError + { + public: + /** + * Constructor. + */ + MissingValue(const std::string & arg) throw (); + }; + } +} + +#endif diff --git a/0.8.0/paludis/args/args_group.cc b/0.8.0/paludis/args/args_group.cc new file mode 100644 index 000000000..45b6d74e9 --- /dev/null +++ b/0.8.0/paludis/args/args_group.cc @@ -0,0 +1,78 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2005, 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "args.hh" +#include <list> + +/** \file + * Implementation for ArgsGroup. + * + * \ingroup grplibpaludisargs + */ + +using namespace paludis::args; + +namespace paludis +{ + /** + * Implementation data for ArgsGroup. + * + * \ingroup grplibpaludisargs + */ + template<> + struct Implementation<ArgsGroup> : + InternalCounted<ArgsGroup> + { + std::list<ArgsOption *> args_options; + }; +} + +ArgsGroup::ArgsGroup(ArgsHandler * h, const std::string & our_name, + const std::string & our_description) : + PrivateImplementationPattern<ArgsGroup>(new Implementation<ArgsGroup>), + _name(our_name), + _description(our_description), + _handler(h) +{ + h->add(this); +} + +void +ArgsGroup::add(ArgsOption * const value) +{ + /// \bug Should check for uniqueness of short and long names. + _imp->args_options.push_back(value); +} + +ArgsGroup::~ArgsGroup() +{ +} + +ArgsGroup::Iterator +ArgsGroup::begin() const +{ + return Iterator(_imp->args_options.begin()); +} + +ArgsGroup::Iterator +ArgsGroup::end() const +{ + return Iterator(_imp->args_options.end()); +} + diff --git a/0.8.0/paludis/args/args_group.hh b/0.8.0/paludis/args/args_group.hh new file mode 100644 index 000000000..62b363d16 --- /dev/null +++ b/0.8.0/paludis/args/args_group.hh @@ -0,0 +1,117 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2005, 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef PALUDIS_GUARD_ARGS_ARGS_GROUP_HH +#define PALUDIS_GUARD_ARGS_ARGS_GROUP_HH 1 + +#include <paludis/args/args_option.hh> +#include <paludis/util/instantiation_policy.hh> +#include <paludis/util/private_implementation_pattern.hh> +#include <string> + +#include <libwrapiter/libwrapiter_forward_iterator.hh> + +/** \file + * Declaration for ArgsGroup. + * + * \ingroup grplibpaludisargs + */ + +namespace paludis +{ + + namespace args + { + class ArgsHandler; + + /** + * Contains a related group of command line arguments. + * + * \ingroup grplibpaludisargs + */ + class PALUDIS_VISIBLE ArgsGroup : + private PrivateImplementationPattern<ArgsGroup>, + private InstantiationPolicy<ArgsGroup, instantiation_method::NonCopyableTag> + { + private: + const std::string _name; + const std::string _description; + + ArgsHandler * _handler; + + public: + ArgsHandler * handler() + { + return _handler; + } + + /** + * Add an ArgsOption instance (called by the ArgsOption + * constructor). + */ + void add(ArgsOption * const value); + + ///\name Iterate over our ArgsOptions. + ///\{ + + typedef libwrapiter::ForwardIterator<ArgsGroup, ArgsOption * const> Iterator; + + Iterator begin() const; + Iterator end() const; + + ///\} + + ///\name Basic operations + ///\{ + + ArgsGroup(ArgsHandler * h, const std::string & name, + const std::string & description); + + ~ArgsGroup(); + + ///\} + + /** + * Fetch our name. + */ + const std::string & name() const + { + return _name; + } + + /** + * Fetch our description. + */ + const std::string & description() const + { + return _description; + } + + /** + * Fetch our handler. + */ + ArgsHandler * handler() const + { + return _handler; + } + }; + } +} + +#endif diff --git a/0.8.0/paludis/args/args_handler.cc b/0.8.0/paludis/args/args_handler.cc new file mode 100644 index 000000000..d7ab67cbb --- /dev/null +++ b/0.8.0/paludis/args/args_handler.cc @@ -0,0 +1,231 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2005, 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "args.hh" +#include "args_dumper.hh" +#include <paludis/util/system.hh> +#include <algorithm> +#include <sstream> +#include <list> +#include <map> + +/** \file + * Implementation for ArgsHandler. + * + * \ingroup grplibpaludisargs + */ + +using namespace paludis::args; + +namespace paludis +{ + /** + * Implementation data for ArgsHandler. + * + * \ingroup grplibpaludisargs + */ + template<> + struct Implementation<ArgsHandler> : + InternalCounted<Implementation<ArgsHandler> > + { + std::list<ArgsGroup *> groups; + std::list<std::string> parameters; + std::list<std::string> usage_lines; + std::list<std::pair<std::string, std::string> > environment_lines; + + std::map<std::string, ArgsOption *> longopts; + std::map<char, ArgsOption *> shortopts; + }; +} + +ArgsHandler::ArgsHandler() : + PrivateImplementationPattern<ArgsHandler>(new Implementation<ArgsHandler>) +{ +} + +ArgsHandler::~ArgsHandler() +{ +} + +void +ArgsHandler::add_usage_line(const std::string & l) +{ + _imp->usage_lines.push_back(l); +} + +void +ArgsHandler::add_enviromnent_variable(const std::string & e, const std::string & desc) +{ + _imp->environment_lines.push_back(std::make_pair(e, desc)); +} + +void +ArgsHandler::add(ArgsGroup * const g) +{ + /// \bug Should check for name uniqueness. + _imp->groups.push_back(g); +} + +void +ArgsHandler::run(const int argc, const char * const * const argv) +{ + std::list<std::string> args; + + std::string env_options = paludis::getenv_with_default("PALUDIS_OPTIONS", ""); + + std::istringstream iss(env_options); + std::string option; + while(iss.good()) + { + iss >> option; + if(!option.empty()) + args.push_back(option); + } + + args.insert(args.end(), &argv[1], &argv[argc]); + + libwrapiter::ForwardIterator<ArgsVisitor, std::string> argit(args.begin()), arge(args.end()); + + ArgsVisitor visitor(&argit, arge); + + for ( ; argit != arge; ++argit ) + { + std::string arg = *argit; + + if (arg == "--") + { + ++argit; + break; + } + else if (0 == arg.compare(0, 2, "--")) + { + arg.erase(0, 2); + std::map<std::string, ArgsOption *>::iterator it = _imp->longopts.find(arg); + if (it == _imp->longopts.end()) + throw BadArgument("--" + arg); + (*it).second->accept(&visitor); + } + else if (arg[0] == '-') + { + arg.erase(0, 1); + for (std::string::iterator c = arg.begin(); c != arg.end(); ++c) + { + std::map<char, ArgsOption *>::iterator it = _imp->shortopts.find(*c); + if (it == _imp->shortopts.end()) + { + throw BadArgument(std::string("-") + *c); + } + (*it).second->accept(&visitor); + } + } + else + { + _imp->parameters.push_back(arg); + } + } + + _imp->parameters.insert(_imp->parameters.end(), + argit, libwrapiter::ForwardIterator<ArgsVisitor, std::string>(args.end())); +} + +void +ArgsHandler::dump_to_stream(std::ostream & s) const +{ + ArgsDumper dump(s); + std::list<ArgsGroup *>::const_iterator g(_imp->groups.begin()), g_end(_imp->groups.end()); + for ( ; g != g_end ; ++g) + { + s << (*g)->name() << ":" << std::endl; + + std::for_each((*g)->begin(), (*g)->end(), accept_visitor(&dump)); + + s << std::endl; + } +} + +ArgsHandler::ParametersIterator +ArgsHandler::begin_parameters() const +{ + return ParametersIterator(_imp->parameters.begin()); +} + +ArgsHandler::ParametersIterator +ArgsHandler::end_parameters() const +{ + return ParametersIterator(_imp->parameters.end()); +} + +bool +ArgsHandler::empty() const +{ + return _imp->parameters.empty(); +} + +std::ostream & +paludis::args::operator<< (std::ostream & s, const ArgsHandler & h) +{ + h.dump_to_stream(s); + return s; +} + +void +ArgsHandler::add_option(ArgsOption * const opt, const std::string & long_name, + const char short_name) +{ + _imp->longopts[long_name] = opt; + if (short_name != '\0') + _imp->shortopts[short_name] = opt; +} + +ArgsHandler::UsageLineIterator +ArgsHandler::begin_usage_lines() const +{ + return UsageLineIterator(_imp->usage_lines.begin()); +} + +ArgsHandler::UsageLineIterator +ArgsHandler::end_usage_lines() const +{ + return UsageLineIterator(_imp->usage_lines.end()); +} + +ArgsHandler::EnvironmentLineIterator +ArgsHandler::begin_environment_lines() const +{ + return EnvironmentLineIterator(_imp->environment_lines.begin()); +} + +ArgsHandler::EnvironmentLineIterator +ArgsHandler::end_environment_lines() const +{ + return EnvironmentLineIterator(_imp->environment_lines.end()); +} + +ArgsHandler::ArgsGroupsIterator +ArgsHandler::begin_args_groups() const +{ + return ArgsGroupsIterator(_imp->groups.begin()); +} + +ArgsHandler::ArgsGroupsIterator +ArgsHandler::end_args_groups() const +{ + return ArgsGroupsIterator(_imp->groups.end()); +} + diff --git a/0.8.0/paludis/args/args_handler.hh b/0.8.0/paludis/args/args_handler.hh new file mode 100644 index 000000000..85e486dd1 --- /dev/null +++ b/0.8.0/paludis/args/args_handler.hh @@ -0,0 +1,181 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2005, 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef PALUDIS_GUARD_ARGS_ARGS_HANDLER_HH +#define PALUDIS_GUARD_ARGS_ARGS_HANDLER_HH 1 + +#include <paludis/args/args_group.hh> +#include <paludis/util/instantiation_policy.hh> +#include <paludis/util/private_implementation_pattern.hh> + +#include <iosfwd> +#include <string> + +#include <libwrapiter/libwrapiter_forward_iterator.hh> + +/** \file + * Declaration for ArgsHandler. + * + * \ingroup grplibpaludisargs + */ + +namespace paludis +{ + namespace args + { + /** + * Handles command line arguments. + * + * \ingroup grplibpaludisargs + */ + class PALUDIS_VISIBLE ArgsHandler : + private InstantiationPolicy<ArgsHandler, instantiation_method::NonCopyableTag>, + private PrivateImplementationPattern<ArgsHandler> + { + friend class ArgsGroup; + friend std::ostream & operator<< (std::ostream &, const ArgsHandler &); + + protected: + /** + * Add a new usage line. + */ + void add_usage_line(const std::string & l); + + /** + * Add a new environment line. + */ + void add_enviromnent_variable(const std::string & e, const std::string & desc); + + /** + * Add an new ArgsGroup (called by the ArgsGroup constructor). + */ + void add(ArgsGroup * const); + + /** + * Dump, for --help output (called by operator<<). + */ + void dump_to_stream(std::ostream & s) const; + + public: + ///\name Basic operations + ///\{ + + ArgsHandler(); + + virtual ~ArgsHandler(); + + ///\} + + /** + * Parse command line arguments. + */ + void run(const int, const char * const * const); + + ///\name Iterate over our parameters (non - and -- switches and their values) + ///\{ + + typedef libwrapiter::ForwardIterator<ArgsHandler, const std::string> ParametersIterator; + + ParametersIterator begin_parameters() const; + + ParametersIterator end_parameters() const; + + bool empty() const; + + ///\} + + /** + * Add an ArgsOption instance. + */ + void add_option(ArgsOption * const, const std::string & long_name, + const char short_name = '\0'); + + ///\name About our application (for documentation) + ///\{ + + /** + * What is our application name? + */ + virtual std::string app_name() const = 0; + + /** + * What is our application's Unix manual section? + */ + virtual std::string man_section() const + { + return "1"; + } + + /** + * One line synopsis of what our application is. + */ + virtual std::string app_synopsis() const = 0; + + /** + * Long description of what our application is. + */ + virtual std::string app_description() const = 0; + + ///\} + + ///\name Iterate over our usage lines (for documentation) + ///\{ + + typedef libwrapiter::ForwardIterator<ArgsHandler, const std::string> UsageLineIterator; + + UsageLineIterator begin_usage_lines() const; + + UsageLineIterator end_usage_lines() const; + + ///\} + + ///\name Iterate over our environment lines (for documentation) + ///\{ + + typedef libwrapiter::ForwardIterator<ArgsHandler, + const std::pair<std::string, std::string> > EnvironmentLineIterator; + + EnvironmentLineIterator begin_environment_lines() const; + + EnvironmentLineIterator end_environment_lines() const; + + ///\} + + ///\name Iterate over our groups + ///\{ + + typedef libwrapiter::ForwardIterator<ArgsHandler, ArgsGroup * const> ArgsGroupsIterator; + + ArgsGroupsIterator begin_args_groups() const; + + ArgsGroupsIterator end_args_groups() const; + + ///\} + }; + + /** + * Output an ArgsHandler to an ostream, for --help output. + * + * \ingroup grplibpaludisargs + */ + std::ostream & operator<< (std::ostream &, const ArgsHandler &) PALUDIS_VISIBLE; + } +} + +#endif diff --git a/0.8.0/paludis/args/args_option.cc b/0.8.0/paludis/args/args_option.cc new file mode 100644 index 000000000..3ab90bdee --- /dev/null +++ b/0.8.0/paludis/args/args_option.cc @@ -0,0 +1,226 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2005, 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "args.hh" +#include "args_error.hh" +#include <set> +#include <vector> + +/** \file + * Implementation for ArgsOption. + * + * \ingroup grplibpaludisargs + */ + +using namespace paludis::args; + +ArgsOption::ArgsOption(ArgsGroup * const g, const std::string & our_long_name, + const char our_short_name, const std::string & our_description) : + _group(g), + _long_name(our_long_name), + _short_name(our_short_name), + _description(our_description), + _specified(false) +{ + g->add(this); + g->handler()->add_option(this, our_long_name, our_short_name); +} + +ArgsOption::~ArgsOption() +{ +} + +SwitchArg::SwitchArg(ArgsGroup * const our_group, std::string our_long_name, char our_short_name, + std::string our_description) : + ArgsOption(our_group, our_long_name, our_short_name, our_description) +{ +} + +SwitchArg::~SwitchArg() +{ +} + +AliasArg::AliasArg(ArgsOption * const other, const std::string & our_long_name) : + ArgsOption(other->group(), our_long_name, '\0', "Alias for --" + other->long_name()), + _other(other) +{ + other->group()->handler()->add_option(other, our_long_name); +} + +StringArg::StringArg(ArgsGroup * const g, const std::string & our_long_name, + const char our_short_name, const std::string & our_description) : + ArgsOption(g, our_long_name, our_short_name, our_description) +{ +} + +namespace paludis +{ + /** + * Implementation data for StringSetArg. + * + * \ingroup grplibpaludisargs + */ + template<> + struct Implementation<StringSetArg> : + InternalCounted<Implementation<StringSetArg> > + { + std::set<std::string> args; + }; +} + +StringSetArg::StringSetArg(ArgsGroup * const g, const std::string & our_long_name, + const char our_short_name, const std::string & our_description) : + ArgsOption(g, our_long_name, our_short_name, our_description), + PrivateImplementationPattern<StringSetArg>(new Implementation<StringSetArg>) +{ +} + +StringSetArg::Iterator +StringSetArg::args_begin() const +{ + return Iterator(_imp->args.begin()); +} + +StringSetArg::Iterator +StringSetArg::args_end() const +{ + return Iterator(_imp->args.end()); +} + +void +StringSetArg::add_argument(const std::string & arg) +{ + _imp->args.insert(arg); +} + +IntegerArg::IntegerArg(ArgsGroup * const our_group, const std::string & our_long_name, + char our_short_name, const std::string & our_description) : + ArgsOption(our_group, our_long_name, our_short_name, our_description) +{ +} + +namespace +{ + /** + * Is an arg a particular value? + * + * \ingroup grplibpaludisargs + */ + struct ArgIs + { + /// The argument. + const std::string arg; + + /// Constructor. + ArgIs(const std::string & a) : + arg(a) + { + } + + /// Comparator. + bool operator() (const std::pair<std::string, std::string> & p) const + { + return p.first == arg; + } + }; +} + +namespace paludis +{ + /** + * Implementation data for EnumArg. + * + * \ingroup grplibpaludisargs + */ + template<> + struct Implementation<EnumArg> : + InternalCounted<Implementation<EnumArg> > + { + std::vector<std::pair<std::string, std::string> > allowed_args; + }; + + /** + * Implementation data for EnumArg::EnumArgOptions. + * + * \ingroup grplibpaludisargs + */ + template<> + struct Implementation<EnumArg::EnumArgOptions> : + InternalCounted<Implementation<EnumArg::EnumArgOptions> > + { + std::vector<std::pair<std::string, std::string> > options; + }; +} + +void EnumArg::set_argument(const std::string & arg) +{ + if (_imp->allowed_args.end() == std::find_if(_imp->allowed_args.begin(), + _imp->allowed_args.end(), ArgIs(arg))) + throw (BadValue("--" + long_name(), arg)); + + _argument = arg; +} + +EnumArg::~EnumArg() +{ +} + +EnumArg::EnumArgOptions::EnumArgOptions(std::string opt, std::string desc) : + PrivateImplementationPattern<EnumArgOptions>(new Implementation<EnumArgOptions>) +{ + _imp->options.push_back(std::make_pair(opt, desc)); +} + +EnumArg::EnumArgOptions & EnumArg::EnumArgOptions::operator() (std::string opt, std::string desc) +{ + _imp->options.push_back(std::make_pair(opt, desc)); + return *this; +} + +EnumArg::EnumArgOptions::~EnumArgOptions() +{ +} + +EnumArg::EnumArg(ArgsGroup * const our_group, const std::string & our_long_name, + const char our_short_name, const std::string & our_description, + const EnumArgOptions & opts, const std::string & our_default_arg) : + ArgsOption(our_group, our_long_name, our_short_name, our_description), + PrivateImplementationPattern<EnumArg>(new Implementation<EnumArg>), + _argument(our_default_arg), + _default_arg(our_default_arg) +{ + _imp->allowed_args = opts._imp->options; +} + +EnumArg::AllowedArgIterator +EnumArg::begin_allowed_args() const +{ + return AllowedArgIterator(_imp->allowed_args.begin()); +} + +EnumArg::AllowedArgIterator +EnumArg::end_allowed_args() const +{ + return AllowedArgIterator(_imp->allowed_args.end()); +} + +StringSetArg::~StringSetArg() +{ +} + diff --git a/0.8.0/paludis/args/args_option.hh b/0.8.0/paludis/args/args_option.hh new file mode 100644 index 000000000..a46ee5e61 --- /dev/null +++ b/0.8.0/paludis/args/args_option.hh @@ -0,0 +1,361 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2005, 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * Copyright (c) 2006 Stephen Bennett <spb@gentoo.org> + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef PALUDIS_GUARD_ARGS_ARGS_OPTION_HH +#define PALUDIS_GUARD_ARGS_ARGS_OPTION_HH 1 + +#include <paludis/args/args_visitor.hh> +#include <paludis/util/private_implementation_pattern.hh> + +/** \file + * Declaration for ArgsOption. + * + * \ingroup grplibpaludisargs + */ + +namespace paludis +{ + namespace args + { + class ArgsGroup; + + /** + * Base class for a command line option. + * + * \ingroup grplibpaludisargs + */ + class PALUDIS_VISIBLE ArgsOption : + public virtual VisitableInterface<ArgsVisitorTypes> + { + friend class ArgsHandler; + + private: + ArgsGroup * const _group; + + const std::string _long_name; + const char _short_name; + const std::string _description; + + bool _specified; + + ArgsOption(const ArgsOption &); + void operator= (const ArgsOption &); + + protected: + /** + * Constructor. + */ + ArgsOption(ArgsGroup * const, const std::string & long_name, + const char short_name, const std::string & description); + + /** + * Destructor. + */ + ~ArgsOption(); + + public: + /** + * Fetch our long name. + */ + const std::string & long_name() const + { + return _long_name; + } + + /** + * Fetch our short name (may be 0). + */ + char short_name() const + { + return _short_name; + } + + /** + * Fetch our description. + */ + const std::string & description() const + { + return _description; + } + + /** + * Fetch whether or not we were specified on the + * command line. + */ + virtual bool specified() const + { + return _specified; + } + + /** + * Set the value returned by specified(). + */ + virtual void set_specified(const bool value) + { + _specified = value; + } + + /** + * Fetch our group. + */ + ArgsGroup * group() + { + return _group; + } + }; + + /** + * A SwitchArg is an option that can either be specified or not + * specified, and that takes no value (for example, --help). + * + * \ingroup grplibpaludisargs + */ + class PALUDIS_VISIBLE SwitchArg : + public ArgsOption, + public Visitable<SwitchArg, ArgsVisitorTypes> + { + public: + /** + * Constructor. + */ + SwitchArg(ArgsGroup * const group, std::string long_name, char short_name, + std::string description); + + ~SwitchArg(); + }; + + /** + * An option that takes a string argument. + * + * \ingroup grplibpaludisargs + */ + class PALUDIS_VISIBLE StringArg : + public ArgsOption, + public Visitable<StringArg, ArgsVisitorTypes> + { + private: + std::string _argument; + + public: + /** + * Constructor + */ + StringArg(ArgsGroup * const, const std::string & long_name, + const char short_name, const std::string & description); + + /** + * Fetch the argument that was given to this option. + */ + const std::string& argument() const { return _argument; } + + /** + * Set the argument returned by argument(). + */ + void set_argument(const std::string& arg) { _argument = arg; } + }; + + /** + * An option that takes a set of strings. + * + * \ingroup grplibpaludisargs + */ + class PALUDIS_VISIBLE StringSetArg : + public ArgsOption, + public Visitable<StringSetArg, ArgsVisitorTypes>, + private PrivateImplementationPattern<StringSetArg> + { + public: + ///\name Basic operations + ///\{ + + StringSetArg(ArgsGroup * const, const std::string & long_name, + const char short_name, const std::string & description); + + ~StringSetArg(); + + ///\} + + ///\name Iterate over our args. + ///\{ + + typedef libwrapiter::ForwardIterator<StringArg, const std::string> Iterator; + + Iterator args_begin() const; + + Iterator args_end() const; + + ///\} + + /** + * Add an argument to the set. + */ + void add_argument(const std::string & arg); + }; + + + /** + * An AliasArg is an alias for another argument. + * + * \ingroup grplibpaludisargs + */ + class PALUDIS_VISIBLE AliasArg : + public ArgsOption, + public Visitable<AliasArg, ArgsVisitorTypes> + { + private: + ArgsOption * const _other; + + public: + /** + * Constructor. + */ + AliasArg(ArgsOption * const other, const std::string & new_long_name); + + virtual bool specified() const + { + return _other->specified(); + } + + virtual void set_specified(const bool value) + { + _other->set_specified(value); + } + }; + + /** + * An option that takes an integer argument. + * + * \ingroup grplibpaludisargs + */ + class PALUDIS_VISIBLE IntegerArg : + public ArgsOption, + public Visitable<IntegerArg, ArgsVisitorTypes> + { + private: + int _argument; + + public: + /** + * Constructor + */ + IntegerArg(ArgsGroup * const, const std::string & long_name, + const char short_name, const std::string & description); + /** + * Fetch the argument that was given to this option. + */ + int argument() const { return _argument; } + + /** + * Set the argument returned by argument(). + */ + void set_argument(const int arg) { _argument = arg; } + }; + + /** + * An option that takes one of a predefined set of string arguments. + * + * \ingroup grplibpaludisargs + */ + class PALUDIS_VISIBLE EnumArg : + public ArgsOption, + public Visitable<EnumArg, ArgsVisitorTypes>, + private PrivateImplementationPattern<EnumArg> + { + private: + std::string _argument; + const std::string _default_arg; + + public: + + /** + * Helper class for passing available options and associated descriptions + * to the EnumArg constructor. + * + * \ingroup grplibpaludisargs + */ + class PALUDIS_VISIBLE EnumArgOptions : + private PrivateImplementationPattern<EnumArgOptions> + { + friend class EnumArg; + + public: + /** + * Constructor + */ + EnumArgOptions(const std::string, const std::string); + + /** + * Destructor. + */ + ~EnumArgOptions(); + + /** + * Adds another (option, description) pair. + */ + EnumArgOptions & operator() (const std::string, const std::string); + }; + + /** + * Constructor. + */ + EnumArg(ArgsGroup * const group, const std::string & long_name, + const char short_name, const std::string & description, + const EnumArgOptions & opts, const std::string & default_arg); + + ~EnumArg(); + + /** + * Fetch the argument that was given to this option. + */ + const std::string & argument() const + { + return _argument; + } + + /** + * Set the argument returned by argument(), having verified that + * it is one of the arguments allowed for this option. + */ + void set_argument(const std::string & arg); + + /** + * Fetch the default option, as specified to the constructor. + */ + const std::string & default_arg() const + { + return _default_arg; + } + + ///\name Iterate over our allowed arguments and associated descriptions + ///\{ + + typedef libwrapiter::ForwardIterator<EnumArg, + const std::pair<std::string, std::string> > AllowedArgIterator; + + AllowedArgIterator begin_allowed_args() const; + + AllowedArgIterator end_allowed_args() const; + + ///\} + }; + } +} + +#endif diff --git a/0.8.0/paludis/args/args_visitor.cc b/0.8.0/paludis/args/args_visitor.cc new file mode 100644 index 000000000..a43f714bc --- /dev/null +++ b/0.8.0/paludis/args/args_visitor.cc @@ -0,0 +1,97 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ +/* + * Copyright (c) 2005, 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "args_option.hh" +#include "args_error.hh" + +#include <paludis/util/visitor.hh> +#include <paludis/util/destringify.hh> + +#include <sstream> + +/** \file + * Implementation for ArgsVisitor. + * + * \ingroup grplibpaludisargs + */ + +using namespace paludis; +using namespace args; + +ArgsVisitor::ArgsVisitor(libwrapiter::ForwardIterator<ArgsVisitor, std::string> * ai, + libwrapiter::ForwardIterator<ArgsVisitor, std::string> ae) : _args_index(ai), _args_end(ae) +{ +} + +const std::string & +ArgsVisitor::get_param(const ArgsOption * const arg) +{ + if (++(*_args_index) == _args_end) + { + throw MissingValue("--" + arg->long_name()); + } + return **_args_index; +} + +void ArgsVisitor::visit(ArgsOption * const arg) +{ + arg->set_specified(true); +} + +void ArgsVisitor::visit(StringArg * const arg) +{ + visit(static_cast<ArgsOption *>(arg)); + arg->set_argument(get_param(arg)); +} + +void ArgsVisitor::visit(AliasArg * const arg) +{ + visit(static_cast<ArgsOption *>(arg)); +} + +void ArgsVisitor::visit(SwitchArg * const arg) +{ + visit(static_cast<ArgsOption *>(arg)); +} + +void ArgsVisitor::visit(IntegerArg * const arg) +{ + visit(static_cast<ArgsOption*>(arg)); + std::string param = get_param(arg); + try + { + arg->set_argument(destringify<int>(param)); + } + catch(DestringifyError &e) + { + throw BadValue("--" + arg->long_name(), param); + } +} + +void ArgsVisitor::visit(EnumArg * const arg) +{ + visit(static_cast<ArgsOption*>(arg)); + arg->set_argument(get_param(arg)); +} + +void ArgsVisitor::visit(StringSetArg * const arg) +{ + visit(static_cast<ArgsOption *>(arg)); + std::string param = get_param(arg); + arg->add_argument(param); +} diff --git a/0.8.0/paludis/args/args_visitor.hh b/0.8.0/paludis/args/args_visitor.hh new file mode 100644 index 000000000..822ae8ea3 --- /dev/null +++ b/0.8.0/paludis/args/args_visitor.hh @@ -0,0 +1,100 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2005, 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * Copyright (c) 2006 Stephen Bennett <spb@gentoo.org> + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef PALUDIS_GUARD_ARGS_ARGS_VISITOR_HH +#define PALUDIS_GUARD_ARGS_ARGS_VISITOR_HH 1 + +#include <paludis/util/visitor.hh> +#include <paludis/util/attributes.hh> +#include <string> + +#include <libwrapiter/libwrapiter_forward_iterator.hh> + +/** \file + * Declaration for ArgsVisitor + * + * \ingroup grplibpaludisargs + */ + +namespace paludis +{ + namespace args + { + class ArgsOption; + class StringArg; + class AliasArg; + class SwitchArg; + class IntegerArg; + class EnumArg; + class StringSetArg; + + /** + * Visitor types for visitors that can visit Args. + * + * \ingroup grplibpaludisargs + */ + typedef VisitorTypes<ArgsOption *, StringArg *, AliasArg *, SwitchArg *, + IntegerArg *, EnumArg *, StringSetArg *> ArgsVisitorTypes; + + /** + * Visitor class. Processes command-line options as they are found. + * + * \ingroup grplibpaludisargs + */ + class PALUDIS_VISIBLE ArgsVisitor : + public ArgsVisitorTypes::Visitor + { + private: + libwrapiter::ForwardIterator<ArgsVisitor, std::string> * _args_index, _args_end; + + const std::string& get_param(const ArgsOption * const); + + public: + /** + * Constructor + */ + ArgsVisitor(libwrapiter::ForwardIterator<ArgsVisitor, std::string> *, + libwrapiter::ForwardIterator<ArgsVisitor, std::string>); + + /// Visit an ArgsOption. + void visit(ArgsOption * const); + + /// Visit a StringArg. + void visit(StringArg * const); + + /// Visit an AliasArg. + void visit(AliasArg * const); + + /// Visit a SwitchArg. + void visit(SwitchArg * const); + + /// Visit an IntegerArg. + void visit(IntegerArg * const); + + /// Visit an EnumArg. + void visit(EnumArg * const); + + /// Visit a StringSetArg. + void visit(StringSetArg * const); + }; + } +} + +#endif diff --git a/0.8.0/paludis/args/bad_argument.cc b/0.8.0/paludis/args/bad_argument.cc new file mode 100644 index 000000000..e2a961879 --- /dev/null +++ b/0.8.0/paludis/args/bad_argument.cc @@ -0,0 +1,35 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2005, 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "bad_argument.hh" + +/** \file + * Implementation for BadArgument. + * + * \ingroup Args + * \ingroup Exception + */ + +using namespace paludis::args; + +BadArgument::BadArgument(const std::string & option) throw () : + ArgsError("Bad argument '" + option + "'") +{ +} + diff --git a/0.8.0/paludis/args/bad_argument.hh b/0.8.0/paludis/args/bad_argument.hh new file mode 100644 index 000000000..92660e82b --- /dev/null +++ b/0.8.0/paludis/args/bad_argument.hh @@ -0,0 +1,54 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2005, 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef PALUDIS_GUARD_ARGS_BAD_ARGUMENT_HH +#define PALUDIS_GUARD_ARGS_BAD_ARGUMENT_HH 1 + +#include <paludis/args/args_error.hh> + +/** \file + * Declaration for BadArgument. + * + * \ingroup grplibpaludisargs + * \ingroup grpexceptions + */ + +namespace paludis +{ + namespace args + { + /** + * Thrown if an unrecognised command line argument is specified. + * + * \ingroup grplibpaludisargs + * \ingroup grpexceptions + */ + class PALUDIS_VISIBLE BadArgument : + public ArgsError + { + public: + /** + * Constructor. + */ + BadArgument(const std::string & option) throw (); + }; + } +} + +#endif diff --git a/0.8.0/paludis/args/man.cc b/0.8.0/paludis/args/man.cc new file mode 100644 index 000000000..522cb0840 --- /dev/null +++ b/0.8.0/paludis/args/man.cc @@ -0,0 +1,81 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "man.hh" +#include <ostream> + +using namespace paludis; +using namespace paludis::args; +using std::endl; + +void +paludis::args::generate_man(std::ostream & f, const ArgsHandler * const h) +{ + f << ".TH \"" << h->app_name() << "\" " + << h->man_section() << endl; + + f << ".SH NAME" << endl; + f << h->app_name() << " \\- " + << h->app_synopsis() << endl; + + f << ".SH SYNOPSIS" << endl; + + for (ArgsHandler::UsageLineIterator u(h->begin_usage_lines()), + u_end(h->end_usage_lines()) ; u != u_end ; ++u) + f << ".B " << h->app_name() << " " << *u << endl << endl; + + f << ".SH DESCRIPTION" << endl; + f << h->app_description() << endl; + + f << ".SH OPTIONS" << endl; + + for (ArgsHandler::ArgsGroupsIterator a(h->begin_args_groups()), + a_end(h->end_args_groups()) ; a != a_end ; ++a) + { + f << ".SS \"" << (*a)->name() << "\"" << endl; + f << (*a)->description() << endl; + + for (paludis::args::ArgsGroup::Iterator b((*a)->begin()), b_end((*a)->end()) ; + b != b_end ; ++b) + { + f << ".TP" << endl; + f << ".B \""; + if ((*b)->short_name()) + f << "\\-" << (*b)->short_name() << " , "; + f << "\\-\\-" << (*b)->long_name() << "\"" << endl; + f << (*b)->description() << endl; + } + } + + if (h->begin_environment_lines() != + h->end_environment_lines()) + { + f << ".SH ENVIRONMENT" << endl; + + for (ArgsHandler::EnvironmentLineIterator a(h->begin_environment_lines()), + a_end(h->end_environment_lines()) ; a != a_end ; ++a) + { + f << ".TP" << endl; + f << ".B \"" << a->first << "\"" << endl; + f << a->second << endl; + } + + } +} + diff --git a/0.8.0/paludis/args/man.hh b/0.8.0/paludis/args/man.hh new file mode 100644 index 000000000..220707e1f --- /dev/null +++ b/0.8.0/paludis/args/man.hh @@ -0,0 +1,37 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef PALUDIS_GUARD_PALUDIS_ARGS_MAN_HH +#define PALUDIS_GUARD_PALUDIS_ARGS_MAN_HH 1 + +#include <paludis/args/args.hh> +#include <iosfwd> + +namespace paludis +{ + namespace args + { + /** + * Write a man page to an ostream. + */ + void generate_man(std::ostream &, const ArgsHandler * const h) PALUDIS_VISIBLE; + } +} + +#endif |