diff options
author | Julian Andres Klode <julian.klode@canonical.com> | 2020-03-12 21:02:40 +0100 |
---|---|---|
committer | Julian Andres Klode <julian.klode@canonical.com> | 2020-03-12 21:04:50 +0100 |
commit | e93dbf2800bca2a6b8319826b7ad2243b4067faa (patch) | |
tree | 5c101881596cae2660bcd9365cfeb7de22ba31b9 /cmdline | |
parent | 61ebf627f766eb7f189042fc216bb822ac0ef7f4 (diff) |
apt-helper: Add analyze-pattern helper
The analyze-pattern helper parses a pattern and then renders
the parsed pattern, allowing you to analyze how the parser
interpreted the string.
This can be useful to analyse (yes, analyse-pattern also works)
why a pattern is different from aptitude or why it does not
work as expected.
It can also be used to check if apt has pattern support, although
that will miss out on the version shipped in eoan, but who really
cares about that longer term anyway?
Diffstat (limited to 'cmdline')
-rw-r--r-- | cmdline/apt-helper.cc | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/cmdline/apt-helper.cc b/cmdline/apt-helper.cc index 97a4342f5..3d6a692e0 100644 --- a/cmdline/apt-helper.cc +++ b/cmdline/apt-helper.cc @@ -10,6 +10,7 @@ #include <apt-pkg/acquire-item.h> #include <apt-pkg/acquire.h> #include <apt-pkg/cmndline.h> +#include <apt-pkg/cachefilter-patterns.h> #include <apt-pkg/configuration.h> #include <apt-pkg/error.h> #include <apt-pkg/fileutl.h> @@ -257,6 +258,35 @@ static bool DropPrivsAndRun(CommandLine &CmdL) /*{{{*/ return ExecWait(pid, CmdL.FileList[1]); } /*}}}*/ +static bool AnalyzePattern(CommandLine &CmdL) /*{{{*/ +{ + if (CmdL.FileSize() != 2) + return _error->Error("Expect one argument, a pattern"); + + try + { + auto top = APT::Internal::PatternTreeParser(CmdL.FileList[1]).parseTop(); + top->render(std::cout) << "\n"; + } + catch (APT::Internal::PatternTreeParser::Error &e) + { + std::stringstream ss; + ss << "input:" << e.location.start << "-" << e.location.end << ": error: " << e.message << "\n"; + ss << CmdL.FileList[1] << "\n"; + for (size_t i = 0; i < e.location.start; i++) + ss << " "; + for (size_t i = e.location.start; i < e.location.end; i++) + ss << "^"; + + ss << "\n"; + + _error->Error("%s", ss.str().c_str()); + return false; + } + + return true; +} + /*}}}*/ static bool ShowHelp(CommandLine &) /*{{{*/ { std::cout << @@ -278,6 +308,8 @@ static std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/ {"auto-detect-proxy", &DoAutoDetectProxy, _("detect proxy using apt.conf")}, {"wait-online", &DoWaitOnline, _("wait for system to be online")}, {"drop-privs", &DropPrivsAndRun, _("drop privileges before running given command")}, + {"analyze-pattern", &AnalyzePattern, _("analyse a pattern")}, + {"analyse-pattern", &AnalyzePattern, nullptr}, {nullptr, nullptr, nullptr}}; } /*}}}*/ |