ソースを参照

generate commands array after config is loaded

This ensures that location strings loaded from a location specified via
configuration (Dir::Locale) effect the help messages for commands.

Git-Dch: Ignore
David Kalnischkies 10 年 前
コミット
011188e392

+ 46 - 41
apt-private/private-cmndline.cc

@@ -149,6 +149,11 @@ static bool addArgumentsAPTFTPArchive(std::vector<CommandLine::Args> &Args, char
 }
 									/*}}}*/
 static bool addArgumentsAPTInternalSolver(std::vector<CommandLine::Args> &, char const * const)/*{{{*/
+{
+   return true;
+}
+									/*}}}*/
+static bool addArgumentsAPTHelper(std::vector<CommandLine::Args> &, char const * const)/*{{{*/
 {
    return true;
 }
@@ -315,34 +320,32 @@ static bool addArgumentsAPT(std::vector<CommandLine::Args> &Args, char const * c
    return true;
 }
 									/*}}}*/
-std::vector<CommandLine::Args> getCommandArgs(char const * const Program, char const * const Cmd)/*{{{*/
+std::vector<CommandLine::Args> getCommandArgs(APT_CMD const Program, char const * const Cmd)/*{{{*/
 {
    std::vector<CommandLine::Args> Args;
    Args.reserve(50);
-   if (Program == NULL || Cmd == NULL)
-      ; // FIXME: Invalid command supplied
+   if (Cmd == nullptr)
+   {
+      if (Program == APT_CMD::APT_EXTRACTTEMPLATES)
+	 addArgumentsAPTExtractTemplates(Args, Cmd);
+   }
    else if (strcmp(Cmd, "help") == 0)
       ; // no options for help so no need to implement it in each
-   else if (strcmp(Program, "apt-get") == 0)
-      addArgumentsAPTGet(Args, Cmd);
-   else if (strcmp(Program, "apt-cache") == 0)
-      addArgumentsAPTCache(Args, Cmd);
-   else if (strcmp(Program, "apt-cdrom") == 0)
-      addArgumentsAPTCDROM(Args, Cmd);
-   else if (strcmp(Program, "apt-config") == 0)
-      addArgumentsAPTConfig(Args, Cmd);
-   else if (strcmp(Program, "apt-extracttemplates") == 0)
-      addArgumentsAPTExtractTemplates(Args, Cmd);
-   else if (strcmp(Program, "apt-ftparchive") == 0)
-      addArgumentsAPTFTPArchive(Args, Cmd);
-   else if (strcmp(Program, "apt-internal-solver") == 0)
-      addArgumentsAPTInternalSolver(Args, Cmd);
-   else if (strcmp(Program, "apt-mark") == 0)
-      addArgumentsAPTMark(Args, Cmd);
-   else if (strcmp(Program, "apt-sortpkg") == 0)
-      addArgumentsAPTSortPkgs(Args, Cmd);
-   else if (strcmp(Program, "apt") == 0)
-      addArgumentsAPT(Args, Cmd);
+   else
+      switch (Program)
+      {
+	 case APT_CMD::APT: addArgumentsAPT(Args, Cmd); break;
+	 case APT_CMD::APT_GET: addArgumentsAPTGet(Args, Cmd); break;
+	 case APT_CMD::APT_CACHE: addArgumentsAPTCache(Args, Cmd); break;
+	 case APT_CMD::APT_CDROM: addArgumentsAPTCDROM(Args, Cmd); break;
+	 case APT_CMD::APT_CONFIG: addArgumentsAPTConfig(Args, Cmd); break;
+	 case APT_CMD::APT_EXTRACTTEMPLATES: addArgumentsAPTExtractTemplates(Args, Cmd); break;
+	 case APT_CMD::APT_FTPARCHIVE: addArgumentsAPTFTPArchive(Args, Cmd); break;
+	 case APT_CMD::APT_HELPER: addArgumentsAPTHelper(Args, Cmd); break;
+	 case APT_CMD::APT_INTERNAL_SOLVER: addArgumentsAPTInternalSolver(Args, Cmd); break;
+	 case APT_CMD::APT_MARK: addArgumentsAPTMark(Args, Cmd); break;
+	 case APT_CMD::APT_SORTPKG: addArgumentsAPTSortPkgs(Args, Cmd); break;
+      }
 
    // options without a command
    addArg('h', "help", "help", 0);
@@ -380,19 +383,9 @@ static void BinarySpecificConfiguration(char const * const Binary)	/*{{{*/
    _config->MoveSubTree(conf.c_str(), NULL);
 }
 									/*}}}*/
