Package: lens-cli
lenslensis a command line tool for easy traversal and
pretty printing fo data structures from the terminal.
It is extensible.Uses the greatPygmentslibrary.It should work on both Python 2 and 3, but was developed
with Python 3 in mind.InstallationThe package is yet registered to PyPI. That means you should
be able to install it with pip.lenswas already taken, so
we usedlens-cli.pip install lens-cliUsageCallinglens -hwill print this message:usage: lens [-h] [--input INPUT] [--format FORMAT]
            [--no-highlight NO_HIGHLIGHT]
            [key [key ...]]

Extensible data structure traversal from the command line

positional arguments:
  key                   the keys to traverse

optional arguments:
  -h, --help            show this help message and exit
  --input INPUT, -i INPUT
                        the input file (defaults to the standard input)
  --format FORMAT, -f FORMAT
                        the data format to consume (defaults to json)
  --no-highlight NO_HIGHLIGHT, -n NO_HIGHLIGHT
                        prevent syntax highlightingThis should be relatively straightforward. Let's go through a few examples:# just calling lens will print everything, syntax-highlighted$curl-shttps://httpbin.org/get|lens{"args":{},"headers":{"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Encoding":"gzip, deflate, br","Accept-Language":"de,en-US;q=0.7,en;q=0.3","Dnt":"1","Host":"httpbin.org","Referer":"https://httpbin.org/","Upgrade-Insecure-Requests":"1","User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:50.0) Gecko/20100101 Firefox/50.0"},"origin":"213.61.134.12","url":"https://httpbin.org/get"}# suppose we only want to see the "Host" header$curl-shttps://httpbin.org/get|lensheaderHost"httpbin.org"# or use xml$curl-shttps://httpbin.org/xml|lens-f=xml
<slideshowtitle="Sample Slide Show"date="Date of publication"author="Yours Truly"><slidetype="all"><title>WakeuptoWonderWidgets!</title></slide><slidetype="all"><title>Overview</title><item>Why<em>WonderWidgets</em>aregreat</item><item/><item>Who<em>buys</em>WonderWidgets</item></slide>
</slideshow># suppose we want to only get the title of the second slide$curl-shttps://httpbin.org/xml|lens-f=xmlslide1title
<title>Overview</title>We can also read from files by providing an-ioption.
lens also discovers if its output is redirected to a file, in which case
no syntax highlighting will be applied (the same can be achieved through
the--no-highlighting/-noption).ExtendingYou can write your own parsers/traversers - admittedly, parsers
is not a great name, but for now we stick with it.They should inherit fromLensParserinlens.parsers.base,
and at least implement the methodtreat(self, inpt, keys),
whereinptis the input string andkeysare the keys to
traverse. It should return the traversed data structure as a
string.Optionally, the parser can specify a Pygments lexer by exposing
the static attributelexer, if highlighting is applicable. It
will work without one, though.The plugins can be third partypipmodules, in which case they
should follow the naming schemelens-{format-name}and export
the parser under the nameParser.An example of a plugin can be found in thebsonparser repository.That is all you need to know!Have fun!