From: David Kalnischkies Date: Mon, 26 Oct 2015 10:42:32 +0000 (+0100) Subject: move apts cmdline helper type into -private X-Git-Tag: 1.1_exp15~21 X-Git-Url: https://git.saurik.com/apt.git/commitdiff_plain/6079b276a959086ff18302cab752b6d7cfe5ad9f move apts cmdline helper type into -private Its not as simple as I initially thought to abstract this enough to make it globally usable, so lets not pollute global namespace with this for now. Git-Dch: Ignore --- diff --git a/apt-pkg/contrib/cmndline.cc b/apt-pkg/contrib/cmndline.cc index d299bbbdf..eb48d1d75 100644 --- a/apt-pkg/contrib/cmndline.cc +++ b/apt-pkg/contrib/cmndline.cc @@ -83,43 +83,6 @@ char const * CommandLine::GetCommand(Dispatch const * const Map, return Map[j].Match; } return NULL; -} -char const * CommandLine::GetCommand(DispatchWithHelp const * const Map, - unsigned int const argc, char const * const * const argv) -{ - // if there is a -- on the line there must be the word we search for either - // before it (as -- marks the end of the options) or right after it (as we can't - // decide if the command is actually an option, given that in theory, you could - // have parameters named like commands) - for (size_t i = 1; i < argc; ++i) - { - if (strcmp(argv[i], "--") != 0) - continue; - // check if command is before -- - for (size_t k = 1; k < i; ++k) - for (size_t j = 0; Map[j].Match != NULL; ++j) - if (strcmp(argv[k], Map[j].Match) == 0) - return Map[j].Match; - // see if the next token after -- is the command - ++i; - if (i < argc) - for (size_t j = 0; Map[j].Match != NULL; ++j) - if (strcmp(argv[i], Map[j].Match) == 0) - return Map[j].Match; - // we found a --, but not a command - return NULL; - } - // no --, so search for the first word matching a command - // FIXME: How like is it that an option parameter will be also a valid Match ? - for (size_t i = 1; i < argc; ++i) - { - if (*(argv[i]) == '-') - continue; - for (size_t j = 0; Map[j].Match != NULL; ++j) - if (strcmp(argv[i], Map[j].Match) == 0) - return Map[j].Match; - } - return NULL; } /*}}}*/ // CommandLine::Parse - Main action member /*{{{*/ @@ -411,7 +374,7 @@ unsigned int CommandLine::FileSize() const } /*}}}*/ // CommandLine::DispatchArg - Do something with the first arg /*{{{*/ -bool CommandLine::DispatchArg(DispatchWithHelp const * const Map,bool NoMatch) +bool CommandLine::DispatchArg(Dispatch const * const Map,bool NoMatch) { int I; for (I = 0; Map[I].Match != 0; I++) @@ -436,26 +399,8 @@ bool CommandLine::DispatchArg(DispatchWithHelp const * const Map,bool NoMatch) } bool CommandLine::DispatchArg(Dispatch *Map,bool NoMatch) { - int I; - for (I = 0; Map[I].Match != 0; I++) - { - if (strcmp(FileList[0],Map[I].Match) == 0) - { - bool Res = Map[I].Handler(*this); - if (Res == false && _error->PendingError() == false) - _error->Error("Handler silently failed"); - return Res; - } - } - - // No matching name - if (Map[I].Match == 0) - { - if (NoMatch == true) - _error->Error(_("Invalid operation %s"),FileList[0]); - } - - return false; + Dispatch const * const Map2 = Map; + return DispatchArg(Map2, NoMatch); } /*}}}*/ // CommandLine::SaveInConfig - for output later in a logfile or so /*{{{*/ diff --git a/apt-pkg/contrib/cmndline.h b/apt-pkg/contrib/cmndline.h index 33d9f9f3a..805cb9eae 100644 --- a/apt-pkg/contrib/cmndline.h +++ b/apt-pkg/contrib/cmndline.h @@ -84,14 +84,12 @@ class CommandLine bool Parse(int argc,const char **argv); void ShowHelp(); unsigned int FileSize() const APT_PURE; + // FIXME: merge on next ABI break bool DispatchArg(Dispatch *List,bool NoMatch = true); - bool DispatchArg(DispatchWithHelp const * const List,bool NoMatch = true); + bool DispatchArg(Dispatch const * const List,bool NoMatch = true); static char const * GetCommand(Dispatch const * const Map, unsigned int const argc, char const * const * const argv) APT_PURE; - static char const * GetCommand(DispatchWithHelp const * const Map, - unsigned int const argc, char const * const * const argv) APT_PURE; - static CommandLine::Args MakeArgs(char ShortOpt, char const *LongOpt, char const *ConfName, unsigned long Flags) APT_CONST; @@ -117,11 +115,5 @@ struct CommandLine::Dispatch const char *Match; bool (*Handler)(CommandLine &); }; -struct CommandLine::DispatchWithHelp -{ - const char *Match; - bool (*Handler)(CommandLine &); - const char *Help; -}; #endif diff --git a/apt-private/private-cmndline.cc b/apt-private/private-cmndline.cc index c99a0afe0..d80168fda 100644 --- a/apt-private/private-cmndline.cc +++ b/apt-private/private-cmndline.cc @@ -383,7 +383,7 @@ static void BinarySpecificConfiguration(char const * const Binary) /*{{{*/ _config->MoveSubTree(conf.c_str(), NULL); } /*}}}*/ -std::vector ParseCommandLine(CommandLine &CmdL, APT_CMD const Binary,/*{{{*/ +std::vector ParseCommandLine(CommandLine &CmdL, APT_CMD const Binary,/*{{{*/ Configuration * const * const Cnf, pkgSystem ** const Sys, int const argc, const char *argv[]) { if (Cnf != NULL && pkgInitConfig(**Cnf) == false) @@ -395,7 +395,11 @@ std::vector ParseCommandLine(CommandLine &CmdL, A if (likely(argc != 0 && argv[0] != NULL)) BinarySpecificConfiguration(argv[0]); - std::vector const Cmds = GetCommands(); + std::vector const CmdsWithHelp = GetCommands(); + std::vector Cmds; + for (auto const& cmd : CmdsWithHelp) + Cmds.push_back({cmd.Match, cmd.Handler}); + // Args running out of scope invalidates the pointer stored in CmdL, // but we don't use the pointer after this function, so we ignore // this problem for now and figure something out if we have to. @@ -410,7 +414,7 @@ std::vector ParseCommandLine(CommandLine &CmdL, A (Sys != NULL && pkgInitSystem(*_config, *Sys) == false)) { if (_config->FindB("version") == true) - ShowHelp(CmdL, Cmds.data()); + ShowHelp(CmdL, CmdsWithHelp.data()); _error->DumpErrors(); exit(100); @@ -420,18 +424,18 @@ std::vector ParseCommandLine(CommandLine &CmdL, A if (_config->FindB("help") == true || _config->FindB("version") == true || (CmdL.FileSize() > 0 && strcmp(CmdL.FileList[0], "help") == 0)) { - ShowHelp(CmdL, Cmds.data()); + ShowHelp(CmdL, CmdsWithHelp.data()); exit(0); } if (Cmds.empty() == false && CmdL.FileSize() == 0) { - ShowHelp(CmdL, Cmds.data()); + ShowHelp(CmdL, CmdsWithHelp.data()); exit(1); } return Cmds; } /*}}}*/ -unsigned short DispatchCommandLine(CommandLine &CmdL, std::vector const &Cmds) /*{{{*/ +unsigned short DispatchCommandLine(CommandLine &CmdL, std::vector const &Cmds) /*{{{*/ { // Match the operation bool const returned = Cmds.empty() ? true : CmdL.DispatchArg(Cmds.data()); diff --git a/apt-private/private-cmndline.h b/apt-private/private-cmndline.h index aee679bf8..ac17e2e8a 100644 --- a/apt-private/private-cmndline.h +++ b/apt-private/private-cmndline.h @@ -23,12 +23,18 @@ enum class APT_CMD { APT_SORTPKG, }; -bool ShowHelp(CommandLine &CmdL, CommandLine::DispatchWithHelp const * Cmds); -std::vector GetCommands(); +struct aptDispatchWithHelp +{ + const char *Match; + bool (*Handler)(CommandLine &); + const char *Help; +}; +std::vector GetCommands(); +bool ShowHelp(CommandLine &CmdL, aptDispatchWithHelp const * Cmds); -APT_PUBLIC std::vector ParseCommandLine(CommandLine &CmdL, APT_CMD const Binary, +APT_PUBLIC std::vector ParseCommandLine(CommandLine &CmdL, APT_CMD const Binary, Configuration * const * const Cnf, pkgSystem ** const Sys, int const argc, const char * argv[]); -APT_PUBLIC unsigned short DispatchCommandLine(CommandLine &CmdL, std::vector const &Cmds); +APT_PUBLIC unsigned short DispatchCommandLine(CommandLine &CmdL, std::vector const &Cmds); APT_PUBLIC std::vector getCommandArgs(APT_CMD const Program, char const * const Cmd); diff --git a/cmdline/apt-cache.cc b/cmdline/apt-cache.cc index 2933db218..50af3f329 100644 --- a/cmdline/apt-cache.cc +++ b/cmdline/apt-cache.cc @@ -1522,8 +1522,7 @@ static bool GenCaches(CommandLine &) return CacheFile.BuildCaches(&Progress, true); } /*}}}*/ -// ShowHelp - Show a help screen /*{{{*/ -bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds) +bool ShowHelp(CommandLine &, aptDispatchWithHelp const * Cmds) /*{{{*/ { ioprintf(cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH); @@ -1558,7 +1557,7 @@ bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds) return true; } /*}}}*/ -std::vector GetCommands() /*{{{*/ +std::vector GetCommands() /*{{{*/ { return { {"gencaches",&GenCaches, nullptr}, diff --git a/cmdline/apt-cdrom.cc b/cmdline/apt-cdrom.cc index 397ff2b65..699cc550c 100644 --- a/cmdline/apt-cdrom.cc +++ b/cmdline/apt-cdrom.cc @@ -203,8 +203,7 @@ static bool DoIdent(CommandLine &) return AddOrIdent(false); } /*}}}*/ -// ShowHelp - Show the help screen /*{{{*/ -bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds) +bool ShowHelp(CommandLine &, aptDispatchWithHelp const * Cmds) /*{{{*/ { ioprintf(cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH); @@ -241,7 +240,7 @@ bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds) return true; } /*}}}*/ -std::vector GetCommands() /*{{{*/ +std::vector GetCommands() /*{{{*/ { return { {"add", &DoAdd, "Add a CDROM"}, diff --git a/cmdline/apt-config.cc b/cmdline/apt-config.cc index 9d80e4ebf..47e37c2f7 100644 --- a/cmdline/apt-config.cc +++ b/cmdline/apt-config.cc @@ -76,8 +76,7 @@ static bool DoDump(CommandLine &CmdL) return true; } /*}}}*/ -// ShowHelp - Show the help screen /*{{{*/ -bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds) +bool ShowHelp(CommandLine &, aptDispatchWithHelp const * Cmds) /*{{{*/ { ioprintf(cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH); if (_config->FindB("version") == true) @@ -104,7 +103,7 @@ bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds) return true; } /*}}}*/ -std::vector GetCommands() /*{{{*/ +std::vector GetCommands() /*{{{*/ { return { {"shell", &DoShell, _("get configuration values via shell evaluation")}, diff --git a/cmdline/apt-extracttemplates.cc b/cmdline/apt-extracttemplates.cc index c5c37d122..1b16542fe 100644 --- a/cmdline/apt-extracttemplates.cc +++ b/cmdline/apt-extracttemplates.cc @@ -215,8 +215,7 @@ bool DebFile::ParseInfo() return true; } /*}}}*/ -// ShowHelp - show a short help text /*{{{*/ -bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const *) +bool ShowHelp(CommandLine &, aptDispatchWithHelp const *) /*{{{*/ { ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH); @@ -341,7 +340,7 @@ static bool Go(CommandLine &CmdL) return !_error->PendingError(); } /*}}}*/ -std::vector GetCommands() /*{{{*/ +std::vector GetCommands() /*{{{*/ { return { {nullptr, nullptr, nullptr} diff --git a/cmdline/apt-get.cc b/cmdline/apt-get.cc index fd7f045c6..69b12b2c7 100644 --- a/cmdline/apt-get.cc +++ b/cmdline/apt-get.cc @@ -1526,8 +1526,7 @@ static bool DoIndexTargets(CommandLine &CmdL) return true; } /*}}}*/ -// ShowHelp - Show a help screen /*{{{*/ -bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds) +bool ShowHelp(CommandLine &, aptDispatchWithHelp const * Cmds) /*{{{*/ { ioprintf(cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH); @@ -1611,7 +1610,7 @@ bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds) return true; } /*}}}*/ -std::vector GetCommands() /*{{{*/ +std::vector GetCommands() /*{{{*/ { return { {"update", &DoUpdate, _("Retrieve new lists of packages")}, diff --git a/cmdline/apt-helper.cc b/cmdline/apt-helper.cc index aef10828d..b0c1ddacf 100644 --- a/cmdline/apt-helper.cc +++ b/cmdline/apt-helper.cc @@ -105,7 +105,7 @@ static bool DoSrvLookup(CommandLine &CmdL) /*{{{*/ return true; } /*}}}*/ -bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)/*{{{*/ +bool ShowHelp(CommandLine &, aptDispatchWithHelp const * Cmds) /*{{{*/ { ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH); @@ -132,7 +132,7 @@ bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)/*{{{*/ return true; } /*}}}*/ -std::vector GetCommands() /*{{{*/ +std::vector GetCommands() /*{{{*/ { return { {"download-file", &DoDownloadFile, _("download the given uri to the target-path")}, diff --git a/cmdline/apt-internal-solver.cc b/cmdline/apt-internal-solver.cc index f6eaa11f7..28989f6cd 100644 --- a/cmdline/apt-internal-solver.cc +++ b/cmdline/apt-internal-solver.cc @@ -41,8 +41,8 @@ #include /*}}}*/ -// ShowHelp - Show a help screen /*{{{*/ -bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const *) { +bool ShowHelp(CommandLine &, aptDispatchWithHelp const *) /*{{{*/ +{ ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH); std::cout << @@ -65,7 +65,7 @@ APT_NORETURN static void DIE(std::string const &message) { /*{{{*/ exit(EXIT_FAILURE); } /*}}}*/ -std::vector GetCommands() /*{{{*/ +std::vector GetCommands() /*{{{*/ { return {}; } diff --git a/cmdline/apt-mark.cc b/cmdline/apt-mark.cc index c49476c7c..d02e80beb 100644 --- a/cmdline/apt-mark.cc +++ b/cmdline/apt-mark.cc @@ -280,8 +280,7 @@ static bool ShowSelection(CommandLine &CmdL) /*{{{*/ return true; } /*}}}*/ -// ShowHelp - Show a help screen /*{{{*/ -bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds) +bool ShowHelp(CommandLine &, aptDispatchWithHelp const * Cmds) /*{{{*/ { ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH); @@ -314,7 +313,7 @@ bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds) return true; } /*}}}*/ -std::vector GetCommands() /*{{{*/ +std::vector GetCommands() /*{{{*/ { return { {"auto",&DoAuto, _("Mark the given packages as automatically installed")}, diff --git a/cmdline/apt-sortpkgs.cc b/cmdline/apt-sortpkgs.cc index 82c9c333d..b5a2c12df 100644 --- a/cmdline/apt-sortpkgs.cc +++ b/cmdline/apt-sortpkgs.cc @@ -132,8 +132,7 @@ static bool DoIt(string InFile) return true; } /*}}}*/ -// ShowHelp - Show the help text /*{{{*/ -bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const *) +bool ShowHelp(CommandLine &, aptDispatchWithHelp const *) /*{{{*/ { ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH); if (_config->FindB("version") == true) @@ -154,7 +153,7 @@ bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const *) return true; } /*}}}*/ -std::vector GetCommands() /*{{{*/ +std::vector GetCommands() /*{{{*/ { return { {nullptr, nullptr, nullptr} diff --git a/cmdline/apt.cc b/cmdline/apt.cc index e32a9f1e3..be2f7663e 100644 --- a/cmdline/apt.cc +++ b/cmdline/apt.cc @@ -37,7 +37,7 @@ #include /*}}}*/ -bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)/*{{{*/ +bool ShowHelp(CommandLine &, aptDispatchWithHelp const * Cmds) /*{{{*/ { ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH); @@ -58,7 +58,7 @@ bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)/*{{{*/ return true; } /*}}}*/ -std::vector GetCommands() /*{{{*/ +std::vector GetCommands() /*{{{*/ { return { // query diff --git a/ftparchive/apt-ftparchive.cc b/ftparchive/apt-ftparchive.cc index f53958ceb..0abb3e2dd 100644 --- a/ftparchive/apt-ftparchive.cc +++ b/ftparchive/apt-ftparchive.cc @@ -604,8 +604,7 @@ static void LoadBinDir(vector &PkgList,Configuration &Setup) } /*}}}*/ -// ShowHelp - Show the help text /*{{{*/ -bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const *) +bool ShowHelp(CommandLine &, aptDispatchWithHelp const *) /*{{{*/ { ioprintf(cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH); if (_config->FindB("version") == true) @@ -1023,7 +1022,7 @@ static bool Clean(CommandLine &CmdL) } /*}}}*/ -std::vector GetCommands() /*{{{*/ +std::vector GetCommands() /*{{{*/ { return { {"packages",&SimpleGenPackages, nullptr}, diff --git a/test/libapt/commandline_test.cc b/test/libapt/commandline_test.cc index 0da2ba45f..7f6c511df 100644 --- a/test/libapt/commandline_test.cc +++ b/test/libapt/commandline_test.cc @@ -17,18 +17,22 @@ class CLT: public CommandLine { } }; -#define EXPECT_CMD(x, ...) { const char * const argv[] = { __VA_ARGS__ }; EXPECT_EQ(x, CLT::AsString(argv, sizeof(argv)/sizeof(argv[0]))); } +bool ShowHelp(CommandLine &, aptDispatchWithHelp const *) {return false;} +std::vector GetCommands() {return {};} + TEST(CommandLineTest,SaveInConfig) { - EXPECT_CMD("apt-get install -sf", +#define APT_EXPECT_CMD(x, ...) { const char * const argv[] = { __VA_ARGS__ }; EXPECT_EQ(x, CLT::AsString(argv, sizeof(argv)/sizeof(argv[0]))); } + APT_EXPECT_CMD("apt-get install -sf", "apt-get", "install", "-sf"); - EXPECT_CMD("apt-cache -s apt -so Debug::test=Test", + APT_EXPECT_CMD("apt-cache -s apt -so Debug::test=Test", "apt-cache", "-s", "apt", "-so", "Debug::test=Test"); - EXPECT_CMD("apt-cache -s apt -so Debug::test=\"Das ist ein Test\"", + APT_EXPECT_CMD("apt-cache -s apt -so Debug::test=\"Das ist ein Test\"", "apt-cache", "-s", "apt", "-so", "Debug::test=Das ist ein Test"); - EXPECT_CMD("apt-cache -s apt --hallo test=1.0", + APT_EXPECT_CMD("apt-cache -s apt --hallo test=1.0", "apt-cache", "-s", "apt", "--hallo", "test=1.0"); +#undef APT_EXPECT_CMD } TEST(CommandLineTest,Parsing) { diff --git a/test/libapt/gtest_runner.cc b/test/libapt/gtest_runner.cc index 29f631326..46054afa3 100644 --- a/test/libapt/gtest_runner.cc +++ b/test/libapt/gtest_runner.cc @@ -1,10 +1,6 @@ #include #include -#include - -bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const *) {return false;} -std::vector GetCommands() {return {};} int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv);