Skip to content

Commit 82412c5

Browse files
veloman-yunkanmgautierfr
authored andcommitted
Respecting the --ns option in zimdump show
1 parent c673e5f commit 82412c5

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

src/zimdump.cpp

+29-11
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class ZimDumper
103103
int listEntriesByNamespace(const std::string ns, bool details);
104104

105105
zim::Entry getEntryByPath(const std::string &path);
106+
zim::Entry getEntryByNsAndPath(char ns, const std::string &path);
106107
zim::Entry getEntry(zim::size_type idx);
107108

108109
void dumpFiles(const std::string& directory, bool symlinkdump, std::function<bool (const char c)> nsfilter);
@@ -116,6 +117,11 @@ zim::Entry ZimDumper::getEntryByPath(const std::string& path)
116117
return m_archive.getEntryByPath(path);
117118
}
118119

120+
zim::Entry ZimDumper::getEntryByNsAndPath(char ns, const std::string &path)
121+
{
122+
return m_archive.getEntryByPathWithNamespace(ns, path);
123+
}
124+
119125
zim::Entry ZimDumper::getEntry(zim::size_type idx)
120126
{
121127
return m_archive.getEntryByPath(idx);
@@ -380,7 +386,9 @@ Return value:
380386
See DIR/dump_errors.log for the listing of the errors.
381387
)";
382388

383-
int subcmdInfo(ZimDumper &app, std::map<std::string, docopt::value> &args)
389+
typedef std::map<std::string, docopt::value> Options;
390+
391+
int subcmdInfo(ZimDumper &app, Options &args)
384392
{
385393
app.printInfo();
386394
return 0;
@@ -396,7 +404,7 @@ int subcmdDumpAll(ZimDumper &app, const std::string &outdir, bool redirect, std:
396404
return 0;
397405
}
398406

399-
int subcmdDump(ZimDumper &app, std::map<std::string, docopt::value> &args)
407+
int subcmdDump(ZimDumper &app, Options &args)
400408
{
401409
bool redirect = args["--redirect"].asBool();
402410

@@ -419,22 +427,33 @@ int subcmdDump(ZimDumper &app, std::map<std::string, docopt::value> &args)
419427
return subcmdDumpAll(app, directory, redirect, filter);
420428
}
421429

422-
int subcmdShow(ZimDumper &app, std::map<std::string, docopt::value> &args)
430+
zim::Entry getEntry(ZimDumper &app, Options &args)
431+
{
432+
if (args["--idx"]) {
433+
return app.getEntry(args["--idx"].asLong());
434+
}
435+
436+
const std::string entryPath = args["--url"].asString();
437+
const auto ns = args["--ns"];
438+
if ( !ns ) {
439+
return app.getEntryByPath(entryPath);
440+
}
441+
442+
return app.getEntryByNsAndPath(ns.asString()[0], entryPath);
443+
}
444+
445+
int subcmdShow(ZimDumper &app, Options &args)
423446
{
424447
// docopt guaranty us that we have `--idx` or `--url`.
425448
try {
426-
if (args["--idx"]) {
427-
return app.dumpEntry(app.getEntry(args["--idx"].asLong()));
428-
} else {
429-
return app.dumpEntry(app.getEntryByPath(args["--url"].asString()));
430-
}
449+
return app.dumpEntry(getEntry(app, args));
431450
} catch(...) {
432451
std::cerr << "Entry not found" << std::endl;
433452
return -1;
434453
}
435454
}
436455

437-
int subcmdList(ZimDumper &app, std::map<std::string, docopt::value> &args)
456+
int subcmdList(ZimDumper &app, Options &args)
438457
{
439458
bool idx(args["--idx"]);
440459
bool url(args["--url"]);
@@ -465,8 +484,7 @@ int main(int argc, char* argv[])
465484
int ret = 0;
466485
std::ostringstream versions;
467486
printVersions(versions);
468-
std::map<std::string, docopt::value> args
469-
= docopt::docopt(USAGE,
487+
Options args = docopt::docopt(USAGE,
470488
{ argv + 1, argv + argc },
471489
true,
472490
versions.str());

0 commit comments

Comments
 (0)