From: David Kalnischkies Date: Sun, 25 Oct 2015 22:45:09 +0000 (+0100) Subject: generate commands array after config is loaded X-Git-Tag: 1.1_exp15~22 X-Git-Url: https://git.saurik.com/apt.git/commitdiff_plain/011188e3920f21e6883c2dab956b3d4fb4e8cbfa 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 --- diff --git a/apt-private/private-cmndline.cc b/apt-private/private-cmndline.cc index a0acbbd79..c99a0afe0 100644 --- a/apt-private/private-cmndline.cc +++ b/apt-private/private-cmndline.cc @@ -149,6 +149,11 @@ static bool addArgumentsAPTFTPArchive(std::vector &Args, char } /*}}}*/ static bool addArgumentsAPTInternalSolver(std::vector &, char const * const)/*{{{*/ +{ + return true; +} + /*}}}*/ +static bool addArgumentsAPTHelper(std::vector &, char const * const)/*{{{*/ { return true; } @@ -315,34 +320,32 @@ static bool addArgumentsAPT(std::vector &Args, char const * c return true; } /*}}}*/ -std::vector getCommandArgs(char const * const Program, char const * const Cmd)/*{{{*/ +std::vector getCommandArgs(APT_CMD const Program, char const * const Cmd)/*{{{*/ { std::vector 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 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 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 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 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 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(); diff --git a/apt-private/private-cmndline.h b/apt-private/private-cmndline.h index 5674088bc..aee679bf8 100644 --- a/apt-private/private-cmndline.h +++ b/apt-private/private-cmndline.h @@ -9,10 +9,27 @@ class Configuration; class pkgSystem; -APT_PUBLIC std::vector 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 GetCommands(); + +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 std::vector getCommandArgs(APT_CMD const Program, char const * const Cmd); #endif diff --git a/cmdline/apt-cache.cc b/cmdline/apt-cache.cc index 67e4a8523..2933db218 100644 --- a/cmdline/apt-cache.cc +++ b/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 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(); diff --git a/cmdline/apt-cdrom.cc b/cmdline/apt-cdrom.cc index 3fc3faf4a..397ff2b65 100644 --- a/cmdline/apt-cdrom.cc +++ b/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 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(); diff --git a/cmdline/apt-config.cc b/cmdline/apt-config.cc index 3ccfa9a95..9d80e4ebf 100644 --- a/cmdline/apt-config.cc +++ b/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 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 const langs = APT::Configuration::getLanguages(true); _config->Clear("Acquire::Languages"); diff --git a/cmdline/apt-extracttemplates.cc b/cmdline/apt-extracttemplates.cc index e28b42870..c5c37d122 100644 --- a/cmdline/apt-extracttemplates.cc +++ b/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 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, {}); } /*}}}*/ diff --git a/cmdline/apt-get.cc b/cmdline/apt-get.cc index 2a19360c8..fd7f045c6 100644 --- a/cmdline/apt-get.cc +++ b/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 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(); diff --git a/cmdline/apt-helper.cc b/cmdline/apt-helper.cc index 186fded17..aef10828d 100644 --- a/cmdline/apt-helper.cc +++ b/cmdline/apt-helper.cc @@ -32,7 +32,7 @@ #include /*}}}*/ -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 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(); diff --git a/cmdline/apt-internal-solver.cc b/cmdline/apt-internal-solver.cc index 278f6d471..f6eaa11f7 100644 --- a/cmdline/apt-internal-solver.cc +++ b/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 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, {}); } /*}}}*/ diff --git a/cmdline/apt-mark.cc b/cmdline/apt-mark.cc index 0a5bb164a..c49476c7c 100644 --- a/cmdline/apt-mark.cc +++ b/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 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(); diff --git a/cmdline/apt-sortpkgs.cc b/cmdline/apt-sortpkgs.cc index 93aa5a76b..82c9c333d 100644 --- a/cmdline/apt-sortpkgs.cc +++ b/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 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, {}); } /*}}}*/ diff --git a/cmdline/apt.cc b/cmdline/apt.cc index eb16b561c..e32a9f1e3 100644 --- a/cmdline/apt.cc +++ b/cmdline/apt.cc @@ -37,7 +37,7 @@ #include /*}}}*/ -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 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) diff --git a/ftparchive/apt-ftparchive.cc b/ftparchive/apt-ftparchive.cc index ca03080ca..f53958ceb 100644 --- a/ftparchive/apt-ftparchive.cc +++ b/ftparchive/apt-ftparchive.cc @@ -605,9 +605,7 @@ static void LoadBinDir(vector &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 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); } + /*}}}*/ diff --git a/test/libapt/commandline_test.cc b/test/libapt/commandline_test.cc index 627f1b486..0da2ba45f 100644 --- a/test/libapt/commandline_test.cc +++ b/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 Args = getCommandArgs("apt-get", com); + std::vector 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 Args = getCommandArgs("apt-get", com); + std::vector 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 Args = getCommandArgs("apt-get", com); + std::vector 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 Args = getCommandArgs("apt-get", com); + std::vector Args = getCommandArgs(APT_CMD::APT_GET, com); ::Configuration c; CommandLine CmdL(Args.data(), &c); ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv)); diff --git a/test/libapt/gtest_runner.cc b/test/libapt/gtest_runner.cc index 5823c55de..29f631326 100644 --- a/test/libapt/gtest_runner.cc +++ b/test/libapt/gtest_runner.cc @@ -1,5 +1,11 @@ #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); int result = RUN_ALL_TESTS();