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 /*{{{*/
}
/*}}}*/
// 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++)
}
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 /*{{{*/
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;
const char *Match;
bool (*Handler)(CommandLine &);
};
-struct CommandLine::DispatchWithHelp
-{
- const char *Match;
- bool (*Handler)(CommandLine &);
- const char *Help;
-};
#endif
_config->MoveSubTree(conf.c_str(), NULL);
}
/*}}}*/
-std::vector<CommandLine::DispatchWithHelp> ParseCommandLine(CommandLine &CmdL, APT_CMD const Binary,/*{{{*/
+std::vector<CommandLine::Dispatch> 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)
if (likely(argc != 0 && argv[0] != NULL))
BinarySpecificConfiguration(argv[0]);
- std::vector<CommandLine::DispatchWithHelp> const Cmds = GetCommands();
+ std::vector<aptDispatchWithHelp> const CmdsWithHelp = GetCommands();
+ std::vector<CommandLine::Dispatch> 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.
(Sys != NULL && pkgInitSystem(*_config, *Sys) == false))
{
if (_config->FindB("version") == true)
- ShowHelp(CmdL, Cmds.data());
+ ShowHelp(CmdL, CmdsWithHelp.data());
_error->DumpErrors();
exit(100);
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<CommandLine::DispatchWithHelp> const &Cmds) /*{{{*/
+unsigned short DispatchCommandLine(CommandLine &CmdL, std::vector<CommandLine::Dispatch> const &Cmds) /*{{{*/
{
// Match the operation
bool const returned = Cmds.empty() ? true : CmdL.DispatchArg(Cmds.data());
APT_SORTPKG,
};
-bool ShowHelp(CommandLine &CmdL, CommandLine::DispatchWithHelp const * Cmds);
-std::vector<CommandLine::DispatchWithHelp> GetCommands();
+struct aptDispatchWithHelp
+{
+ const char *Match;
+ bool (*Handler)(CommandLine &);
+ const char *Help;
+};
+std::vector<aptDispatchWithHelp> GetCommands();
+bool ShowHelp(CommandLine &CmdL, aptDispatchWithHelp const * Cmds);
-APT_PUBLIC std::vector<CommandLine::DispatchWithHelp> ParseCommandLine(CommandLine &CmdL, APT_CMD const Binary,
+APT_PUBLIC std::vector<CommandLine::Dispatch> 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<CommandLine::DispatchWithHelp> const &Cmds);
+APT_PUBLIC unsigned short DispatchCommandLine(CommandLine &CmdL, std::vector<CommandLine::Dispatch> const &Cmds);
APT_PUBLIC std::vector<CommandLine::Args> getCommandArgs(APT_CMD const Program, char const * const Cmd);
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);
return true;
}
/*}}}*/
-std::vector<CommandLine::DispatchWithHelp> GetCommands() /*{{{*/
+std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
{
return {
{"gencaches",&GenCaches, nullptr},
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);
return true;
}
/*}}}*/
-std::vector<CommandLine::DispatchWithHelp> GetCommands() /*{{{*/
+std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
{
return {
{"add", &DoAdd, "Add a CDROM"},
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)
return true;
}
/*}}}*/
-std::vector<CommandLine::DispatchWithHelp> GetCommands() /*{{{*/
+std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
{
return {
{"shell", &DoShell, _("get configuration values via shell evaluation")},
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);
return !_error->PendingError();
}
/*}}}*/
-std::vector<CommandLine::DispatchWithHelp> GetCommands() /*{{{*/
+std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
{
return {
{nullptr, nullptr, nullptr}
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);
return true;
}
/*}}}*/
-std::vector<CommandLine::DispatchWithHelp> GetCommands() /*{{{*/
+std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
{
return {
{"update", &DoUpdate, _("Retrieve new lists of packages")},
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);
return true;
}
/*}}}*/
-std::vector<CommandLine::DispatchWithHelp> GetCommands() /*{{{*/
+std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
{
return {
{"download-file", &DoDownloadFile, _("download the given uri to the target-path")},
#include <apti18n.h>
/*}}}*/
-// 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 <<
exit(EXIT_FAILURE);
}
/*}}}*/
-std::vector<CommandLine::DispatchWithHelp> GetCommands() /*{{{*/
+std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
{
return {};
}
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);
return true;
}
/*}}}*/
-std::vector<CommandLine::DispatchWithHelp> GetCommands() /*{{{*/
+std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
{
return {
{"auto",&DoAuto, _("Mark the given packages as automatically installed")},
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)
return true;
}
/*}}}*/
-std::vector<CommandLine::DispatchWithHelp> GetCommands() /*{{{*/
+std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
{
return {
{nullptr, nullptr, nullptr}
#include <apti18n.h>
/*}}}*/
-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);
return true;
}
/*}}}*/
-std::vector<CommandLine::DispatchWithHelp> GetCommands() /*{{{*/
+std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
{
return {
// query
}
/*}}}*/
-// 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)
}
/*}}}*/
-std::vector<CommandLine::DispatchWithHelp> GetCommands() /*{{{*/
+std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
{
return {
{"packages",&SimpleGenPackages, nullptr},
}
};
-#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<aptDispatchWithHelp> 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)
{
#include <gtest/gtest.h>
#include <apt-pkg/error.h>
-#include <apt-pkg/cmndline.h>
-
-bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const *) {return false;}
-std::vector<CommandLine::DispatchWithHelp> GetCommands() {return {};}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);