Просмотр исходного кода

deduplicate main methods

All mains pretty much do the same thing, so lets try a little harder to
move the common parts into -private to have the real differences more
visible.

Git-Dch: Ignore
David Kalnischkies лет назад: 10
Родитель
Сommit
e7e10e4747

+ 1 - 1
apt-pkg/contrib/cmndline.cc

@@ -411,7 +411,7 @@ unsigned int CommandLine::FileSize() const
 }
 									/*}}}*/
 // CommandLine::DispatchArg - Do something with the first arg		/*{{{*/
-bool CommandLine::DispatchArg(DispatchWithHelp *Map,bool NoMatch)
+bool CommandLine::DispatchArg(DispatchWithHelp const * const Map,bool NoMatch)
 {
    int I;
    for (I = 0; Map[I].Match != 0; I++)

+ 1 - 1
apt-pkg/contrib/cmndline.h

@@ -85,7 +85,7 @@ class CommandLine
    void ShowHelp();
    unsigned int FileSize() const APT_PURE;
    bool DispatchArg(Dispatch *List,bool NoMatch = true);
-   bool DispatchArg(DispatchWithHelp *List,bool NoMatch = true);
+   bool DispatchArg(DispatchWithHelp 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;

+ 67 - 2
apt-private/private-cmndline.cc

@@ -124,6 +124,32 @@ static bool addArgumentsAPTConfig(std::vector<CommandLine::Args> &Args, char con
    else
       return false;
 
+   return true;
+}
+									/*}}}*/
+static bool addArgumentsAPTExtractTemplates(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
+{
+   addArg('t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg);
+   return true;
+}
+									/*}}}*/
+static bool addArgumentsAPTFTPArchive(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
+{
+   addArg(0,"md5","APT::FTPArchive::MD5",0);
+   addArg(0,"sha1","APT::FTPArchive::SHA1",0);
+   addArg(0,"sha256","APT::FTPArchive::SHA256",0);
+   addArg(0,"sha512","APT::FTPArchive::SHA512",0);
+   addArg('d',"db","APT::FTPArchive::DB",CommandLine::HasArg);
+   addArg('s',"source-override","APT::FTPArchive::SourceOverride",CommandLine::HasArg);
+   addArg(0,"delink","APT::FTPArchive::DeLinkAct",0);
+   addArg(0,"readonly","APT::FTPArchive::ReadOnlyDB",0);
+   addArg(0,"contents","APT::FTPArchive::Contents",0);
+   addArg('a',"arch","APT::FTPArchive::Architecture",CommandLine::HasArg);
+   return true;
+}
+									/*}}}*/
+static bool addArgumentsAPTInternalSolver(std::vector<CommandLine::Args> &, char const * const)/*{{{*/
+{
    return true;
 }
 									/*}}}*/
@@ -254,6 +280,12 @@ static bool addArgumentsAPTMark(std::vector<CommandLine::Args> &Args, char const
       addArg('s',"no-act","APT::Mark::Simulate",0);
    }
 
+   return true;
+}
+									/*}}}*/
+static bool addArgumentsAPTSortPkgs(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
+{
+   addArg('s',"source","APT::SortPkgs::Source",0);
    return true;
 }
 									/*}}}*/
@@ -299,8 +331,16 @@ std::vector<CommandLine::Args> getCommandArgs(char const * const Program, char c
       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);
 
@@ -340,10 +380,19 @@ static void BinarySpecificConfiguration(char const * const Binary)	/*{{{*/
    _config->MoveSubTree(conf.c_str(), NULL);
 }
 									/*}}}*/
-void ParseCommandLine(CommandLine &CmdL, CommandLine::DispatchWithHelp const * Cmds, CommandLine::Args * const Args,/*{{{*/
+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 *))
 {
-   CmdL = CommandLine(Args,_config);
+   // 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();
@@ -377,3 +426,19 @@ void ParseCommandLine(CommandLine &CmdL, CommandLine::DispatchWithHelp const * C
    }
 }
 									/*}}}*/
+unsigned short DispatchCommandLine(CommandLine &CmdL, CommandLine::DispatchWithHelp const * const Cmds)	/*{{{*/
+{
+   // Match the operation
+   bool const returned = (Cmds != nullptr) ? CmdL.DispatchArg(Cmds) : true;
+
+   // Print any errors or warnings found during parsing
+   bool const Errors = _error->PendingError();
+   if (_config->FindI("quiet",0) > 0)
+      _error->DumpErrors();
+   else
+      _error->DumpErrors(GlobalError::DEBUG);
+   if (returned == false)
+      return 100;
+   return Errors == true ? 100 : 0;
+}
+									/*}}}*/

+ 2 - 1
apt-private/private-cmndline.h

@@ -10,8 +10,9 @@ 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, CommandLine::Args * const Args,
+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);
 
 #endif

+ 27 - 5
apt-private/private-main.cc

@@ -2,6 +2,7 @@
 
 #include <apt-pkg/cmndline.h>
 #include <apt-pkg/configuration.h>
+#include <apt-pkg/fileutl.h>
 
 #include <apt-private/private-main.h>
 
@@ -13,14 +14,18 @@
 #include <apti18n.h>
 
 
-void InitSignals()
+void InitLocale()							/*{{{*/
+{
+   setlocale(LC_ALL,"");
+   textdomain(PACKAGE);
+}
+									/*}}}*/
+void InitSignals()							/*{{{*/
 {
-   // Setup the signals
    signal(SIGPIPE,SIG_IGN);
 }
-
-
-void CheckSimulateMode(CommandLine &CmdL)
+									/*}}}*/
+void CheckIfSimulateMode(CommandLine &CmdL)				/*{{{*/
 {
    // disable locking in simulation, but show the message only for users
    // as root hasn't the same problems like unreadable files which can heavily
@@ -39,3 +44,20 @@ void CheckSimulateMode(CommandLine &CmdL)
       _config->Set("Debug::NoLocking",true);
    }
 }
+									/*}}}*/
+void CheckIfCalledByScript(int argc, const char *argv[])		/*{{{*/
+{
+   if (unlikely(argc < 1)) return;
+
+   if(!isatty(STDOUT_FILENO) &&
+      _config->FindB("Apt::Cmd::Disable-Script-Warning", false) == false)
+   {
+      std::cerr << std::endl
+                << "WARNING: " << flNotDir(argv[0]) << " "
+                << "does not have a stable CLI interface. "
+                << "Use with caution in scripts."
+                << std::endl
+                << std::endl;
+   }
+}
+									/*}}}*/

+ 3 - 1
apt-private/private-main.h

@@ -5,7 +5,9 @@
 
 class CommandLine;
 
-APT_PUBLIC void CheckSimulateMode(CommandLine &CmdL);
+APT_PUBLIC void InitLocale();
 APT_PUBLIC void InitSignals();
+APT_PUBLIC void CheckIfSimulateMode(CommandLine &CmdL);
+APT_PUBLIC void CheckIfCalledByScript(int argc, const char *argv[]);
 
 #endif

+ 5 - 17
cmdline/apt-cache.cc

@@ -45,6 +45,7 @@
 #include <apt-private/private-cmndline.h>
 #include <apt-private/private-show.h>
 #include <apt-private/private-search.h>
+#include <apt-private/private-main.h>
 
 #include <regex.h>
 #include <stddef.h>
@@ -1559,6 +1560,8 @@ static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
 									/*}}}*/
 int main(int argc,const char *argv[])					/*{{{*/
 {
+   InitLocale();
+
    CommandLine::DispatchWithHelp Cmds[] =  {
       {"gencaches",&GenCaches, nullptr},
       {"showsrc",&ShowSrcPackage, _("Show source records")},
@@ -1580,30 +1583,15 @@ int main(int argc,const char *argv[])					/*{{{*/
       {nullptr, nullptr, nullptr}
    };
 
-   std::vector<CommandLine::Args> Args = getCommandArgs("apt-cache", CommandLine::GetCommand(Cmds, argc, argv));
-
-   // Set up gettext support
-   setlocale(LC_ALL,"");
-   textdomain(PACKAGE);
-
    // Parse the command line and initialize the package library
    CommandLine CmdL;
-   ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
+   ParseCommandLine(CmdL, Cmds, "apt-cache", &_config, &_system, argc, argv, ShowHelp);
 
    InitOutput();
 
    if (_config->Exists("APT::Cache::Generate") == true)
       _config->Set("pkgCacheFile::Generate", _config->FindB("APT::Cache::Generate", true));
 
-   // Match the operation
-   CmdL.DispatchArg(Cmds);
-
-   // Print any errors or warnings found during parsing
-   bool const Errors = _error->PendingError();
-   if (_config->FindI("quiet",0) > 0)
-      _error->DumpErrors();
-   else
-      _error->DumpErrors(GlobalError::DEBUG);
-   return Errors == true ? 100 : 0;
+   return DispatchCommandLine(CmdL, Cmds);
 }
 									/*}}}*/

+ 5 - 15
cmdline/apt-cdrom.cc

@@ -32,6 +32,7 @@
 
 #include <apt-private/private-cmndline.h>
 #include <apt-private/private-output.h>
+#include <apt-private/private-main.h>
 
 #include <apti18n.h>
 									/*}}}*/
@@ -242,31 +243,20 @@ static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
 									/*}}}*/
 int main(int argc,const char *argv[])					/*{{{*/
 {
+   InitLocale();
+
    CommandLine::DispatchWithHelp Cmds[] = {
       {"add", &DoAdd, "Add a CDROM"},
       {"ident", &DoIdent, "Report the identity of a CDROM"},
       {nullptr, nullptr, nullptr}
    };
 
-   std::vector<CommandLine::Args> Args = getCommandArgs("apt-cdrom", CommandLine::GetCommand(Cmds, argc, argv));
-
-   // Set up gettext support
-   setlocale(LC_ALL,"");
-   textdomain(PACKAGE);
-
    // Parse the command line and initialize the package library
    CommandLine CmdL;
-   ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
+   ParseCommandLine(CmdL, Cmds, "apt-cdrom", &_config, &_system, argc, argv, ShowHelp);
 
    InitOutput();
 
-   // Match the operation
-   bool returned = CmdL.DispatchArg(Cmds);
-
-   if (_config->FindI("quiet",0) > 0)
-      _error->DumpErrors();
-   else
-      _error->DumpErrors(GlobalError::DEBUG);
-   return returned == true ? 0 : 100;
+   return DispatchCommandLine(CmdL, Cmds);
 }
 									/*}}}*/

+ 5 - 19
cmdline/apt-config.cc

@@ -32,6 +32,7 @@
 #include <string.h>
 
 #include <apt-private/private-cmndline.h>
+#include <apt-private/private-main.h>
 
 #include <apti18n.h>
 									/*}}}*/
@@ -107,21 +108,17 @@ static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
 									/*}}}*/
 int main(int argc,const char *argv[])					/*{{{*/
 {
+   InitLocale();
+
    CommandLine::DispatchWithHelp Cmds[] = {
       {"shell", &DoShell, _("get configuration values via shell evaluation")},
       {"dump", &DoDump, _("show the active configuration setting")},
       {nullptr, nullptr, nullptr}
    };
 
-   std::vector<CommandLine::Args> Args = getCommandArgs("apt-config", CommandLine::GetCommand(Cmds, argc, argv));
-
-   // Set up gettext support
-   setlocale(LC_ALL,"");
-   textdomain(PACKAGE);
-
    // Parse the command line and initialize the package library
    CommandLine CmdL;
-   ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
+   ParseCommandLine(CmdL, Cmds, "apt-config", &_config, &_system, argc, argv, ShowHelp);
 
    std::vector<std::string> const langs = APT::Configuration::getLanguages(true);
    _config->Clear("Acquire::Languages");
@@ -154,17 +151,6 @@ int main(int argc,const char *argv[])					/*{{{*/
    for (std::vector<std::string>::const_iterator p = profiles.begin(); p != profiles.end(); ++p)
       _config->Set("APT::Build-Profiles::", *p);
 
-   // Match the operation
-   CmdL.DispatchArg(Cmds);
-   
-   // Print any errors or warnings found during parsing
-   if (_error->empty() == false)
-   {
-      bool Errors = _error->PendingError();
-      _error->DumpErrors();
-      return Errors == true?100:0;
-   }
-   
-   return 0;
+   return DispatchCommandLine(CmdL, Cmds);
 }
 									/*}}}*/

+ 4 - 22
cmdline/apt-extracttemplates.cc

@@ -34,6 +34,7 @@
 #include <apt-pkg/mmap.h>
 
 #include <apt-private/private-cmndline.h>
+#include <apt-private/private-main.h>
 
 #include <iostream>
 #include <stdio.h>
@@ -344,34 +345,15 @@ static bool Go(CommandLine &CmdL)
 									/*}}}*/
 int main(int argc, const char **argv)					/*{{{*/
 {
-	CommandLine::Args Args[] = {
-		{'h',"help","help",0},
-		{'v',"version","version",0},
-		{'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg},
-		{'c',"config-file",0,CommandLine::ConfigFile},
-		{'o',"option",0,CommandLine::ArbItem},
-		{0,0,0,0}};
-
-	// Set up gettext support
-	setlocale(LC_ALL,"");
-	textdomain(PACKAGE);
+	InitLocale();
 
 	// Parse the command line and initialize the package library
 	CommandLine::DispatchWithHelp Cmds[] = {{nullptr, nullptr, nullptr}};
 	CommandLine CmdL;
-	ParseCommandLine(CmdL, Cmds, Args, &_config, &_system, argc, argv, ShowHelp);
+	ParseCommandLine(CmdL, Cmds, "apt-extracttemplates", &_config, &_system, argc, argv, ShowHelp);
 
 	Go(CmdL);
 
-	// Print any errors or warnings found during operation
-	if (_error->empty() == false)
-	{
-		// This goes to stderr..
-		bool Errors = _error->PendingError();
-		_error->DumpErrors();
-		return Errors == true?100:0;
-	}
-	
-	return 0;
+	return DispatchCommandLine(CmdL, nullptr);
 }
 									/*}}}*/

+ 5 - 22
cmdline/apt-get.cc

@@ -1622,6 +1622,8 @@ static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
 									/*}}}*/
 int main(int argc,const char *argv[])					/*{{{*/
 {
+   InitLocale();
+
    CommandLine::DispatchWithHelp Cmds[] = {
       {"update", &DoUpdate, _("Retrieve new lists of packages")},
       {"upgrade", &DoUpgrade, _("Perform an upgrade")},
@@ -1648,34 +1650,15 @@ int main(int argc,const char *argv[])					/*{{{*/
       {nullptr, nullptr, nullptr}
    };
 
-   std::vector<CommandLine::Args> Args = getCommandArgs("apt-get", CommandLine::GetCommand(Cmds, argc, argv));
-
-   // Set up gettext support
-   setlocale(LC_ALL,"");
-   textdomain(PACKAGE);
-
    // Parse the command line and initialize the package library
    CommandLine CmdL;
-   ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
+   ParseCommandLine(CmdL, Cmds, "apt-get", &_config, &_system, argc, argv, ShowHelp);
 
-   // see if we are in simulate mode
-   CheckSimulateMode(CmdL);
-
-   // Init the signals
    InitSignals();
-
-   // Setup the output streams
    InitOutput();
 
-   // Match the operation
-   CmdL.DispatchArg(Cmds);
+   CheckIfSimulateMode(CmdL);
 
-   // Print any errors or warnings found during parsing
-   bool const Errors = _error->PendingError();
-   if (_config->FindI("quiet",0) > 0)
-      _error->DumpErrors();
-   else
-      _error->DumpErrors(GlobalError::DEBUG);
-   return Errors == true ? 100 : 0;
+   return DispatchCommandLine(CmdL, Cmds);
 }
 									/*}}}*/

+ 5 - 18
cmdline/apt-helper.cc

@@ -22,6 +22,7 @@
 #include <apt-private/private-output.h>
 #include <apt-private/private-download.h>
 #include <apt-private/private-cmndline.h>
+#include <apt-private/private-main.h>
 #include <apt-pkg/srvrec.h>
 
 #include <iostream>
@@ -136,6 +137,8 @@ static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const  * Cmds)
 
 int main(int argc,const char *argv[])					/*{{{*/
 {
+   InitLocale();
+
    CommandLine::DispatchWithHelp Cmds[] = {
       {"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)")},
@@ -143,28 +146,12 @@ int main(int argc,const char *argv[])					/*{{{*/
       {nullptr, nullptr, nullptr}
    };
 
-   std::vector<CommandLine::Args> Args = getCommandArgs(
-      "apt-helper", CommandLine::GetCommand(Cmds, argc, argv));
-
-   // Set up gettext support
-   setlocale(LC_ALL,"");
-   textdomain(PACKAGE);
-
    // Parse the command line and initialize the package library
    CommandLine CmdL;
-   ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
+   ParseCommandLine(CmdL, Cmds, "apt-helper", &_config, &_system, argc, argv, ShowHelp);
 
    InitOutput();
 
-   // Match the operation
-   CmdL.DispatchArg(Cmds);
-
-   // Print any errors or warnings found during parsing
-   bool const Errors = _error->PendingError();
-   if (_config->FindI("quiet",0) > 0)
-      _error->DumpErrors();
-   else
-      _error->DumpErrors(GlobalError::DEBUG);
-   return Errors == true ? 100 : 0;
+   return DispatchCommandLine(CmdL, Cmds);
 }
 									/*}}}*/

