Author: jkupec
Date: Wed Jun 13 09:00:24 2007
New Revision: 5739
URL: http://svn.opensuse.org/viewcvs/zypp?rev=5739&view=rev
Log:
command enumeration/mapping class added
Added:
trunk/zypper/src/zypper-command.cc
trunk/zypper/src/zypper-command.h
Modified:
trunk/zypper/src/CMakeLists.txt
trunk/zypper/src/zypper.cc
Modified: trunk/zypper/src/CMakeLists.txt
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/zypper/src/CMakeLists.txt?rev=5739&r1=5738&r2=5739&view=diff
==============================================================================
--- trunk/zypper/src/CMakeLists.txt (original)
+++ trunk/zypper/src/CMakeLists.txt Wed Jun 13 09:00:24 2007
@@ -5,6 +5,7 @@
SET( zypper_SRCS
zypper.cc
+ zypper-command.cc
zypper-getopt.cc
zypper-sources.cc
zypper-misc.cc
@@ -17,6 +18,7 @@
SET (zypper_HEADERS
zypper.h
+ zypper-command.h
zypper-getopt.h
zypper-sources.h
zypper-misc.h
Added: trunk/zypper/src/zypper-command.cc
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/zypper/src/zypper-command.cc?rev=5739&view=auto
==============================================================================
--- trunk/zypper/src/zypper-command.cc (added)
+++ trunk/zypper/src/zypper-command.cc Wed Jun 13 09:00:24 2007
@@ -0,0 +1,57 @@
+#include <map>
+
+#include
+
+#include
+#include
+
+#include "zypper-command.h"
+
+// redefine _ gettext macro defined by ZYpp
+#ifdef _
+#undef _
+#endif
+#define _(MSG) ::gettext(MSG)
+
+
+static std::mapstd::string,ZypperCommand::Command _table;
+
+const ZypperCommand ZypperCommand::ADD_REPO(ZypperCommand::ADD_REPO_e);
+const ZypperCommand ZypperCommand::REMOVE_REPO(ZypperCommand::REMOVE_REPO_e);
+const ZypperCommand ZypperCommand::LIST_REPOS(ZypperCommand::LIST_REPOS_e);
+const ZypperCommand ZypperCommand::REFRESH(ZypperCommand::REFRESH_e);
+const ZypperCommand ZypperCommand::SEARCH(ZypperCommand::SEARCH_e);
+const ZypperCommand ZypperCommand::INSTALL(ZypperCommand::INSTALL_e);
+const ZypperCommand ZypperCommand::REMOVE(ZypperCommand::REMOVE_e);
+const ZypperCommand ZypperCommand::UPDATE(ZypperCommand::UPDATE_e);
+
+ZypperCommand::ZypperCommand(const std::string & strval_r)
+ : _command(parse(strval_r))
+{}
+
+ZypperCommand::Command ZypperCommand::parse(const std::string & strval_r)
+{
+ if (_table.empty())
+ {
+ // initialize it
+ _table["addrepo"] = _table["service-add"] = _table["sa"] = ZypperCommand::ADD_REPO_e;
+ _table["rmrepo"] = _table["service-delete"] = _table["sd"] = ZypperCommand::REMOVE_REPO_e;
+ _table["listrepos"] = _table["service-list"] = _table["sl"] = ZypperCommand::LIST_REPOS_e;
+ _table["refresh"] = _table["ref"] = ZypperCommand::REFRESH_e;
+ _table["search"] = _table["se"] = ZypperCommand::SEARCH_e;
+ _table["install"] = _table["in"] = ZypperCommand::INSTALL_e;
+ _table["remove"] = _table["rm"] = ZypperCommand::REMOVE_e;
+ _table["update"] = _table["up"] = ZypperCommand::UPDATE_e;
+ _table["NONE"] = _table["none"] = ZypperCommand::NONE_e;
+ }
+
+ std::mapstd::string,ZypperCommand::Command::const_iterator it
+ = _table.find(strval_r);
+ if (it == _table.end())
+ {
+ std::string message =
+ boost::str( boost::format(_("Unknown command '%s'")) % strval_r );
+ ZYPP_THROW(zypp::Exception(message));
+ }
+ return it->second;
+}
Added: trunk/zypper/src/zypper-command.h
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/zypper/src/zypper-command.h?rev=5739&view=auto
==============================================================================
--- trunk/zypper/src/zypper-command.h (added)
+++ trunk/zypper/src/zypper-command.h Wed Jun 13 09:00:24 2007
@@ -0,0 +1,56 @@
+#ifndef ZYPPERCOMMAND_H_
+#define ZYPPERCOMMAND_H_
+
+#include<iosfwd>
+#include<string>
+
+/**
+ * Enumeration of <b>zypper</b> commands with mapping of command aliases.
+ * The mapping includes <b>rug</b> equivalents as well.
+ */
+struct ZypperCommand
+{
+ static const ZypperCommand ADD_REPO;
+ static const ZypperCommand REMOVE_REPO;
+ static const ZypperCommand LIST_REPOS;
+ static const ZypperCommand REFRESH;
+ static const ZypperCommand SEARCH;
+ static const ZypperCommand INSTALL;
+ static const ZypperCommand REMOVE;
+ static const ZypperCommand UPDATE;
+
+ enum Command
+ {
+ NONE_e,
+ ADD_REPO_e,
+ REMOVE_REPO_e,
+ LIST_REPOS_e,
+ REFRESH_e,
+ SEARCH_e,
+ INSTALL_e,
+ REMOVE_e,
+ UPDATE_e,
+ };
+
+ ZypperCommand(Command command) : _command(command) {}
+
+ explicit ZypperCommand(const std::string & strval_r);
+
+ const Command toEnum() const { return _command; }
+
+ ZypperCommand::Command parse(const std::string & strval_r);
+
+ const std::string & asString() const;
+
+
+ Command _command;
+};
+
+inline std::ostream & operator<<( std::ostream & str, const ZypperCommand & obj )
+{ return str << obj.asString(); }
+
+inline bool operator==(const ZypperCommand & obj1, const ZypperCommand & obj2)
+{ return obj1._command == obj2._command; }
+
+
+#endif /*ZYPPERCOMMAND_H_*/
Modified: trunk/zypper/src/zypper.cc
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/zypper/src/zypper.cc?rev=5739&r1=5738&r2=5739&view=diff
==============================================================================
--- trunk/zypper/src/zypper.cc (original)
+++ trunk/zypper/src/zypper.cc Wed Jun 13 09:00:24 2007
@@ -32,6 +32,7 @@
#include "zypper-search.h"
#include "zypper-info.h"
#include "zypper-getopt.h"
+#include "zypper-command.h"
using namespace std;
//using namespace boost;
@@ -207,10 +208,12 @@
}
/// process one command from the OS shell or the zypper shell
-int one_command(const string& command, int argc, char **argv)
+int one_command(const string& command_str, int argc, char **argv)
{
// === command-specific options ===
+ ZypperCommand command(command_str);
+
struct option no_options = {0, 0, 0, 0};
struct option *specific_options = &no_options;
string specific_help;
@@ -225,7 +228,7 @@
"\t--disable-system-resolvables, -T\t\tDo not read system installed resolvables\n"
);
- if (command == "install" || command == "in") {
+ if (command.toEnum() == ZypperCommand::INSTALL_e) {
static struct option install_options[] = {
{"catalog", required_argument, 0, 'c'},
{"type", required_argument, 0, 't'},
@@ -241,7 +244,7 @@
"\t--no-confirm,-y\tDo not require user confirmation\n"
);
}
- else if (command == "remove" || command == "rm") {
+ else if (command.toEnum() == ZypperCommand::REMOVE_e) {
static struct option remove_options[] = {
{"type", required_argument, 0, 't'},
{"no-confirm", no_argument, 0, 'y'},
@@ -255,7 +258,7 @@
"\t--no-confirm,-y\tDo not require user confirmation\n"
);
}
- else if (command == "service-add" || command == "sa") {
+ else if (command.toEnum() == ZypperCommand::ADD_REPO_e) {
static struct option service_add_options[] = {
{"type", required_argument, 0, 't'},
{"disabled", no_argument, 0, 'd'},
@@ -278,7 +281,7 @@
"\t--no-refresh,-n\t\tDo not automatically refresh the metadata\n"
);
}
- else if (command == "service-list" || command == "sl") {
+ else if (command.toEnum() == ZypperCommand::LIST_REPOS_e) {
static struct option service_list_options[] = {
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
@@ -292,7 +295,7 @@
"This command has no options.\n"
);
}
- else if (command == "service-delete" || command == "sd") {
+ else if (command.toEnum() == ZypperCommand::REMOVE_REPO_e) {
static struct option service_delete_options[] = {
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
@@ -306,7 +309,7 @@
"This command has no options.\n"
);
}
- else if (command == "service-rename" || command == "sr") {
+ else if (command_str == "service-rename" || command_str == "sr") {
static struct option service_rename_options[] = {
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
@@ -320,7 +323,7 @@
"This command has no options.\n"
);
}
- else if (command == "refresh" || command == "ref") {
+ else if (command.toEnum() == ZypperCommand::REFRESH_e) {
static struct option refresh_options[] = {
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
@@ -332,7 +335,7 @@
"Refresh all installation sources found in the system.\n"
);
}
- else if (command == "list-updates" || command == "lu") {
+ else if (command_str == "list-updates" || command_str == "lu") {
static struct option list_updates_options[] = {
{"type", required_argument, 0, 't'},
{"help", no_argument, 0, 'h'},
@@ -348,7 +351,7 @@
"\t--type,-t\t\tType of resolvable (default: patch)\n"
);
}
- else if (command == "update" || command == "up") {
+ else if (command_str == "update" || command_str == "up") {
static struct option update_options[] = {
{"type", required_argument, 0, 't'},
{"no-confirm", no_argument, 0, 'y'},
@@ -364,7 +367,7 @@
"\t--skip-interactive\t\tSkip interactive updates\n"
);
}
- else if (command == "search" || command == "se") {
+ else if (command_str == "search" || command_str == "se") {
static struct option search_options[] = {
{"installed-only", no_argument, 0, 'i'},
{"uninstalled-only", no_argument, 0, 'u'},
@@ -404,7 +407,7 @@
"* and ? wildcards can also be used within search strings.\n"
);
}
- else if (command == "patch-check" || command == "pchk") {
+ else if (command_str == "patch-check" || command_str == "pchk") {
static struct option patch_check_options[] = {
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
@@ -418,7 +421,7 @@
"This command has no options.\n"
);
}
- else if (command == "patches" || command == "pch") {
+ else if (command_str == "patches" || command_str == "pch") {
static struct option patches_options[] = {
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
@@ -432,7 +435,7 @@
"This command has no options.\n"
);
}
- else if (command == "info" || command == "if") {
+ else if (command_str == "info" || command_str == "if") {
static struct option info_options[] = {
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
@@ -444,7 +447,7 @@
"'info' -- Show full information for packages\n"
);
}
- else if (command == "patch-info") {
+ else if (command_str == "patch-info") {
static struct option patch_info_options[] = {
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
@@ -456,7 +459,7 @@
"'patch-info' -- Show detailed information for patches\n"
);
}
- else if (command == "moo") {
+ else if (command_str == "moo") {
static struct option moo_options[] = {
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
@@ -468,9 +471,9 @@
"'moo' - Show an animal\n"
);
}
- else if (!command.empty()) { // empty command is treated earlier
- if (command != "help") // #235709
- cerr << _("Unknown command") << " '" << command << "'." << endl << endl;
+ else if (!command_str.empty()) { // empty command is treated earlier
+ if (command_str != "help") // #235709
+ cerr << _("Unknown command") << " '" << command_str << "'." << endl << endl;
cerr << help_commands;
return ZYPPER_EXIT_ERR_SYNTAX;
}
@@ -530,7 +533,7 @@
// here come commands that need the lock
try {
- if (command == "service-list" || command == "sl")
+ if (command_str == "service-list" || command_str == "sl")
zypp_readonly_hack::IWantIt (); // #247001
God = zypp::getZYpp();
@@ -549,14 +552,14 @@
// --------------------------( moo )----------------------------------------
- if (command == "moo") {
+ if (command_str == "moo") {
cout << " \\\\\\\\\\\n \\\\\\\\\\\\\\__o\n__\\\\\\\\\\\\\\'/_" << endl;
return ZYPPER_EXIT_OK;
}
// --------------------------( service list )-------------------------------
- else if (command == "service-list" || command == "sl")
+ else if (command.toEnum() == ZypperCommand::LIST_REPOS_e)
{
if (ghelp) {
cerr << specific_help << endl;
@@ -575,7 +578,7 @@
// --------------------------( service add )--------------------------------
- else if (command == "service-add" || command == "sa")
+ else if (command.toEnum() == ZypperCommand::ADD_REPO_e)
{
// TODO: repect values in .repo, have these as overrides
bool enabled = ! copts.count("disabled");
@@ -633,7 +636,7 @@
// --------------------------( service delete )-----------------------------
- else if (command == "service-delete" || command == "sd")
+ else if (command.toEnum() == ZypperCommand::REMOVE_REPO_e)
{
if (ghelp || arguments.size() < 1) {
cerr << specific_help;
@@ -657,7 +660,7 @@
// --------------------------( service rename )-----------------------------
- else if (command == "service-rename" || command == "sr")
+ else if (command_str == "service-rename" || command_str == "sr")
{
if (ghelp || arguments.size() < 2) {
cerr << specific_help;
@@ -681,21 +684,19 @@
// --------------------------( refresh )------------------------------------
- else if (command == "refresh" || command == "ref") {
- if (ghelp) {
- cerr << specific_help;
- return !ghelp;
- }
-
+ else if (command.toEnum() == ZypperCommand::REFRESH_e)
+ {
+ if (ghelp) { cout << specific_help; return !ghelp; }
+
refresh_sources();
}
// --------------------------( remove/install )-----------------------------
- else if (command == "install" || command == "in" ||
- command == "remove" || command == "rm") {
-
- if (command == "install" || command == "in") {
+ else if (command.toEnum() == ZypperCommand::INSTALL_e ||
+ command.toEnum() == ZypperCommand::REMOVE_e )
+ {
+ if (command.toEnum() == ZypperCommand::INSTALL_e) {
if (ghelp || arguments.size() < 1) {
cerr << "install [options] name...\n" << specific_help;
return !ghelp;
@@ -704,7 +705,7 @@
gData.packages_to_install = arguments;
}
- if (command == "remove" || command == "rm") {
+ if (command.toEnum() == ZypperCommand::REMOVE_e) {
if (ghelp || arguments.size() < 1) {
cerr << "remove [options] name...\n"
<< specific_help
@@ -744,7 +745,7 @@
cout_v << "DONE" << endl;
for ( vector<string>::const_iterator it = arguments.begin(); it != arguments.end(); ++it ) {
- if (command == "install" || command == "in") {
+ if (command.toEnum() == ZypperCommand::INSTALL_e) {
mark_for_install(kind, *it);
}
else {
@@ -760,7 +761,7 @@
// TODO -c, --catalog option
- else if (command == "search" || command == "se") {
+ else if (command_str == "search" || command_str == "se") {
ZyppSearchOptions options;
if (ghelp) {
@@ -814,7 +815,7 @@
// --------------------------( patch check )--------------------------------
// TODO: rug summary
- else if (command == "patch-check" || command == "pchk") {
+ else if (command_str == "patch-check" || command_str == "pchk") {
if (ghelp) {
cerr << specific_help;
return !ghelp;
@@ -841,7 +842,7 @@
// --------------------------( patches )------------------------------------
- else if (command == "patches" || command == "pch") {
+ else if (command_str == "patches" || command_str == "pch") {
if (ghelp) {
cerr << specific_help;
return !ghelp;
@@ -857,7 +858,7 @@
// --------------------------( list updates )-------------------------------
- else if (command == "list-updates" || command == "lu") {
+ else if (command_str == "list-updates" || command_str == "lu") {
if (ghelp) {
// FIXME catalog...
cerr << specific_help;
@@ -884,7 +885,7 @@
// -----------------------------( update )----------------------------------
- else if (command == "update" || command == "up") {
+ else if (command_str == "update" || command_str == "up") {
if (ghelp) {
cerr << "update [options]\n"
<< specific_help
@@ -916,7 +917,7 @@
// -----------------------------( info )------------------------------------
- else if (command == "info" || command == "if" || command == "patch-info") {
+ else if (command_str == "info" || command_str == "if" || command_str == "patch-info") {
if (ghelp || arguments.size() == 0) {
cerr << specific_help;
return !ghelp;
@@ -927,7 +928,7 @@
cond_load_resolvables ();
establish ();
- printInfo(command,arguments);
+ printInfo(command_str,arguments);
return ZYPPER_EXIT_OK;
}
--
To unsubscribe, e-mail: zypp-commit+unsubscribe@opensuse.org
For additional commands, e-mail: zypp-commit+help@opensuse.org