]> git.saurik.com Git - apt.git/blame - apt-private/private-cmndline.cc
FildFd: Introduce a Flush() function and call it from Close()
[apt.git] / apt-private / private-cmndline.cc
CommitLineData
b9179170
MV
1// Include Files /*{{{*/
2#include <config.h>
3
4#include <apt-pkg/cmndline.h>
ad7e0941 5#include <apt-pkg/configuration.h>
c3ded84c 6#include <apt-pkg/fileutl.h>
ad7e0941
DK
7#include <apt-pkg/pkgsystem.h>
8#include <apt-pkg/init.h>
9#include <apt-pkg/error.h>
8561c2fe 10#include <apt-pkg/strutl.h>
b9179170 11
453b82a3 12#include <apt-private/private-cmndline.h>
b9179170
MV
13
14#include <stdarg.h>
15#include <string.h>
ad7e0941 16#include <stdlib.h>
b9179170 17
8561c2fe
DK
18#include <vector>
19#include <iomanip>
20
b9179170
MV
21#include <apti18n.h>
22 /*}}}*/
23
a02db58f 24APT_SENTINEL static bool strcmp_match_in_list(char const * const Cmd, ...) /*{{{*/
b9179170
MV
25{
26 va_list args;
27 bool found = false;
28 va_start(args, Cmd);
29 char const * Match = NULL;
30 while ((Match = va_arg(args, char const *)) != NULL)
31 {
32 if (strcmp(Cmd, Match) != 0)
33 continue;
34 found = true;
35 break;
36 }
37 va_end(args);
38 return found;
39}
40 /*}}}*/
41#define addArg(w,x,y,z) Args.push_back(CommandLine::MakeArgs(w,x,y,z))
42#define CmdMatches(...) strcmp_match_in_list(Cmd, __VA_ARGS__, NULL)
c3ccac92 43static bool addArgumentsAPTCache(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
b9179170
MV
44{
45 if (CmdMatches("depends", "rdepends", "xvcg", "dotty"))
46 {
47 addArg('i', "important", "APT::Cache::Important", 0);
48 addArg(0, "installed", "APT::Cache::Installed", 0);
49 addArg(0, "pre-depends", "APT::Cache::ShowPre-Depends", 0);
50 addArg(0, "depends", "APT::Cache::ShowDepends", 0);
51 addArg(0, "recommends", "APT::Cache::ShowRecommends", 0);
52 addArg(0, "suggests", "APT::Cache::ShowSuggests", 0);
53 addArg(0, "replaces", "APT::Cache::ShowReplaces", 0);
54 addArg(0, "breaks", "APT::Cache::ShowBreaks", 0);
55 addArg(0, "conflicts", "APT::Cache::ShowConflicts", 0);
56 addArg(0, "enhances", "APT::Cache::ShowEnhances", 0);
57 addArg(0, "recurse", "APT::Cache::RecurseDepends", 0);
8c7af4d4 58 addArg(0, "implicit", "APT::Cache::ShowImplicit", 0);
b9179170
MV
59 }
60 else if (CmdMatches("search"))
61 {
62 addArg('n', "names-only", "APT::Cache::NamesOnly", 0);
63 addArg('f', "full", "APT::Cache::ShowFull", 0);
64 }
65 else if (CmdMatches("show"))
66 {
67 addArg('a', "all-versions", "APT::Cache::AllVersions", 0);
68 }
69 else if (CmdMatches("pkgnames"))
70 {
71 addArg(0, "all-names", "APT::Cache::AllNames", 0);
72 }
58c2833f
MV
73 else if (CmdMatches("unmet"))
74 {
75 addArg('i', "important", "APT::Cache::Important", 0);
76 }
a8275acf
MV
77 else if (CmdMatches("showsrc"))
78 {
79 addArg(0,"only-source","APT::Cache::Only-Source",0);
80 }
81 else if (CmdMatches("gencaches", "showpkg", "stats", "dump",
58c2833f 82 "dumpavail", "showauto", "policy", "madison"))
b9179170
MV
83 ;
84 else
85 return false;
86
859093da
DK
87 bool const found_something = Args.empty() == false;
88
b9179170
MV
89 // FIXME: move to the correct command(s)
90 addArg('g', "generate", "APT::Cache::Generate", 0);
91 addArg('t', "target-release", "APT::Default-Release", CommandLine::HasArg);
92 addArg('t', "default-release", "APT::Default-Release", CommandLine::HasArg);
93
94 addArg('p', "pkg-cache", "Dir::Cache::pkgcache", CommandLine::HasArg);
95 addArg('s', "src-cache", "Dir::Cache::srcpkgcache", CommandLine::HasArg);
859093da
DK
96
97 return found_something;
b9179170
MV
98}
99 /*}}}*/
c3ccac92 100static bool addArgumentsAPTCDROM(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
b9179170
MV
101{
102 if (CmdMatches("add", "ident") == false)
103 return false;
104
105 // FIXME: move to the correct command(s)
106 addArg(0, "auto-detect", "Acquire::cdrom::AutoDetect", CommandLine::Boolean);
107 addArg('d', "cdrom", "Acquire::cdrom::mount", CommandLine::HasArg);
108 addArg('r', "rename", "APT::CDROM::Rename", 0);
109 addArg('m', "no-mount", "APT::CDROM::NoMount", 0);
110 addArg('f', "fast", "APT::CDROM::Fast", 0);
111 addArg('n', "just-print", "APT::CDROM::NoAct", 0);
112 addArg('n', "recon", "APT::CDROM::NoAct", 0);
113 addArg('n', "no-act", "APT::CDROM::NoAct", 0);
114 addArg('a', "thorough", "APT::CDROM::Thorough", 0);
115 return true;
116}
117 /*}}}*/
c3ccac92 118static bool addArgumentsAPTConfig(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
b9179170
MV
119{
120 if (CmdMatches("dump"))
121 {
122 addArg(0,"empty","APT::Config::Dump::EmptyValue",CommandLine::Boolean);
123 addArg(0,"format","APT::Config::Dump::Format",CommandLine::HasArg);
124 }
125 else if (CmdMatches("shell"))
126 ;
127 else
128 return false;
129
e7e10e47
DK
130 return true;
131}
132 /*}}}*/
133static bool addArgumentsAPTExtractTemplates(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
134{
135 addArg('t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg);
136 return true;
137}
138 /*}}}*/
139static bool addArgumentsAPTFTPArchive(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
140{
141 addArg(0,"md5","APT::FTPArchive::MD5",0);
142 addArg(0,"sha1","APT::FTPArchive::SHA1",0);
143 addArg(0,"sha256","APT::FTPArchive::SHA256",0);
144 addArg(0,"sha512","APT::FTPArchive::SHA512",0);
145 addArg('d',"db","APT::FTPArchive::DB",CommandLine::HasArg);
146 addArg('s',"source-override","APT::FTPArchive::SourceOverride",CommandLine::HasArg);
147 addArg(0,"delink","APT::FTPArchive::DeLinkAct",0);
148 addArg(0,"readonly","APT::FTPArchive::ReadOnlyDB",0);
149 addArg(0,"contents","APT::FTPArchive::Contents",0);
150 addArg('a',"arch","APT::FTPArchive::Architecture",CommandLine::HasArg);
151 return true;
152}
153 /*}}}*/
154static bool addArgumentsAPTInternalSolver(std::vector<CommandLine::Args> &, char const * const)/*{{{*/
011188e3
DK
155{
156 return true;
157}
158 /*}}}*/
159static bool addArgumentsAPTHelper(std::vector<CommandLine::Args> &, char const * const)/*{{{*/
e7e10e47 160{
b9179170
MV
161 return true;
162}
163 /*}}}*/
c3ccac92 164static bool addArgumentsAPTGet(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
b9179170
MV
165{
166 if (CmdMatches("install", "remove", "purge", "upgrade", "dist-upgrade",
ebe24b7a 167 "dselect-upgrade", "autoremove", "full-upgrade"))
b9179170 168 {
4fb66d87 169 addArg(0, "show-progress", "DpkgPM::Progress", 0);
b9179170
MV
170 addArg('f', "fix-broken", "APT::Get::Fix-Broken", 0);
171 addArg(0, "purge", "APT::Get::Purge", 0);
172 addArg('V',"verbose-versions","APT::Get::Show-Versions",0);
173 addArg(0, "auto-remove", "APT::Get::AutomaticRemove", 0);
174 addArg(0, "reinstall", "APT::Get::ReInstall", 0);
175 addArg(0, "solver", "APT::Solver", CommandLine::HasArg);
176 if (CmdMatches("upgrade"))
177 {
2004d647 178 addArg(0, "new-pkgs", "APT::Get::Upgrade-Allow-New",
c678a044 179 CommandLine::Boolean);
b9179170
MV
180 }
181 }
182 else if (CmdMatches("update"))
183 {
184 addArg(0, "list-cleanup", "APT::Get::List-Cleanup", 0);
185 }
186 else if (CmdMatches("source"))
187 {
188 addArg('b', "compile", "APT::Get::Compile", 0);
189 addArg('b', "build", "APT::Get::Compile", 0);
ce7f128c 190 addArg('P', "build-profiles", "APT::Build-Profiles", CommandLine::HasArg);
b9179170
MV
191 addArg(0, "diff-only", "APT::Get::Diff-Only", 0);
192 addArg(0, "debian-only", "APT::Get::Diff-Only", 0);
193 addArg(0, "tar-only", "APT::Get::Tar-Only", 0);
194 addArg(0, "dsc-only", "APT::Get::Dsc-Only", 0);
195 }
196 else if (CmdMatches("build-dep"))
197 {
198 addArg('a', "host-architecture", "APT::Get::Host-Architecture", CommandLine::HasArg);
ce7f128c 199 addArg('P', "build-profiles", "APT::Build-Profiles", CommandLine::HasArg);
e4e5d47b 200 addArg(0, "purge", "APT::Get::Purge", 0);
b9179170 201 addArg(0, "solver", "APT::Solver", CommandLine::HasArg);
c872ac50
MV
202 // this has no effect *but* sbuild is using it (see LP: #1255806)
203 // once sbuild is fixed, this option can be removed
204 addArg('f', "fix-broken", "APT::Get::Fix-Broken", 0);
b9179170 205 }
c2a4a8dd 206 else if (CmdMatches("indextargets"))
8881b11e 207 {
c2a4a8dd
DK
208 addArg(0,"format","APT::Get::IndexTargets::Format", CommandLine::HasArg);
209 addArg(0,"release-info","APT::Get::IndexTargets::ReleaseInfo", 0);
8881b11e 210 }
f66738d7 211 else if (CmdMatches("clean", "autoclean", "auto-clean", "check", "download", "changelog") ||
b9179170
MV
212 CmdMatches("markauto", "unmarkauto")) // deprecated commands
213 ;
214 else if (CmdMatches("moo"))
215 addArg(0, "color", "APT::Moo::Color", 0);
216
217 if (CmdMatches("install", "remove", "purge", "upgrade", "dist-upgrade",
f66738d7 218 "dselect-upgrade", "autoremove", "auto-remove", "clean", "autoclean", "auto-clean", "check",
f83b5627 219 "build-dep", "full-upgrade", "source"))
b9179170
MV
220 {
221 addArg('s', "simulate", "APT::Get::Simulate", 0);
222 addArg('s', "just-print", "APT::Get::Simulate", 0);
223 addArg('s', "recon", "APT::Get::Simulate", 0);
224 addArg('s', "dry-run", "APT::Get::Simulate", 0);
225 addArg('s', "no-act", "APT::Get::Simulate", 0);
226 }
227
859093da
DK
228 bool const found_something = Args.empty() == false;
229
b9179170
MV
230 // FIXME: move to the correct command(s)
231 addArg('d',"download-only","APT::Get::Download-Only",0);
232 addArg('y',"yes","APT::Get::Assume-Yes",0);
233 addArg('y',"assume-yes","APT::Get::Assume-Yes",0);
234 addArg(0,"assume-no","APT::Get::Assume-No",0);
235 addArg('u',"show-upgraded","APT::Get::Show-Upgraded",0);
236 addArg('m',"ignore-missing","APT::Get::Fix-Missing",0);
237 addArg('t',"target-release","APT::Default-Release",CommandLine::HasArg);
238 addArg('t',"default-release","APT::Default-Release",CommandLine::HasArg);
239 addArg(0,"download","APT::Get::Download",0);
240 addArg(0,"fix-missing","APT::Get::Fix-Missing",0);
241 addArg(0,"ignore-hold","APT::Ignore-Hold",0);
242 addArg(0,"upgrade","APT::Get::upgrade",0);
243 addArg(0,"only-upgrade","APT::Get::Only-Upgrade",0);
b381a482
JAK
244 addArg(0,"allow-change-held-packages","APT::Get::allow-change-held-packages",CommandLine::Boolean);
245 addArg(0,"allow-remove-essential","APT::Get::allow-remove-essential",CommandLine::Boolean);
246 addArg(0,"allow-downgrades","APT::Get::allow-downgrades",CommandLine::Boolean);
b9179170
MV
247 addArg(0,"force-yes","APT::Get::force-yes",0);
248 addArg(0,"print-uris","APT::Get::Print-URIs",0);
249 addArg(0,"trivial-only","APT::Get::Trivial-Only",0);
250 addArg(0,"remove","APT::Get::Remove",0);
251 addArg(0,"only-source","APT::Get::Only-Source",0);
252 addArg(0,"arch-only","APT::Get::Arch-Only",0);
253 addArg(0,"allow-unauthenticated","APT::Get::AllowUnauthenticated",0);
c99fe2e1 254 addArg(0,"allow-insecure-repositories","Acquire::AllowInsecureRepositories",0);
b9179170
MV
255 addArg(0,"install-recommends","APT::Install-Recommends",CommandLine::Boolean);
256 addArg(0,"install-suggests","APT::Install-Suggests",CommandLine::Boolean);
257 addArg(0,"fix-policy","APT::Get::Fix-Policy-Broken",0);
258
859093da 259 return found_something;
b9179170
MV
260}
261 /*}}}*/
c3ccac92 262static bool addArgumentsAPTMark(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
b9179170
MV
263{
264 if (CmdMatches("auto", "manual", "hold", "unhold", "showauto",
64e3414e 265 "showmanual", "showhold", "showholds",
b9179170 266 "markauto", "unmarkauto"))
64e3414e
DK
267 {
268 addArg('f',"file","Dir::State::extended_states",CommandLine::HasArg);
269 }
270 else if (CmdMatches("install", "remove", "deinstall", "purge",
271 "showinstall", "showinstalls", "showremove", "showremoves",
272 "showdeinstall", "showdeinstalls", "showpurge", "showpurges"))
b9179170
MV
273 ;
274 else
275 return false;
276
64e3414e
DK
277 if (CmdMatches("markauto", "unmarkauto"))
278 {
279 addArg('v',"verbose","APT::MarkAuto::Verbose",0);
280 }
281
282 if (strncmp(Cmd, "show", strlen("show")) != 0)
283 {
284 addArg('s',"simulate","APT::Mark::Simulate",0);
285 addArg('s',"just-print","APT::Mark::Simulate",0);
286 addArg('s',"recon","APT::Mark::Simulate",0);
287 addArg('s',"dry-run","APT::Mark::Simulate",0);
288 addArg('s',"no-act","APT::Mark::Simulate",0);
289 }
b9179170 290
e7e10e47
DK
291 return true;
292}
293 /*}}}*/
294static bool addArgumentsAPTSortPkgs(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/
295{
296 addArg('s',"source","APT::SortPkgs::Source",0);
b9179170
MV
297 return true;
298}
299 /*}}}*/
c3ccac92 300static bool addArgumentsAPT(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/
b9179170
MV
301{
302 if (CmdMatches("list"))
303 {
d2c712a1 304 addArg('i',"installed","APT::Cmd::Installed",0);
df7c9fd2 305 addArg(0,"upgradeable","APT::Cmd::Upgradable",0);
d2c712a1 306 addArg('u',"upgradable","APT::Cmd::Upgradable",0);
3bdf7da5 307 addArg(0,"manual-installed","APT::Cmd::Manual-Installed",0);
e1dc051a 308 addArg('v', "verbose", "APT::Cmd::List-Include-Summary", 0);
c1a61d1c 309 addArg('a', "all-versions", "APT::Cmd::All-Versions", 0);
b9179170 310 }
06293aa7
MV
311 else if (CmdMatches("show"))
312 {
313 addArg('a', "all-versions", "APT::Cache::AllVersions", 0);
314 }
b9179170
MV
315 else if (addArgumentsAPTGet(Args, Cmd) || addArgumentsAPTCache(Args, Cmd))
316 {
317 // we have no (supported) command-name overlaps so far, so we call
318 // specifics in order until we find one which adds arguments
319 }
320 else
321 return false;
322
323 return true;
324}
325 /*}}}*/
011188e3 326std::vector<CommandLine::Args> getCommandArgs(APT_CMD const Program, char const * const Cmd)/*{{{*/
b9179170
MV
327{
328 std::vector<CommandLine::Args> Args;
329 Args.reserve(50);
011188e3
DK
330 if (Cmd == nullptr)
331 {
332 if (Program == APT_CMD::APT_EXTRACTTEMPLATES)
333 addArgumentsAPTExtractTemplates(Args, Cmd);
334 }
b9179170
MV
335 else if (strcmp(Cmd, "help") == 0)
336 ; // no options for help so no need to implement it in each
011188e3
DK
337 else
338 switch (Program)
339 {
340 case APT_CMD::APT: addArgumentsAPT(Args, Cmd); break;
341 case APT_CMD::APT_GET: addArgumentsAPTGet(Args, Cmd); break;
342 case APT_CMD::APT_CACHE: addArgumentsAPTCache(Args, Cmd); break;
343 case APT_CMD::APT_CDROM: addArgumentsAPTCDROM(Args, Cmd); break;
344 case APT_CMD::APT_CONFIG: addArgumentsAPTConfig(Args, Cmd); break;
345 case APT_CMD::APT_EXTRACTTEMPLATES: addArgumentsAPTExtractTemplates(Args, Cmd); break;
346 case APT_CMD::APT_FTPARCHIVE: addArgumentsAPTFTPArchive(Args, Cmd); break;
347 case APT_CMD::APT_HELPER: addArgumentsAPTHelper(Args, Cmd); break;
348 case APT_CMD::APT_INTERNAL_SOLVER: addArgumentsAPTInternalSolver(Args, Cmd); break;
349 case APT_CMD::APT_MARK: addArgumentsAPTMark(Args, Cmd); break;
350 case APT_CMD::APT_SORTPKG: addArgumentsAPTSortPkgs(Args, Cmd); break;
351 }
b9179170
MV
352
353 // options without a command
354 addArg('h', "help", "help", 0);
355 addArg('v', "version", "version", 0);
356 // general options
357 addArg('q', "quiet", "quiet", CommandLine::IntLevel);
358 addArg('q', "silent", "quiet", CommandLine::IntLevel);
359 addArg('c', "config-file", 0, CommandLine::ConfigFile);
360 addArg('o', "option", 0, CommandLine::ArbItem);
361 addArg(0, NULL, NULL, 0);
362
363 return Args;
364}
365 /*}}}*/
366#undef CmdMatches
367#undef addArg
8561c2fe 368static void ShowHelpListCommands(std::vector<aptDispatchWithHelp> const &Cmds)/*{{{*/
41d39345 369{
8561c2fe
DK
370 if (Cmds.empty() || Cmds[0].Match == nullptr)
371 return;
372 std::cout << std::endl << _("Most used commands:") << std::endl;
373 for (auto const &c: Cmds)
374 {
375 if (c.Help == nullptr)
376 continue;
377 std::cout << " " << c.Match << " - " << c.Help << std::endl;
378 }
41d39345
DK
379}
380 /*}}}*/
90986d4d
DK
381static bool ShowCommonHelp(APT_CMD const Binary, CommandLine &CmdL, std::vector<aptDispatchWithHelp> const &Cmds,/*{{{*/
382 bool (*ShowHelp)(CommandLine &))
41d39345 383{
8561c2fe
DK
384 std::cout << PACKAGE << " " << PACKAGE_VERSION << " (" << COMMON_ARCH << ")" << std::endl;
385 if (_config->FindB("version") == true && Binary != APT_CMD::APT_GET)
386 return true;
387 if (ShowHelp(CmdL) == false)
388 return false;
389 if (_config->FindB("version") == true || Binary == APT_CMD::APT_FTPARCHIVE)
390 return true;
391 ShowHelpListCommands(Cmds);
392 std::cout << std::endl;
393 char const * cmd = nullptr;
394 switch (Binary)
41d39345 395 {
8561c2fe
DK
396 case APT_CMD::APT: cmd = "apt(8)"; break;
397 case APT_CMD::APT_CACHE: cmd = "apt-cache(8)"; break;
398 case APT_CMD::APT_CDROM: cmd = "apt-cdrom(8)"; break;
399 case APT_CMD::APT_CONFIG: cmd = "apt-config(8)"; break;
400 case APT_CMD::APT_EXTRACTTEMPLATES: cmd = "apt-extracttemplates(1)"; break;
401 case APT_CMD::APT_FTPARCHIVE: cmd = "apt-ftparchive(1)"; break;
402 case APT_CMD::APT_GET: cmd = "apt-get(8)"; break;
403 case APT_CMD::APT_HELPER: cmd = nullptr; break;
404 case APT_CMD::APT_INTERNAL_SOLVER: cmd = nullptr; break;
405 case APT_CMD::APT_MARK: cmd = "apt-mark(8)"; break;
406 case APT_CMD::APT_SORTPKG: cmd = "apt-sortpkgs(1)"; break;
41d39345 407 }
8561c2fe
DK
408 if (cmd != nullptr)
409 ioprintf(std::cout, _("See %s for more information about the available commands."), cmd);
410 std::cout << std::endl <<
411 _("Configuration options and syntax is detailed in apt.conf(5).\n"
412 "Information about how to configure sources can be found in sources.list(5).\n"
413 "Package and version choices can be expressed via apt_preferences(5).\n"
414 "Security details are available in apt-secure(8).\n");
415 if (Binary == APT_CMD::APT_GET || Binary == APT_CMD::APT)
416 std::cout << std::right << std::setw(70) << _("This APT has Super Cow Powers.") << std::endl;
417 else if (Binary == APT_CMD::APT_HELPER)
418 std::cout << std::right << std::setw(70) << _("This APT helper has Super Meep Powers.") << std::endl;
419 return true;
41d39345
DK
420}
421 /*}}}*/
501cd23e
DK
422static void BinarySpecificConfiguration(char const * const Binary) /*{{{*/
423{
424 std::string const binary = flNotDir(Binary);
425 if (binary == "apt" || binary == "apt-config")
426 {
631800b1 427 _config->CndSet("Binary::apt::APT::Color", true);
501cd23e
DK
428 _config->CndSet("Binary::apt::APT::Cache::Show::Version", 2);
429 _config->CndSet("Binary::apt::APT::Cache::AllVersions", false);
430 _config->CndSet("Binary::apt::APT::Cache::ShowVirtuals", true);
631800b1 431 _config->CndSet("Binary::apt::APT::Cache::Search::Version", 2);
c094c868
DK
432 _config->CndSet("Binary::apt::APT::Cache::ShowDependencyType", true);
433 _config->CndSet("Binary::apt::APT::Cache::ShowVersion", true);
631800b1
DK
434 _config->CndSet("Binary::apt::APT::Get::Upgrade-Allow-New", true);
435 _config->CndSet("Binary::apt::APT::Cmd::Show-Update-Stats", true);
436 _config->CndSet("Binary::apt::DPkg::Progress-Fancy", true);
995a4bf6 437 _config->CndSet("Binary::apt::Acquire::AllowInsecureRepositories", false);
501cd23e
DK
438 }
439
440 _config->Set("Binary", binary);
441 std::string const conf = "Binary::" + binary;
442 _config->MoveSubTree(conf.c_str(), NULL);
443}
444 /*}}}*/
6079b276 445std::vector<CommandLine::Dispatch> ParseCommandLine(CommandLine &CmdL, APT_CMD const Binary,/*{{{*/
90986d4d
DK
446 Configuration * const * const Cnf, pkgSystem ** const Sys, int const argc, const char *argv[],
447 bool (*ShowHelp)(CommandLine &), std::vector<aptDispatchWithHelp> (*GetCommands)(void))
ad7e0941 448{
c3ded84c
DK
449 if (Cnf != NULL && pkgInitConfig(**Cnf) == false)
450 {
451 _error->DumpErrors();
452 exit(100);
453 }
454
455 if (likely(argc != 0 && argv[0] != NULL))
501cd23e 456 BinarySpecificConfiguration(argv[0]);
c3ded84c 457
6079b276
DK
458 std::vector<aptDispatchWithHelp> const CmdsWithHelp = GetCommands();
459 std::vector<CommandLine::Dispatch> Cmds;
41d39345
DK
460 if (CmdsWithHelp.empty() == false)
461 {
462 CommandLine::Dispatch const help = { "help", [](CommandLine &){return false;} };
463 Cmds.push_back(std::move(help));
464 }
6079b276
DK
465 for (auto const& cmd : CmdsWithHelp)
466 Cmds.push_back({cmd.Match, cmd.Handler});
467
011188e3
DK
468 // Args running out of scope invalidates the pointer stored in CmdL,
469 // but we don't use the pointer after this function, so we ignore
470 // this problem for now and figure something out if we have to.
471 std::vector<CommandLine::Args> Args;
472 if (Cmds.empty() == false && Cmds[0].Handler != nullptr)
473 Args = getCommandArgs(Binary, CommandLine::GetCommand(Cmds.data(), argc, argv));
474 else
475 Args = getCommandArgs(Binary, nullptr);
476 CmdL = CommandLine(Args.data(), _config);
477
c3ded84c 478 if (CmdL.Parse(argc,argv) == false ||
ad7e0941
DK
479 (Sys != NULL && pkgInitSystem(*_config, *Sys) == false))
480 {
481 if (_config->FindB("version") == true)
90986d4d 482 ShowCommonHelp(Binary, CmdL, CmdsWithHelp, ShowHelp);
ad7e0941
DK
483
484 _error->DumpErrors();
485 exit(100);
486 }
487
488 // See if the help should be shown
cbbee23e
DK
489 if (_config->FindB("help") == true || _config->FindB("version") == true ||
490 (CmdL.FileSize() > 0 && strcmp(CmdL.FileList[0], "help") == 0))
ad7e0941 491 {
90986d4d 492 ShowCommonHelp(Binary, CmdL, CmdsWithHelp, ShowHelp);
ad7e0941
DK
493 exit(0);
494 }
011188e3 495 if (Cmds.empty() == false && CmdL.FileSize() == 0)
ad7e0941 496 {
90986d4d 497 ShowCommonHelp(Binary, CmdL, CmdsWithHelp, ShowHelp);
ad7e0941
DK
498 exit(1);
499 }
011188e3 500 return Cmds;
ad7e0941
DK
501}
502 /*}}}*/
6079b276 503unsigned short DispatchCommandLine(CommandLine &CmdL, std::vector<CommandLine::Dispatch> const &Cmds) /*{{{*/
e7e10e47
DK
504{
505 // Match the operation
011188e3 506 bool const returned = Cmds.empty() ? true : CmdL.DispatchArg(Cmds.data());
e7e10e47
DK
507
508 // Print any errors or warnings found during parsing
509 bool const Errors = _error->PendingError();
510 if (_config->FindI("quiet",0) > 0)
511 _error->DumpErrors();
512 else
513 _error->DumpErrors(GlobalError::DEBUG);
514 if (returned == false)
515 return 100;
516 return Errors == true ? 100 : 0;
517}
518 /*}}}*/