-void ParseCommandLine(CommandLine &CmdL, CommandLine::DispatchWithHelp const * Cmds, char const * const Binary,/*{{{*/
-      Configuration * const * const Cnf, pkgSystem ** const Sys, int const argc, const char *argv[], bool(*ShowHelp)(CommandLine &, CommandLine::DispatchWithHelp const *))
+std::vector<CommandLine::DispatchWithHelp> ParseCommandLine(CommandLine &CmdL, APT_CMD const Binary,/*{{{*/
+      Configuration * const * const Cnf, pkgSystem ** const Sys, int const argc, const char *argv[])
 {
-   // 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.
-   std::vector<CommandLine::Args> Args;
-   if (Cmds != nullptr && Cmds[0].Handler != nullptr)
-      Args = getCommandArgs(Binary, CommandLine::GetCommand(Cmds, argc, argv));
-   else
-      Args = getCommandArgs(Binary, Binary);
-   CmdL = CommandLine(Args.data(), _config);
-
    if (Cnf != NULL && pkgInitConfig(**Cnf) == false)
    {
       _error->DumpErrors();
@@ -402,11 +395,22 @@ void ParseCommandLine(CommandLine &CmdL, CommandLine::DispatchWithHelp const * C
    if (likely(argc != 0 && argv[0] != NULL))
       BinarySpecificConfiguration(argv[0]);
 
+   std::vector<CommandLine::DispatchWithHelp> const Cmds = GetCommands();
+   // 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.
+   std::vector<CommandLine::Args> Args;
+   if (Cmds.empty() == false && Cmds[0].Handler != nullptr)
+      Args = getCommandArgs(Binary, CommandLine::GetCommand(Cmds.data(), argc, argv));
+   else
+      Args = getCommandArgs(Binary, nullptr);
+   CmdL = CommandLine(Args.data(), _config);
+
    if (CmdL.Parse(argc,argv) == false ||
        (Sys != NULL && pkgInitSystem(*_config, *Sys) == false))
    {
       if (_config->FindB("version") == true)
-	 ShowHelp(CmdL, Cmds);
+	 ShowHelp(CmdL, Cmds.data());
 
       _error->DumpErrors();
       exit(100);
@@ -416,20 +420,21 @@ void ParseCommandLine(CommandLine &CmdL, CommandLine::DispatchWithHelp const * C
    if (_config->FindB("help") == true || _config->FindB("version") == true ||
 	 (CmdL.FileSize() > 0 && strcmp(CmdL.FileList[0], "help") == 0))
    {
-      ShowHelp(CmdL, Cmds);
+      ShowHelp(CmdL, Cmds.data());
       exit(0);
    }
-   if (Cmds != nullptr && CmdL.FileSize() == 0)
+   if (Cmds.empty() == false && CmdL.FileSize() == 0)
    {
-      ShowHelp(CmdL, Cmds);
+      ShowHelp(CmdL, Cmds.data());
       exit(1);
    }
+   return Cmds;
 }
 									/*}}}*/
-unsigned short DispatchCommandLine(CommandLine &CmdL, CommandLine::DispatchWithHelp const * const Cmds)	/*{{{*/
+unsigned short DispatchCommandLine(CommandLine &CmdL, std::vector<CommandLine::DispatchWithHelp> const &Cmds)	/*{{{*/
 {
    // Match the operation
-   bool const returned = (Cmds != nullptr) ? CmdL.DispatchArg(Cmds) : true;
+   bool const returned = Cmds.empty() ? true : CmdL.DispatchArg(Cmds.data());
 
    // Print any errors or warnings found during parsing
    bool const Errors = _error->PendingError();

+ 22 - 5
apt-private/private-cmndline.h

@@ -9,10 +9,27 @@
 class Configuration;
 class pkgSystem;
 
-APT_PUBLIC std::vector<CommandLine::Args> getCommandArgs(char const * const Program, char const * const Cmd);
-APT_PUBLIC void ParseCommandLine(CommandLine &CmdL, CommandLine::DispatchWithHelp const * Cmds, char const * const Binary,
-      Configuration * const * const Cnf, pkgSystem ** const Sys, int const argc, const char * argv[],
-      bool(*ShowHelp)(CommandLine &, CommandLine::DispatchWithHelp const *));
-APT_PUBLIC unsigned short DispatchCommandLine(CommandLine &CmdL, CommandLine::DispatchWithHelp const * const Cmds);
+enum class APT_CMD {
+   APT,
+   APT_GET,
+   APT_CACHE,
+   APT_CDROM,
+   APT_CONFIG,
+   APT_EXTRACTTEMPLATES,
+   APT_FTPARCHIVE,
+   APT_HELPER,
+   APT_INTERNAL_SOLVER,
+   APT_MARK,
+   APT_SORTPKG,
+};
+
+bool ShowHelp(CommandLine &CmdL, CommandLine::DispatchWithHelp const * Cmds);
+std::vector<CommandLine::DispatchWithHelp> GetCommands();
+
+APT_PUBLIC std::vector<CommandLine::DispatchWithHelp> 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 std::vector<CommandLine::Args> getCommandArgs(APT_CMD const Program, char const * const Cmd);
 
 #endif

+ 9 - 6
cmdline/apt-cache.cc

@@ -1523,7 +1523,7 @@ static bool GenCaches(CommandLine &)
 }
 									/*}}}*/
 // ShowHelp - Show a help screen					/*{{{*/
-static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
+bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
 {
    ioprintf(cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
 
@@ -1558,11 +1558,9 @@ static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
    return true;
 }
 									/*}}}*/
-int main(int argc,const char *argv[])					/*{{{*/
+std::vector<CommandLine::DispatchWithHelp> GetCommands()		/*{{{*/
 {
-   InitLocale();
-
-   CommandLine::DispatchWithHelp Cmds[] =  {
+   return {
       {"gencaches",&GenCaches, nullptr},
       {"showsrc",&ShowSrcPackage, _("Show source records")},
       {"showpkg",&DumpPackage, nullptr},
@@ -1582,10 +1580,15 @@ int main(int argc,const char *argv[])					/*{{{*/
       {"madison",&Madison, nullptr},
       {nullptr, nullptr, nullptr}
    };
+}
+									/*}}}*/
+int main(int argc,const char *argv[])					/*{{{*/
+{
+   InitLocale();
 
    // Parse the command line and initialize the package library
    CommandLine CmdL;
-   ParseCommandLine(CmdL, Cmds, "apt-cache", &_config, &_system, argc, argv, ShowHelp);
+   auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_CACHE, &_config, &_system, argc, argv);
 
    InitOutput();
 

+ 9 - 6
cmdline/apt-cdrom.cc

@@ -204,7 +204,7 @@ static bool DoIdent(CommandLine &)
 }
 									/*}}}*/
 // ShowHelp - Show the help screen					/*{{{*/
-static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
+bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
 {
    ioprintf(cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
 
@@ -241,19 +241,22 @@ static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
    return true;
 }
 									/*}}}*/
-int main(int argc,const char *argv[])					/*{{{*/
+std::vector<CommandLine::DispatchWithHelp> GetCommands()		/*{{{*/
 {
-   InitLocale();
-
-   CommandLine::DispatchWithHelp Cmds[] = {
+   return {
       {"add", &DoAdd, "Add a CDROM"},
       {"ident", &DoIdent, "Report the identity of a CDROM"},
       {nullptr, nullptr, nullptr}
    };
+}
+									/*}}}*/
+int main(int argc,const char *argv[])					/*{{{*/
+{
+   InitLocale();
 
    // Parse the command line and initialize the package library
    CommandLine CmdL;
-   ParseCommandLine(CmdL, Cmds, "apt-cdrom", &_config, &_system, argc, argv, ShowHelp);
+   auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_CDROM, &_config, &_system, argc, argv);
 
    InitOutput();
 

+ 9 - 8
cmdline/apt-config.cc

@@ -77,9 +77,7 @@ static bool DoDump(CommandLine &CmdL)
 }
 									/*}}}*/
 // ShowHelp - Show the help screen					/*{{{*/
-// ---------------------------------------------------------------------
-/* */
-static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
+bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
 {
    ioprintf(cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
    if (_config->FindB("version") == true)
@@ -106,19 +104,22 @@ static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
    return true;
 }
 									/*}}}*/
-int main(int argc,const char *argv[])					/*{{{*/
+std::vector<CommandLine::DispatchWithHelp> GetCommands()		/*{{{*/
 {
-   InitLocale();
-
-   CommandLine::DispatchWithHelp Cmds[] = {
+   return {
       {"shell", &DoShell, _("get configuration values via shell evaluation")},
       {"dump", &DoDump, _("show the active configuration setting")},
       {nullptr, nullptr, nullptr}
    };
+}
+									/*}}}*/
+int main(int argc,const char *argv[])					/*{{{*/
+{
+   InitLocale();
 
    // Parse the command line and initialize the package library
    CommandLine CmdL;
-   ParseCommandLine(CmdL, Cmds, "apt-config", &_config, &_system, argc, argv, ShowHelp);
+   auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_CONFIG, &_config, &_system, argc, argv);
 
    std::vector<std::string> const langs = APT::Configuration::getLanguages(true);
    _config->Clear("Acquire::Languages");

+ 10 - 7
cmdline/apt-extracttemplates.cc

@@ -216,9 +216,7 @@ bool DebFile::ParseInfo()
 }
 									/*}}}*/
 // ShowHelp - show a short help text					/*{{{*/
-// ---------------------------------------------------------------------
-/* */
-static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const *)
+bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const *)
 {
 	ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
 
@@ -343,17 +341,22 @@ static bool Go(CommandLine &CmdL)
 	return !_error->PendingError();
 }
 									/*}}}*/
+std::vector<CommandLine::DispatchWithHelp> GetCommands()		/*{{{*/
+{
+   return {
+	{nullptr, nullptr, nullptr}
+   };
+}
+									/*}}}*/
 int main(int argc, const char **argv)					/*{{{*/
 {
 	InitLocale();
 
-	// Parse the command line and initialize the package library
-	CommandLine::DispatchWithHelp Cmds[] = {{nullptr, nullptr, nullptr}};
 	CommandLine CmdL;
-	ParseCommandLine(CmdL, Cmds, "apt-extracttemplates", &_config, &_system, argc, argv, ShowHelp);
+	auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_EXTRACTTEMPLATES, &_config, &_system, argc, argv);
 
 	Go(CmdL);
 
-	return DispatchCommandLine(CmdL, nullptr);
+	return DispatchCommandLine(CmdL, {});
 }
 									/*}}}*/

+ 9 - 8
cmdline/apt-get.cc

@@ -1527,9 +1527,7 @@ static bool DoIndexTargets(CommandLine &CmdL)
 }
 									/*}}}*/
 // ShowHelp - Show a help screen					/*{{{*/
-// ---------------------------------------------------------------------
-/* */
-static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
+bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
 {
    ioprintf(cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
 
@@ -1613,11 +1611,9 @@ static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
    return true;
 }
 									/*}}}*/
-int main(int argc,const char *argv[])					/*{{{*/
+std::vector<CommandLine::DispatchWithHelp> GetCommands()		/*{{{*/
 {
-   InitLocale();
-
-   CommandLine::DispatchWithHelp Cmds[] = {
+   return {
       {"update", &DoUpdate, _("Retrieve new lists of packages")},
       {"upgrade", &DoUpgrade, _("Perform an upgrade")},
       {"install", &DoInstall, _("Install new packages (pkg is libc6 not libc6.deb)")},
@@ -1642,10 +1638,15 @@ int main(int argc,const char *argv[])					/*{{{*/
       {"moo", &DoMoo, nullptr},
       {nullptr, nullptr, nullptr}
    };
+}
+									/*}}}*/
+int main(int argc,const char *argv[])					/*{{{*/
+{
+   InitLocale();
 
    // Parse the command line and initialize the package library
    CommandLine CmdL;
-   ParseCommandLine(CmdL, Cmds, "apt-get", &_config, &_system, argc, argv, ShowHelp);
+   auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_GET, &_config, &_system, argc, argv);
 
    InitSignals();
    InitOutput();

+ 16 - 15
cmdline/apt-helper.cc

@@ -32,7 +32,7 @@
 #include <apti18n.h>
 									/*}}}*/
 
-static bool DoAutoDetectProxy(CommandLine &CmdL)
+static bool DoAutoDetectProxy(CommandLine &CmdL)			/*{{{*/
 {
    if (CmdL.FileSize() != 2)
       return _error->Error(_("Need one URL as argument"));
@@ -44,8 +44,8 @@ static bool DoAutoDetectProxy(CommandLine &CmdL)
 
    return true;
 }
-
-static bool DoDownloadFile(CommandLine &CmdL)
+									/*}}}*/
+static bool DoDownloadFile(CommandLine &CmdL)				/*{{{*/
 {
    if (CmdL.FileSize() <= 2)
       return _error->Error(_("Must specify at least one pair url/filename"));
@@ -77,8 +77,8 @@ static bool DoDownloadFile(CommandLine &CmdL)
 
    return true;
 }
-
-static bool DoSrvLookup(CommandLine &CmdL)
+									/*}}}*/
+static bool DoSrvLookup(CommandLine &CmdL)				/*{{{*/
 {
    if (CmdL.FileSize() <= 1)
       return _error->Error("Must specify at least one SRV record");
@@ -104,8 +104,8 @@ static bool DoSrvLookup(CommandLine &CmdL)
    }
    return true;
 }
-
-static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const  * Cmds)
+									/*}}}*/
+bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const  * Cmds)/*{{{*/
 {
    ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
 
@@ -131,22 +131,23 @@ static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const  * Cmds)
       _("This APT helper has Super Meep Powers.") << std::endl;
    return true;
 }
-
-
-int main(int argc,const char *argv[])					/*{{{*/
+									/*}}}*/
+std::vector<CommandLine::DispatchWithHelp> GetCommands()		/*{{{*/
 {
-   InitLocale();
-
-   CommandLine::DispatchWithHelp Cmds[] = {
+   return {
       {"download-file", &DoDownloadFile, _("download the given uri to the target-path")},
       {"srv-lookup", &DoSrvLookup, _("lookup a SRV record (e.g. _http._tcp.ftp.debian.org)")},
       {"auto-detect-proxy", &DoAutoDetectProxy, _("detect proxy using apt.conf")},
       {nullptr, nullptr, nullptr}
    };
+}
+									/*}}}*/
+int main(int argc,const char *argv[])					/*{{{*/
+{
+   InitLocale();
 
-   // Parse the command line and initialize the package library
    CommandLine CmdL;
-   ParseCommandLine(CmdL, Cmds, "apt-helper", &_config, &_system, argc, argv, ShowHelp);
+   auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_HELPER, &_config, &_system, argc, argv);
 
    InitOutput();
 

+ 8 - 5
cmdline/apt-internal-solver.cc

@@ -42,9 +42,7 @@
 									/*}}}*/
 
 // ShowHelp - Show a help screen					/*{{{*/
-// ---------------------------------------------------------------------
-/* */
-static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const *) {
+bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const *) {
 	ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
 
 	std::cout <<
@@ -67,6 +65,11 @@ APT_NORETURN static void DIE(std::string const &message) {		/*{{{*/
 	exit(EXIT_FAILURE);
 }
 									/*}}}*/
+std::vector<CommandLine::DispatchWithHelp> GetCommands()		/*{{{*/
+{
+   return {};
+}
+									/*}}}*/
 int main(int argc,const char *argv[])					/*{{{*/
 {
 	InitLocale();
@@ -75,7 +78,7 @@ int main(int argc,const char *argv[])					/*{{{*/
 	DropPrivileges();
 
 	CommandLine CmdL;
-	ParseCommandLine(CmdL, nullptr, "apt-internal-solver", &_config, NULL, argc, argv, ShowHelp);
+	ParseCommandLine(CmdL, APT_CMD::APT_INTERNAL_SOLVER, &_config, NULL, argc, argv);
 
 	if (CmdL.FileList[0] != 0 && strcmp(CmdL.FileList[0], "scenario") == 0)
 	{
@@ -182,6 +185,6 @@ int main(int argc,const char *argv[])					/*{{{*/
 
 	EDSP::WriteProgress(100, "Done", output);
 
-	return DispatchCommandLine(CmdL, nullptr);
+	return DispatchCommandLine(CmdL, {});
 }
 									/*}}}*/

+ 9 - 6
cmdline/apt-mark.cc

@@ -281,7 +281,7 @@ static bool ShowSelection(CommandLine &CmdL)				/*{{{*/
 }
 									/*}}}*/
 // ShowHelp - Show a help screen					/*{{{*/
-static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
+bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
 {
    ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
 
@@ -314,11 +314,9 @@ static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
    return true;
 }
 									/*}}}*/
-int main(int argc,const char *argv[])					/*{{{*/
+std::vector<CommandLine::DispatchWithHelp> GetCommands()		/*{{{*/
 {
-   InitLocale();
-
-   CommandLine::DispatchWithHelp Cmds[] = {
+   return {
       {"auto",&DoAuto, _("Mark the given packages as automatically installed")},
       {"manual",&DoAuto, _("Mark the given packages as manually installed")},
       {"hold",&DoSelection, _("Mark a package as held back")},
@@ -339,9 +337,14 @@ int main(int argc,const char *argv[])					/*{{{*/
       {"unmarkauto", &DoMarkAuto, nullptr},
       {nullptr, nullptr, nullptr}
    };
+}
+									/*}}}*/
+int main(int argc,const char *argv[])					/*{{{*/
+{
+   InitLocale();
 
    CommandLine CmdL;
-   ParseCommandLine(CmdL, Cmds, "apt-mark", &_config, &_system, argc, argv, ShowHelp);
+   auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_MARK, &_config, &_system, argc, argv);
 
    InitOutput();
 

+ 10 - 7
cmdline/apt-sortpkgs.cc

@@ -133,9 +133,7 @@ static bool DoIt(string InFile)
 }
 									/*}}}*/
 // ShowHelp - Show the help text					/*{{{*/
-// ---------------------------------------------------------------------
-/* */
-static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const *)
+bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const *)
 {
    ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
    if (_config->FindB("version") == true)
@@ -156,20 +154,25 @@ static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const *)
    return true;
 }
 									/*}}}*/
