7 The `xo` utility allows command line access to the functionality of
8 the libxo library. Using `xo`, shell scripts can emit XML, JSON, and
9 HTML using the same commands that emit text output.
11 The style of output can be selected using a specific option: "-X" for
12 XML, "-J" for JSON, "-H" for HTML, or "-T" for TEXT, which is the
13 default. The "--style <style>" option can also be used. The standard
14 set of "--libxo" options are available (see :ref:`options`), as well
15 as the :ref:`LIBXO_OPTIONS <libxo-options>` environment variable.
17 The `xo` utility accepts a format string suitable for `xo_emit` and
18 a set of zero or more arguments used to supply data for that string::
20 xo "The {k:name} weighs {:weight/%d} pounds.\n" fish 6
23 The fish weighs 6 pounds.
35 <div class="text">The </div>
36 <div class="data" data-tag="name">fish</div>
37 <div class="text"> weighs </div>
38 <div class="data" data-tag="weight">6</div>
39 <div class="text"> pounds.</div>
42 The `--wrap $path` option can be used to wrap emitted content in a
43 specific hierarchy. The path is a set of hierarchical names separated
44 by the '/' character::
46 xo --wrap top/a/b/c '{:tag}' value
70 The `--open $path` and `--close $path` can be used to emit
71 hierarchical information without the matching close and open
72 tag. This allows a shell script to emit open tags, data, and
73 then close tags. The `--depth` option may be used to set the
74 depth for indentation. The `--leading-xpath` may be used to
75 prepend data to the XPath values used for HTML output style::
80 xo --depth 2 '{:tag}' value
97 When making partial lines of output (where the format string does not
98 include a newline), use the `--continuation` option to let secondary
99 invocations know they are adding data to an existing line.
101 When emitting a series of objects, use the `--not-first` option to
102 ensure that any details from the previous object (e.g. commas in JSON)
103 are handled correctly.
105 Use the `--top-wrap` option to ensure any top-level object details are
106 handled correctly, e.g. wrap the entire output in a top-level set of
107 braces for JSON output.
113 xo --top-wrap --open top/data
114 xo --depth 2 'First {:tag} ' value1
115 xo --depth 2 --continuation 'and then {:tag}\n' value2
116 xo --top-wrap --close top/data
119 First value1 and then value2
123 <div class="text">First </div>
124 <div class="data" data-tag="tag">value1</div>
125 <div class="text"> </div>
126 <div class="text">and then </div>
127 <div class="data" data-tag="tag">value2</div>
151 A "*list*" is set of one or more instances that appear under the same
152 parent. The instances contain details about a specific object. One
153 can think of instances as objects or records. A call is needed to
154 open and close the list, while a distinct call is needed to open and
155 close each instance of the list.
157 Use the `--open-list` and `--open-instances` to open lists and
158 instances. Use the `--close-list` and `--close-instances` to close
159 them. Each of these options take a `name` parameter, providing the
160 name of the list and instance.
162 In the following example, a list named "machine" is created with three
166 xo $opts --open-list machine
168 for name in red green blue; do
169 xo $opts --depth 1 $NF --open-instance machine
170 xo $opts --depth 2 "Machine {k:name} has {:memory}\n" $name 55
171 xo $opts --depth 1 --close-instance machine
174 xo $opts $NF --close-list machine
176 The normal `libxo` functions use a state machine to help these
177 transitions, but since each `xo` command is invoked independent of the
178 previous calls, the state must be passed in explicitly via these
179 command line options.
181 The `--instance` option can be used to treat a single `xo` invocation
182 as an instance with the given set of fields::
184 % xo --libxo:XP --instance foo 'The {:product} is {:status}\n' stereo "in route"
186 <product>stereo</product>
187 <status>in route</status>
195 Usage: xo [options] format [fields]
196 --close <path> Close tags for the given path
197 --close-instance <name> Close an open instance name
198 --close-list <name> Close an open list name
199 --continuation OR -C Output belongs on same line as previous output
200 --depth <num> Set the depth for pretty printing
201 --help Display this help text
202 --html OR -H Generate HTML output
203 --instance OR -I <name> Wrap in an instance of the given name
204 --json OR -J Generate JSON output
205 --leading-xpath <path> Add a prefix to generated XPaths (HTML)
206 --not-first Indicate this object is not the first (JSON)
207 --open <path> Open tags for the given path
208 --open-instance <name> Open an instance given by name
209 --open-list <name> Open a list given by name
210 --option <opts> -or -O <opts> Give formatting options
211 --pretty OR -p Make 'pretty' output (add indent, newlines)
212 --style <style> Generate given style (xml, json, text, html)
213 --text OR -T Generate text output (the default style)
214 --top-wrap Generate a top-level object wrapper (JSON)
215 --version Display version information
216 --warn OR -W Display warnings in text on stderr
217 --warn-xml Display warnings in xml on stdout
218 --wrap <path> Wrap output in a set of containers
219 --xml OR -X Generate XML output
220 --xpath Add XPath data to HTML output)
227 % xo 'The {:product} is {:status}\n' stereo "in route"
228 The stereo is in route
229 % xo -p -X 'The {:product} is {:status}\n' stereo "in route"
230 <product>stereo</product>
231 <status>in route</status>
232 % xo --libxo xml,pretty 'The {:product} is {:status}\n' stereo "in route"
233 <product>stereo</product>
234 <status>in route</status>