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