+std::vector<CommandLine::DispatchWithHelp> GetCommands()		/*{{{*/
+{
+   return {
+      {nullptr, nullptr, nullptr}
+   };
+}
+									/*}}}*/
 int main(int argc,const char *argv[])					/*{{{*/
 {
    InitLocale();
 
-   // Parse the command line and initialize the package library
-   CommandLine::DispatchWithHelp Cmds[] = {{nullptr, nullptr, nullptr}};
    CommandLine CmdL;
-   ParseCommandLine(CmdL, Cmds, "apt-sortpkgs", &_config, &_system, argc, argv, ShowHelp);
+   ParseCommandLine(CmdL, APT_CMD::APT_SORTPKG, &_config, &_system, argc, argv);
 
    // Match the operation
    for (unsigned int I = 0; I != CmdL.FileSize(); I++)
       if (DoIt(CmdL.FileList[I]) == false)
 	 break;
 
-   return DispatchCommandLine(CmdL, nullptr);
+   return DispatchCommandLine(CmdL, {});
 }
 									/*}}}*/

+ 10 - 7
cmdline/apt.cc

@@ -37,7 +37,7 @@
 #include <apti18n.h>
 									/*}}}*/
 
-static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
+bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)/*{{{*/
 {
    ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
 
@@ -57,12 +57,10 @@ static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
 
    return true;
 }
