xref: /freebsd/contrib/libxo/doc/xo.rst (revision 190cef3d)
1
2.. index:: --libxo, xo
3
4The "xo" Utility
5================
6
7The `xo` utility allows command line access to the functionality of
8the libxo library.  Using `xo`, shell scripts can emit XML, JSON, and
9HTML using the same commands that emit text output.
10
11The style of output can be selected using a specific option: "-X" for
12XML, "-J" for JSON, "-H" for HTML, or "-T" for TEXT, which is the
13default.  The "--style <style>" option can also be used.  The standard
14set of "--libxo" options are available (see :ref:`options`), as well
15as the `LIBXO_OPTIONS`_ environment variable.
16
17.. _`LIBXO_OPTIONS`: :ref:`libxo-options`
18
19The `xo` utility accepts a format string suitable for `xo_emit` and
20a set of zero or more arguments used to supply data for that string::
21
22    xo "The {k:name} weighs {:weight/%d} pounds.\n" fish 6
23
24  TEXT:
25    The fish weighs 6 pounds.
26  XML:
27    <name>fish</name>
28    <weight>6</weight>
29  JSON:
30    "name": "fish",
31    "weight": 6
32  HTML:
33    <div class="line">
34      <div class="text">The </div>
35      <div class="data" data-tag="name">fish</div>
36      <div class="text"> weighs </div>
37      <div class="data" data-tag="weight">6</div>
38      <div class="text"> pounds.</div>
39    </div>
40
41The `--wrap $path` option can be used to wrap emitted content in a
42specific hierarchy.  The path is a set of hierarchical names separated
43by the '/' character::
44
45    xo --wrap top/a/b/c '{:tag}' value
46
47  XML:
48    <top>
49      <a>
50        <b>
51          <c>
52            <tag>value</tag>
53          </c>
54        </b>
55      </a>
56    </top>
57  JSON:
58    "top": {
59      "a": {
60        "b": {
61          "c": {
62            "tag": "value"
63          }
64        }
65      }
66    }
67
68The `--open $path` and `--close $path` can be used to emit
69hierarchical information without the matching close and open
70tag.  This allows a shell script to emit open tags, data, and
71then close tags.  The `--depth` option may be used to set the
72depth for indentation.  The `--leading-xpath` may be used to
73prepend data to the XPath values used for HTML output style::
74
75  EXAMPLE;
76    #!/bin/sh
77    xo --open top/data
78    xo --depth 2 '{tag}' value
79    xo --close top/data
80  XML:
81    <top>
82      <data>
83        <tag>value</tag>
84      </data>
85    </top>
86  JSON:
87    "top": {
88      "data": {
89        "tag": "value"
90      }
91    }
92
93Command Line Options
94--------------------
95
96::
97
98  Usage: xo [options] format [fields]
99    --close <path>        Close tags for the given path
100    --depth <num>         Set the depth for pretty printing
101    --help                Display this help text
102    --html OR -H          Generate HTML output
103    --json OR -J          Generate JSON output
104    --leading-xpath <path> Add a prefix to generated XPaths (HTML)
105    --open <path>         Open tags for the given path
106    --pretty OR -p        Make 'pretty' output (add indent, newlines)
107    --style <style>       Generate given style (xml, json, text, html)
108    --text OR -T          Generate text output (the default style)
109    --version             Display version information
110    --warn OR -W          Display warnings in text on stderr
111    --warn-xml            Display warnings in xml on stdout
112    --wrap <path>         Wrap output in a set of containers
113    --xml OR -X           Generate XML output
114    --xpath               Add XPath data to HTML output);
115
116Example
117-------
118
119::
120
121  % xo 'The {:product} is {:status}\n' stereo "in route"
122  The stereo is in route
123  % ./xo/xo -p -X 'The {:product} is {:status}\n' stereo "in route"
124  <product>stereo</product>
125  <status>in route</status>
126