forked from robotology/yarp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyarp_config_files.dox
384 lines (334 loc) · 11.9 KB
/
yarp_config_files.dox
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/**
\page yarp_config_files YARP config files
\tableofcontents
This tutorial covers how to write and read configuration files in YARP's native
format.
In brief, YARP config files look a lot like
<a href="http://en.wikipedia.org/wiki/INI_file">INI files</a>
but are actually closer in spirit to
<a href="http://en.wikipedia.org/wiki/S-expression">S-expressions</a>.
YARP config files translate into `yarp::os::Property` objects
(see e.g. the `yarp::os::Property::fromConfigFile` command).
\section yarp_config_file_basics A minimal config file
Here is a minimal config file, let's call it "config.ini" (the extension
doesn't matter):
\code
width 10
height 15
\endcode
The format of each line is the same as that of the
string representation of the yarp::os::Bottle class,
defined here: \ref data_rep. In summary, each line is
a list of items (strings, numbers, sublists, etc) separated by spaces/tabs.
We call each top-level list a "group", and assign a name to each group based
on its first element.
This "config.ini" example contains two groups, one called
"width", and the other called "height".
\section yarp_config_file_reading Accessing the minimal config file from code
We could read the example config file in \ref yarp_config_file_basics "the above section",
by doing the following in code:
\code
#include <yarp/os/Property.h>
int main() {
yarp::os::Property prop;
prop.fromConfigFile("config.ini");
// ... query prop object here ...
return 0;
}
\endcode
The Property object prop will contain all the groups in the configuration
file. The groups can be accessed by iterating, or by using the
yarp::os::Property::findGroup command. This takes a keyword as an
argument, and returns the group that has that keyword as its first
element. So for config.ini:
\code
prop.findGroup("width") // gives the list [width,10]
prop.findGroup("height") // gives the list [height,15]
\endcode
In general, the list of values associated with a group can be found
with the tail command, which drops the keyword:
\code
prop.findGroup("width").tail() // gives the list [10]
\endcode
Since it is common to use groups of two elements to represent
simple key-value pairs, there is a shorthand command
yarp::os::Property::find to get the second element of a group directly.
It returns a yarp::os::Value object which can easily be converted to
regular C types.
\code
prop.find("width").asInt32() // gives the int 10
prop.find("height").asInt32() // gives the int 15
\endcode
\section yarp_config_file_add A config file with sections
Configuration files often need to have nesting, with
values organized into sections.
Special tags of the form "[NAME]" are one way to achieve this
with YARP config files (or INI-style configuration files in general).
For example, for a file containing:
\code
[SIZE]
width 10
height 15
[APPEARANCE]
color red
\endcode
the Property object we read in code will now contain the
two groups "SIZE" and "APPEARANCE", which will have "width", "height",
and "color" nested within them. In other words:
\code
prop.findGroup("SIZE").find("width").asInt32() // gives 10
prop.findGroup("APPEARANCE").find("color").asString() // gives "red"
\endcode
In fact, in YARP config files, the above structure could also be written as:
\code
SIZE (width 10) (height 15)
APPEARANCE (color red)
\endcode
This is exactly equivalent. The special [SECTION] syntax is just
a convenience, familiar to many from INI files.
Here is the rule. Any list on a separate line in a configuration
file, with any degree of nesting, is a group. That means it can be
found using the findGroup/find commands, with its name being given by
its first element. Any sublists within that group can be found
recursively in code using findGroup/find, again with their names being
given by their first element.
What happens if there are multiple groups at the same level with the
same name? That is undefined, and whatever YARP's current behavior is
for this case is not guaranteed to remain so in the future.
\section yarp_config_file_nesting Nesting configuration files
To include one configuration file, say "preamble.ini", inside another one,
do:
\code
[include "preamble.ini"]
\endcode
This will insert the content of preamble.ini as if cut-and-pasted.
If the .ini file is not located in the current directory you have to specify
the path to reach it.
If you would rather have the content included within a subsection
called FOO, do instead:
\code
[include FOO "preamble.ini"]
\endcode
This last form will result in any sections within preamble.ini
being nested within FOO.
For example, if preamble.ini is:
\code
alpha 0.5
[tweaks]
pnudge 10 -10
\endcode
Then with a config.ini of:
\code
x 10
y 10
[include "preamble.ini"]
\endcode
we end up with top-level groups of x, y, alpha, and tweaks.
With a config.ini of:
\code
x 10
y 10
[include setup "preamble.ini"]
\endcode
we end up with top-level groups of x, y, and setup (alpha and tweaks
are nested within setup).
It is also possible to include a whole directory. In
this case, all files with the extension ".ini" from that directory
will be merged together. An example is given in \ref
yarp_config_file_dir_includes.
\section yarp_config_file_nesting Nesting configuration files belonging to different ResourceFinder contexts
If you need to include a configuration file from a specific context, using yarp::os::ResourceFinder (\subpage yarp_resource_finder_tutorials) do:
\code
[import contextname filename.ini]
\endcode
The import tag also supports multi-level nesting.
The imported file is inserted at the specific point where the import directive is found, as if cut-and-pasted.
It is not possibile to import a file inside a section, as done using the syntax [include FOO filename.ini] previously described.
The options included by the imported file may be overwritten by the options located in the parent configuration file (or viceversa),
so be careful about the order in which the parameters are processed.
This mechanism can be exploited to include a base configuration file and then override specific parameters.
\section yarp_config_file_lists Section collections
Occasionally a config file needs a list of sections containing
similar entries. One convenient way to achieve that is with
a "section prefix". For example:
\code
[camera left]
geom 0.7 0.3
color_correct 0.01 0.0 -0.1
[camera right]
geom -0.7 0.3
color_correct 0.0 0.0 0.0
\endcode
This configuration file is equivalent to:
\code
camera left right
[left]
geom 0.7 0.3
color_correct 0.01 0.0 -0.1
[right]
geom -0.7 0.3
color_correct 0.0 0.0 0.0
\endcode
So we have a handy "camera" list accessible
as prop.findGroup("camera").tail() that gives us the names of the
camera-related groups.
\section yarp_config_file_includes Combining section collections and includes
As of YARP 2.2.8, section collections and include file features can be
combined. In other words, one can have a configuration file like this:
\code
[include camera left "cam_left.ini"]
[include camera right "cam_right.ini"]
\endcode
This takes the material in "cam_left.ini" and places it in a section
called "left", and similarly places the material in "cam_right.ini"
in a section called "right", and then creates a group called "camera"
equivalent to the following line:
\code
camera left right
\endcode
\section yarp_config_file_dir_includes Combining section collections and directory includes
As of YARP 2.3.21, it is possible to include a whole directory in place
of a file. That gives new possibilities for section collections.
Suppose we have a file cam/left.ini containing:
\code
[camera left]
geom 0.7 0.3
color_correct 0.01 0.0 -0.1
\endcode
And a file cam/right.ini containing:
\code
[camera right]
geom -0.7 0.3
color_correct 0.0 0.0 0.0
\endcode
And a file all.ini containing:
\code
[include "cam"]
\endcode
Loading all.ini is equivalent to loading:
\code
camera left right
[left]
geom 0.7 0.3
color_correct 0.01 0.0 -0.1
[right]
geom -0.7 0.3
color_correct 0.0 0.0 0.0
\endcode
Note the ordering of files read may not be defined.
\section yarp_config_file_comments Comments in config files
Use the # character, or //, as your prefer.
\code
# this is a comment
// this is also a comment
\endcode
\section yarp_config_file_quoting Quoting in config files
Keys, values, and section
names containing spaces or special characters
should be surrounded by double-quotes, for example:
\code
"a long key whose string value looks like a list" "(1 2 3)"
\endcode
maps the string "a long key whose string value looks like a list" to the
string "(1 2 3)".
If the quote character needs to be included in a string it
can be itself quoted by preceding it with a backslash character.
\section yarp_config_file_continue Continuing across lines
A long line can be continued on the next line by breaking the
line up with a backslash character directly before the end of line.
For example:
\code
encoders (1 2 3 4 \
5 6 7 8)
\endcode
is the same as:
\code
encoders (1 2 3 4 5 6 7 8)
\endcode
\section yarp_config_list_syntax List syntax
Values in a config file can be any type supported by yarp::os::Bottle.
That includes lists, which are surrounded by parentheses and
have spaces (not commas) between items:
\code
example_list (1 2 3)
nested_list (foo (red (255 0 0)) (green (0 255 0)))
\endcode
Binary blobs are also supported, and are expressed as lists of
space-separated numbers surrounded by square parentheses.
\section yarp_config_file_environment Expansion of variables
By default, environment variables in the config file written as $NAME or
${NAME} will be expanded. To include a dollar sign without expansion,
precede it with a backslash.
Key/value pairs already encountered in the config file can also be
used in this way. For example, in an environment where USER is set
to paulfitz:
\code
username "$USER"
tmpdir "/tmp/robot_${username}_cache"
\endcode
This would give "tmpdir" set to "/tmp/robot_paulfitz_cache".
Extra "pseudo-environment" variables can be made available by calling
yarp::os::Property::fromConfigFile with an extra Property argument.
\section yarp_config_file_equals Compatibility with other INI readers
Occasionally it is nice to be able to read YARP config files from
other systems (e.g. Python has an INI reader). Your best bet to
do that is to include an equals sign between keys and values, with
spaces around the equals. YARP will accept and ignore this, and
it will make other readers happier. For YARP, these two lines are
equivalent:
\code
x 10
x = 10
\endcode
Don't forget the spaces, and don't do anything fancy beyond key/value
pairs.
\section yarp_config_command Command line configuration switches
The yarp::os::ResourceFinder class bundles together some common
ways to configure YARP programs. An instance of this class
is usually passed the command line arguments:
\code
int main(int argc, char *argv[]) {
ResourceFinder prop;
prop.configure(argc,argv);
...
}
\endcode
The yarp::os::ResourceFinder class can be used just like
the yarp::os::Property class mentioned in:
\ref yarp_config_file_reading.
If the user of the program supplies a <tt>--from config.ini</tt> argument
that file shall be read as a configuration file. Values
can be overridden on the command line. For example, if <tt>config.ini</tt>
contains:
\code
width 10
height 15
\endcode
And the program is called with these arguments:
\verbatim
--from config.ini --width 200
\endverbatim
In this case, we get:
\code
prop.find("width").asInt32() // gives the int 200
prop.find("height").asInt32() // gives the int 15
\endcode
This can be handy to override one or two values in a long config file
without needing to copy it and modify it. One wrinkle remains -- how
to override values that are in sections? For example, if <tt>config.ini</tt>
is:
\code
[SIZE]
width 10
height 15
[APPEARANCE]
color red
\endcode
Then calling the program with <tt>--width 200</tt> would simply add
a new top-level <tt>width</tt> value without modifying the <tt>width</tt>
in the <tt>SIZE</tt> section. As of yarp 2.3.20, it is possible to
use a nested notation in the command line, and call the program as:
\verbatim
--from config.ini --SIZE::width 200
\endverbatim
*/