-
-int main(int argc, const char *argv[])					/*{{{*/
+									/*}}}*/
+std::vector<CommandLine::DispatchWithHelp> GetCommands()		/*{{{*/
 {
-   InitLocale();
-
-   CommandLine::DispatchWithHelp Cmds[] = {
+   return {
       // query
       {"list", &DoList, _("list packages based on package names")},
       {"search", &DoSearch, _("search in package descriptions")},
@@ -86,9 +84,14 @@ int main(int argc, const char *argv[])					/*{{{*/
       {"moo", &DoMoo, nullptr},
       {nullptr, nullptr, nullptr}
    };
+}
+									/*}}}*/
+int main(int argc, const char *argv[])					/*{{{*/
+{
+   InitLocale();
 
    CommandLine CmdL;
-   ParseCommandLine(CmdL, Cmds, "apt", &_config, &_system, argc, argv, ShowHelp);
+   auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT, &_config, &_system, argc, argv);
 
    int const quiet = _config->FindI("quiet", 0);
    if (quiet == 2)

+ 10 - 8
ftparchive/apt-ftparchive.cc

@@ -605,9 +605,7 @@ static void LoadBinDir(vector<PackageMap> &PkgList,Configuration &Setup)
 									/*}}}*/
 
 // ShowHelp - Show the help text					/*{{{*/
-// ---------------------------------------------------------------------
-/* */
-static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const *)
+bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const *)
 {
    ioprintf(cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
    if (_config->FindB("version") == true)
@@ -1025,11 +1023,9 @@ static bool Clean(CommandLine &CmdL)
 }
 									/*}}}*/
 
-int main(int argc, const char *argv[])
+std::vector<CommandLine::DispatchWithHelp> GetCommands()		/*{{{*/
 {
-   InitLocale();
-
-   CommandLine::DispatchWithHelp Cmds[] = {
+   return {
       {"packages",&SimpleGenPackages, nullptr},
       {"contents",&SimpleGenContents, nullptr},
       {"sources",&SimpleGenSources, nullptr},
@@ -1038,10 +1034,15 @@ int main(int argc, const char *argv[])
       {"clean",&Clean, nullptr},
       {nullptr, nullptr, nullptr}
    };
+}
+									/*}}}*/
+int main(int argc, const char *argv[])					/*{{{*/
+{
+   InitLocale();
 
    // Parse the command line and initialize the package library
    CommandLine CmdL;
-   ParseCommandLine(CmdL, Cmds, "apt-ftparchive", &_config, NULL, argc, argv, ShowHelp);
+   auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_FTPARCHIVE, &_config, NULL, argc, argv);
 
    _config->CndSet("quiet",0);
    Quiet = _config->FindI("quiet",0);
@@ -1049,3 +1050,4 @@ int main(int argc, const char *argv[])
 
    return DispatchCommandLine(CmdL, Cmds);
 }
+									/*}}}*/

+ 4 - 4
test/libapt/commandline_test.cc

@@ -96,7 +96,7 @@ TEST(CommandLineTest,GetCommand)
    char const * argv[] = { "apt-get", "-t", "unstable", "remove", "-d", "foo" };
    char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv);
    EXPECT_STREQ("remove", com);