+ 7 - 18
cmdline/apt-internal-solver.cc

@@ -27,6 +27,7 @@
 
 #include <apt-private/private-output.h>
 #include <apt-private/private-cmndline.h>
+#include <apt-private/private-main.h>
 
 #include <string.h>
 #include <iostream>
@@ -68,20 +69,13 @@ APT_NORETURN static void DIE(std::string const &message) {		/*{{{*/
 									/*}}}*/
 int main(int argc,const char *argv[])					/*{{{*/
 {
-	CommandLine::Args Args[] = {
-		{'h',"help","help",0},
-		{'v',"version","version",0},
-		{'q',"quiet","quiet",CommandLine::IntLevel},
-		{'q',"silent","quiet",CommandLine::IntLevel},
-		{'c',"config-file",0,CommandLine::ConfigFile},
-		{'o',"option",0,CommandLine::ArbItem},
-		{0,0,0,0}};
-
-        // we really don't need anything
-        DropPrivileges();
+	InitLocale();
+
+	// we really don't need anything
+	DropPrivileges();
 
 	CommandLine CmdL;
-	ParseCommandLine(CmdL, nullptr, Args, &_config, NULL, argc, argv, ShowHelp);
+	ParseCommandLine(CmdL, nullptr, "apt-internal-solver", &_config, NULL, argc, argv, ShowHelp);
 
 	if (CmdL.FileList[0] != 0 && strcmp(CmdL.FileList[0], "scenario") == 0)
 	{
@@ -188,11 +182,6 @@ int main(int argc,const char *argv[])					/*{{{*/
 
 	EDSP::WriteProgress(100, "Done", output);
 
-	bool const Errors = _error->PendingError();
-	if (_config->FindI("quiet",0) > 0)
-		_error->DumpErrors(std::cerr);
-	else
-		_error->DumpErrors(std::cerr, GlobalError::DEBUG);
-	return Errors == true ? 100 : 0;
+	return DispatchCommandLine(CmdL, nullptr);
 }
 									/*}}}*/

+ 5 - 17
cmdline/apt-mark.cc

@@ -24,6 +24,7 @@
 
 #include <apt-private/private-cmndline.h>
 #include <apt-private/private-output.h>
+#include <apt-private/private-main.h>
 
 #include <errno.h>
 #include <fcntl.h>
@@ -315,6 +316,8 @@ static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
 									/*}}}*/
 int main(int argc,const char *argv[])					/*{{{*/
 {
+   InitLocale();
+
    CommandLine::DispatchWithHelp Cmds[] = {
       {"auto",&DoAuto, _("Mark the given packages as automatically installed")},
       {"manual",&DoAuto, _("Mark the given packages as manually installed")},
@@ -337,26 +340,11 @@ int main(int argc,const char *argv[])					/*{{{*/
       {nullptr, nullptr, nullptr}
    };
 
-   std::vector<CommandLine::Args> Args = getCommandArgs("apt-mark", CommandLine::GetCommand(Cmds, argc, argv));
-
-   // Set up gettext support
-   setlocale(LC_ALL,"");
-   textdomain(PACKAGE);
-
    CommandLine CmdL;
-   ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
+   ParseCommandLine(CmdL, Cmds, "apt-mark", &_config, &_system, argc, argv, ShowHelp);
 
    InitOutput();
 
-   // Match the operation
-   CmdL.DispatchArg(Cmds);
-
-   // Print any errors or warnings found during parsing
-   bool const Errors = _error->PendingError();
-   if (_config->FindI("quiet",0) > 0)
-      _error->DumpErrors();
-   else
-      _error->DumpErrors(GlobalError::DEBUG);
-   return Errors == true ? 100 : 0;
+   return DispatchCommandLine(CmdL, Cmds);
 }
 									/*}}}*/

+ 5 - 22
cmdline/apt-sortpkgs.cc

@@ -24,6 +24,7 @@
 #include <apt-pkg/pkgsystem.h>
 
 #include <apt-private/private-cmndline.h>
+#include <apt-private/private-main.h>
 
 #include <vector>
 #include <algorithm>
@@ -157,36 +158,18 @@ static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const *)
 									/*}}}*/
 int main(int argc,const char *argv[])					/*{{{*/
 {
-   CommandLine::Args Args[] = {
-      {'h',"help","help",0},
-      {'v',"version","version",0},
-      {'s',"source","APT::SortPkgs::Source",0},
-      {'c',"config-file",0,CommandLine::ConfigFile},
-      {'o',"option",0,CommandLine::ArbItem},
-      {0,0,0,0}};
-
-   // Set up gettext support
-   setlocale(LC_ALL,"");
-   textdomain(PACKAGE);
+   InitLocale();
 
    // Parse the command line and initialize the package library
    CommandLine::DispatchWithHelp Cmds[] = {{nullptr, nullptr, nullptr}};
    CommandLine CmdL;
-   ParseCommandLine(CmdL, Cmds, Args, &_config, &_system, argc, argv, ShowHelp);
+   ParseCommandLine(CmdL, Cmds, "apt-sortpkgs", &_config, &_system, argc, argv, ShowHelp);
 
    // Match the operation
    for (unsigned int I = 0; I != CmdL.FileSize(); I++)
       if (DoIt(CmdL.FileList[I]) == false)
 	 break;
-   
-   // Print any errors or warnings found during parsing
-   if (_error->empty() == false)
-   {
-      bool Errors = _error->PendingError();
-      _error->DumpErrors();
-      return Errors == true?100:0;
-   }
-   
-   return 0;   
+
+   return DispatchCommandLine(CmdL, nullptr);
 }
 									/*}}}*/

+ 7 - 40
cmdline/apt.cc

@@ -60,6 +60,8 @@ static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
 
 int main(int argc, const char *argv[])					/*{{{*/
 {
+   InitLocale();
+
    CommandLine::DispatchWithHelp Cmds[] = {
       // query
       {"list", &DoList, _("list packages based on package names")},
@@ -85,51 +87,16 @@ int main(int argc, const char *argv[])					/*{{{*/
       {nullptr, nullptr, nullptr}
    };
 
-   std::vector<CommandLine::Args> Args = getCommandArgs("apt", CommandLine::GetCommand(Cmds, argc, argv));
-
-   // Init the signals
+   // FIXME: Those ignore commandline configuration like -q
    InitSignals();
-
-   // Init the output
    InitOutput();
 
-   // Set up gettext support
-   setlocale(LC_ALL,"");
-   textdomain(PACKAGE);
-
-    if(pkgInitConfig(*_config) == false) 
-    {
-        _error->DumpErrors();
-        return 100;
-    }
-
-   // Parse the command line and initialize the package library
    CommandLine CmdL;
-   ParseCommandLine(CmdL, Cmds, Args.data(), NULL, &_system, argc, argv, ShowHelp);
-
-   if(!isatty(STDOUT_FILENO) &&
-      _config->FindB("Apt::Cmd::Disable-Script-Warning", false) == false)
-   {
-      std::cerr << std::endl
-                << "WARNING: " << argv[0] << " "
-                << "does not have a stable CLI interface yet. "
-                << "Use with caution in scripts."
-                << std::endl
-                << std::endl;
-   }
-
-   // see if we are in simulate mode
-   CheckSimulateMode(CmdL);
+   ParseCommandLine(CmdL, Cmds, "apt", &_config, &_system, argc, argv, ShowHelp);
 
-   // parse args
-   CmdL.DispatchArg(Cmds);
+   CheckIfCalledByScript(argc, argv);
+   CheckIfSimulateMode(CmdL);
 
-   // Print any errors or warnings found during parsing
-   bool const Errors = _error->PendingError();
-   if (_config->FindI("quiet",0) > 0)
-      _error->DumpErrors();
-   else
-      _error->DumpErrors(GlobalError::DEBUG);
-   return Errors == true ? 100 : 0;
+   return DispatchCommandLine(CmdL, Cmds);
 }
 									/*}}}*/

+ 5 - 31
ftparchive/apt-ftparchive.cc

@@ -21,6 +21,7 @@
 
 #include <apt-private/private-cmndline.h>
 #include <apt-private/private-output.h>
+#include <apt-private/private-main.h>
 
 #include <algorithm>
 #include <climits>
@@ -1026,25 +1027,7 @@ static bool Clean(CommandLine &CmdL)
 
 int main(int argc, const char *argv[])
 {
-   setlocale(LC_ALL, "");
-   CommandLine::Args Args[] = {
-      {'h',"help","help",0},
-      {0,"md5","APT::FTPArchive::MD5",0},
-      {0,"sha1","APT::FTPArchive::SHA1",0},
-      {0,"sha256","APT::FTPArchive::SHA256",0},
-      {0,"sha512","APT::FTPArchive::SHA512",0},
-      {'v',"version","version",0},
-      {'d',"db","APT::FTPArchive::DB",CommandLine::HasArg},
-      {'s',"source-override","APT::FTPArchive::SourceOverride",CommandLine::HasArg},
-      {'q',"quiet","quiet",CommandLine::IntLevel},
-      {'q',"silent","quiet",CommandLine::IntLevel},
-      {0,"delink","APT::FTPArchive::DeLinkAct",0},
-      {0,"readonly","APT::FTPArchive::ReadOnlyDB",0},
-      {0,"contents","APT::FTPArchive::Contents",0},
-      {'a',"arch","APT::FTPArchive::Architecture",CommandLine::HasArg},
-      {'c',"config-file",0,CommandLine::ConfigFile},
-      {'o',"option",0,CommandLine::ArbItem},
-      {0,0,0,0}};
+   InitLocale();
 
    CommandLine::DispatchWithHelp Cmds[] = {
       {"packages",&SimpleGenPackages, nullptr},
@@ -1057,21 +1040,12 @@ int main(int argc, const char *argv[])
    };
 
    // Parse the command line and initialize the package library
-   CommandLine CmdL(Args,_config);
-   ParseCommandLine(CmdL, Cmds, Args, &_config, NULL, argc, argv, ShowHelp);
+   CommandLine CmdL;
+   ParseCommandLine(CmdL, Cmds, "apt-ftparchive", &_config, NULL, argc, argv, ShowHelp);
 
    _config->CndSet("quiet",0);
    Quiet = _config->FindI("quiet",0);
    InitOutput(clog.rdbuf());
 
-   // Match the operation
-   CmdL.DispatchArg(Cmds);
-   
-   if (_error->empty() == false)
-   {
-      bool Errors = _error->PendingError();
-      _error->DumpErrors();
-      return Errors == true?100:0;
-   }
-   return 0;
+   return DispatchCommandLine(CmdL, Cmds);
 }