libxo —
library for emitting text, XML, JSON,
or HTML output
Text, XML, JSON, and HTML Output Emission Library
(libxo, -lxo)
The functions defined in libxo are used to
generate a choice of
TEXT,
XML,
JSON, or
HTML
output. A common set of functions are used, with command line switches
passed to the library to control the details of the output.
Most commands emit text output aimed at humans. It is designed to
be parsed and understood by a user. Humans are gifted at extracting details
and pattern matching. Often programmers need to extract information from
this human-oriented output. Programmers use tools like
grep(1),
awk(1), and regular expressions to ferret out the pieces of
information they need. Such solutions are fragile and require updates when
output contents change or evolve, requiring testing and validation.
Modern tool developers favor encoding schemes like XML and JSON,
which allow trivial parsing and extraction of data. Such formats are simple,
well understood, hierarchical, easily parsed, and often integrate easier
with common tools and environments.
In addition, modern reality means that more output ends up in web
browsers than in terminals, making HTML output valuable.
libxo allows a single set of function
calls in source code to generate traditional text output, as well as XML and
JSON formatted data. HTML can also be generated; "<div>"
elements surround the traditional text output, with attributes that detail
how to render the data.
There are four encoding styles supported by
libxo:
- TEXT output can be display on a terminal session, allowing compatibility
with traditional command line usage.
- XML output is suitable for tools like XPath and protocols like
NETCONF.
- JSON output can be used for RESTful APIs and integration with languages
like Javascript and Python.
- HTML can be matched with a small CSS file to permit rendering in any HTML5
browser.
In general, XML and JSON are suitable for encoding data, while
TEXT is suited for terminal output and HTML is suited for display in a web
browser (see
xohtml(1) ).
libxo uses command line options to trigger
rendering behavior. The following options are recognised:
- --libxo <options>
- --libxo=<options>
- --libxo:<brief-options>
Options is a comma-separated list of tokens that correspond to
output styles, flags, or features:
- Token
Action
-
dtrt
- Enable "Do The Right Thing" mode
html
- Emit HTML output
indent=xx
- Set the indentation level
info
- Add info attributes (HTML)
json
- Emit JSON output
keys
- Emit the key attribute for keys (XML)
log-gettext
- Log (via stderr) each
gettext(3) string lookup
log-syslog
- Log (via stderr) each syslog message (via
xo_syslog(3))
no-humanize
- Ignore the {h:} modifier (TEXT, HTML)
no-locale
- Do not initialize the locale setting
no-retain
- Prevent retaining formatting information
no-top
- Do not emit a top set of braces (JSON)
not-first
- Pretend the 1st output item was not 1st (JSON)
pretty
- Emit pretty-printed output
retain
- Force retaining formatting information
text
- Emit TEXT output
underscores
- Replace XML-friendly "-"s with JSON friendly "_"s
e
units
- Add the 'units' (XML) or 'data-units (HTML) attribute
warn
- Emit warnings when libxo detects bad calls
warn-xml
- Emit warnings in XML
xml
- Emit XML output
xpath
- Add XPath expressions (HTML)
The “brief-options” are single letter commands,
designed for those with too little patience to use real tokens. No comma
separator is used.
| Token
Action |
| H Enable HTML output (XO_STYLE_HTML) |
| I Enable info output (XOF_INFO) |
| i<num> Indent by <number> |
| J Enable JSON output (XO_STYLE_JSON) |
| P Enable pretty-printed output (XOF_PRETTY) |
| T Enable text output (XO_STYLE_TEXT) |
| W Enable warnings (XOF_WARN) |
| X Enable XML output (XO_STYLE_XML) |
| x Enable XPath data (XOF_XPATH) |
Refer to
xo_options(7) for additional option information.
The libxo library allows an application to
generate text, XML, JSON, and HTML output using a common set of function
calls. The application decides at run time which output style should be
produced. The application calls a function
xo_emit(3) to product output that is described in a format
string. A “field descriptor” tells
libxo what the field is and what it means. Each
field descriptor is placed in braces with a printf-like format string:
xo_emit(" {:lines/%7ju} {:words/%7ju} "
"{:characters/%7ju}{d:filename/%s}\n",
linect, wordct, charct, file);
Each field can have a role, with the 'value' role being the
default, and the role tells libxo how and when to
render that field, as well as a
printf(3)-like format string.
Output can then be generated in various style, using the
"--libxo" option.
Handles give an abstraction for libxo that
encapsulates the state of a stream of output. Handles have the data type
"xo_handle_t" and are opaque to the caller.
The library has a default handle that is automatically
initialized. By default, this handle will send text style output to standard
output. The
xo_set_style(3) and
xo_set_flags(3) functions can be used to change this
behavior.
Many libxo functions take a handle as
their first parameter; most that do not use the default handle. Any function
taking a handle can be passed NULL to access the
default handle.
For the typical command that is generating output on standard
output, there is no need to create an explicit handle, but they are
available when needed, e.g., for daemons that generate multiple streams of
output.
libxo-csv(7,)
xo(1),
xolint(1),
xo_attr(3),
xo_create(3),
xo_emit(3),
xo_emit_err(3),
xo_err(3),
xo_finish(3),
xo_flush(3),
xo_no_setlocale(3),
xo_open_container(3),
xo_open_list(3),
xo_options(7,)
xo_parse_args(3),
xo_set_allocator(3),
xo_set_flags(3),
xo_set_info(3),
xo_set_options(3),
xo_set_style(3),
xo_set_writer(3),
xo_format(5)
The libxo library first appeared in
FreeBSD 11.0.