-   std::vector<CommandLine::Args> Args = getCommandArgs("apt-get", com);
+   std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
    ::Configuration c;
    CommandLine CmdL(Args.data(), &c);
    ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv));
@@ -110,7 +110,7 @@ TEST(CommandLineTest,GetCommand)
    char const * argv[] = {"apt-get", "-t", "unstable", "remove", "--", "-d", "foo" };
    char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv);
    EXPECT_STREQ("remove", com);
-   std::vector<CommandLine::Args> Args = getCommandArgs("apt-get", com);
+   std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
    ::Configuration c;
    CommandLine CmdL(Args.data(), &c);
    ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv));
@@ -125,7 +125,7 @@ TEST(CommandLineTest,GetCommand)
    char const * argv[] = {"apt-get", "-t", "unstable", "--", "remove", "-d", "foo" };
    char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv);
    EXPECT_STREQ("remove", com);
-   std::vector<CommandLine::Args> Args = getCommandArgs("apt-get", com);
+   std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
    ::Configuration c;
    CommandLine CmdL(Args.data(), &c);
    ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv));
@@ -140,7 +140,7 @@ TEST(CommandLineTest,GetCommand)
    char const * argv[] = {"apt-get", "install", "-t", "unstable", "--", "remove", "-d", "foo" };
    char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv);
    EXPECT_STREQ("install", com);
-   std::vector<CommandLine::Args> Args = getCommandArgs("apt-get", com);
+   std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
    ::Configuration c;
    CommandLine CmdL(Args.data(), &c);
    ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv));

+ 6 - 0
test/libapt/gtest_runner.cc

@@ -1,5 +1,11 @@
 #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);
    int result = RUN_ALL_TESTS();