]> git.tuebingen.mpg.de Git - lopsub.git/blob - lopsub-suite.5.m4
debian: Final version of 64-bit time_t transition.
[lopsub.git] / lopsub-suite.5.m4
1 .TH lopsub-suite 5 "DATE()" GIT_VERSION()
2 .SH NAME
3 lopsub-suite \- lopsub suite syntax
4 .SH DESCRIPTION
5 A
6 .B lopsub suite
7 describes the options to a command line utility with zero or more
8 related subcommands. The
9 .BR lopsubgen (1)
10 utility translates a lopsub suite into either C source code or a manual
11 page. The generated C code can be compiled and linked against the
12 lopsub library to produce command line parsers for the main command
13 and all subcommands defined in the suite.
14
15 This document explains the format of a lopsub suite. The overall
16 structure is as follows:
17
18 .EX
19         [suite mysuite]
20         suite directives
21
22         [supercommand sup_cmd]
23                 command directives for sup_cmd
24                 [option opt1]
25                 ... (further options)
26
27         [subcommand sub1]
28                 command directives for sub1
29                 [option opt_1_to_sub1]
30                         option directives for opt_1
31                 ... (further options)
32
33         ... (further subcommands)
34
35         [section see also]
36         ... (optional extra section for man page)
37
38         ... (further extra sections)
39 .EE
40
41 A suite begins with a
42 .B [suite]
43 line which declares the name of the suite. The
44 .B suite directives,
45 all subsequent lines up to the first
46 .B [supercommand]
47 or
48 .B [subcommand]
49 line, state further properties of the suite which are unrelated to
50 any particular command, for example the version number. All available
51 suite directives are summarized below.
52
53 The
54 .B [supercommand]
55 and
56 .B [subcommand]
57 lines indicate the beginning of a command of the suite. The part between this
58 line and the first
59 .B [option]
60 line contains the
61 .B command directives,
62 for example the purpose and the description of the named command.
63 See the section on command directives below.
64
65 Supercommands and subcommands share the same set of possible command
66 directives. They differ mainly in the way the documentation is
67 formatted. There can only be one supercommand but arbitrary many
68 subcommands. For example, the supercommand could be the name of
69 the application, and the subcommands could be "load", "save" "info"
70 and "quit". The subcommand would be passed as the first non-option
71 argument to the supercommand, followed by options specific to that
72 subcommand.
73
74 Of course it is possible to define no subcommands at all. Conversely,
75 one can define only subcommands but no supercommand. This makes
76 sense if the commands are run by some other means, for example in an
77 interactive session from a command prompt.
78
79 Within the command section, an
80 .B [option]
81 line starts an option to the current command. It is followed by
82 .B option directives,
83 which specify, for example, whether or not the option takes
84 an argument. All supported option directives are listed in the
85 corresponding section below.
86
87 Further material for man output can be included between the
88 .B [section]
89 and
90 .B [/section]
91 markers.
92 This text will not be included in the generated .c and .h files and
93 is thus also not part of the short and long help available at run
94 time. The text is not interpreted except that leading whitespace is
95 stripped from each line. Arbitrary roff source can be included here.
96
97 Empty lines and lines starting with a hash character (#) are
98 ignored.
99 .SH SUITE DIRECTIVES
100 Most directives of this section are only relevant for man page output (with
101 .B aux_info_default
102 being the exception), they are ignored for C output.
103 .TP
104 .B caption
105 The optional text for an unnumbered section heading at the beginning of the
106 manual page.
107 .TP
108 .B title
109 Sets the title of the man page. Defaults to the name of the
110 supercommand. If this is not given and the suite has no supercommand,
111 the .TH macro to set the title of the man page is omitted.
112 .TP
113 .B mansect
114 Sets the man page section. Defaults to 1 (user commands). Both title
115 and section are positioned at the left and right in the header line.
116 .TP
117 .B date
118 This text is positioned in the middle of the footer line of the man page. It is
119 common to set this to the date of the last nontrivial change that was made to
120 the man page. Defaults to the current date.
121 .TP
122 .B version-string
123 Positioned at the left in the footer line. Defaults to the empty string. The
124 value of this directive is ignored if a version string is explicitly requested
125 by passing
126 .B --version-string
127 to
128 .BR losubgen .
129 .TP
130 .B manual_title
131 Centered in the header line. Defaults to "User commands".
132 .TP
133 .B introduction
134 .TQ
135 .B conclusion
136 The text enclosed between
137 .B [introduction]
138 and
139 .B [/introduction]
140 is shown between the supercommand (if any) and the subcommand list.
141 Concluding remarks after the subcommand list may be added in the same
142 way with
143 .B [conclusion]
144 and
145 .BR [/conclusion] .
146 Both texts will become part of the manual page, but are not not part
147 of the short or long help.
148 .TP
149 .B aux_info_prefix
150 This text is shown at the bottom of each command before the value of the
151 aux_info directive. If no
152 .B aux_info
153 is specified for a command, the prefix is omitted as well. If
154 .B aux_info_prefix
155 is not given, the empty string is assumed.
156 .TP
157 .B aux_info_default
158 This text is only used for header output.  The argument for the generated macro
159 call is set to this value for all commands for which no
160 .B aux_info
161 directive is given. If no
162 .B aux_info_default
163 directive is given, the value 0 is used as the argument for the macro.
164 .SH COMMAND DIRECTIVES
165 .TP
166 .B purpose
167 A single line containing the purpose of the command. This text is printed in
168 the short and long help, and is also included in the man page.
169 .TP
170 .B description
171 .TQ
172 .B closing
173 Arbitrary plain text enclosed between
174 .B [description]
175 and
176 .BR [/description] .
177 The text may be split over multiple lines and paragraphs. The
178 description of the supercommand (if any) becomes the description of
179 the manual page, which is shown after the command summary and before
180 the list of options. The descriptions of all commands are included
181 in the manual page and in the long help text but not in the short help.
182
183 Closing remarks for the command can be added in a similar way by enclosing
184 plain text between
185 .B [closing]
186 and
187 .BR [/closing] .
188 This text will be positioned after the option list.
189 .TP
190 .B synopsis
191 Custom synopsis of the command. If this is not given, the synopsis text will be
192 auto-generated from the options and the value of the
193 .B non-opts-name
194 directive.
195 .TP
196 .B non-opts-name
197 Name of the command line arguments which are not related to any option,
198 for example file names. This text will be included in the automatically
199 generated synopsis text.
200
201 If this is not given, the command is assumed to take no arguments other than
202 the specified options and their arguments. For such commands the attempt to
203 parse an argv[] vector fails if it contains further non-option arguments.
204 .TP
205 .B aux_info
206 This directive is special because its value is not included in the generated .c
207 file but in the header file. More precisely, the preprocessor macro
208 .B \%LSG_SUITENAME_AUX_INFOS
209 will be defined which expands to a series of macro calls to
210 .B \%LSG_SUITENAME_AUX_INFO(val_n),
211 one for each command of the suite, where
212 .B val_n
213 is the (unquoted) value of the
214 .B aux_info
215 directive of the nth command. Commands for which no
216 .B aux_info
217 directive was specified receive a value of zero. The
218 .B \%LSG_SUITENAME_AUX_INFO
219 macro is supposed to be defined by the application.
220 Hence it is up to the application to make the expansion of
221 .B \%LSG_SUITENAME_AUX_INFOS
222 a valid C construct.
223
224 The value, if specified, is also copied to the man page at the end of the
225 section for the command.
226
227 .SH OPTION DIRECTIVES
228 .TP
229 .B short_opt
230 The optional single-letter equivalent for the option. If this is
231 specified, the option may be given either in the GNU-style long
232 option format with two leading dashes or in the short form with a
233 single dash. Otherwise only the long form will be accepted. As usual,
234 multiple short option flags may be combined.
235 .TP
236 .B summary
237 A single line which summarizes the option. This text is included in
238 both the short and the long help and in the man page. Defaults to
239 the empty string.
240 .TP
241 .B typestr
242 A description for the type of the values for the option. The given text
243 is printed in the synopsis of the command, which is part of the short
244 and the long help and the man page. It defaults to the string "val".
245
246 This directive is ignored for flag options (options without an argument).
247 .TP
248 .B arg_info
249 This directive determines whether the option takes an argument. The
250 possible values are
251 .B no_arg, required_arg
252 and
253 .B optional_arg
254 which indicate, respectively, that the option takes no argument at all,
255 an argument which is mandatory, or an argument which may be omitted. The
256 default value is
257 .B no_arg.
258 Hence an option works as a flag if the
259 .B arg_info
260 directive is not given.
261
262 Note that arguments to options which take an optional argument must
263 be given as --foo=bar rather than --foo bar because the latter form
264 would be ambiguous.
265 .TP
266 .B arg_type
267 For flag options this directive should be set to
268 .B none,
269 or not set at all. For options which take an argument, the value of the directive
270 determines the type of the argument.
271 Possible values are
272 .B string
273 for options which take a string argument, and
274 .B int32,
275 .B uint32.
276 .B int64,
277 .B uint64,
278 for options which take a numeric argument.
279 .TP
280 .B flag
281 Lopsub maintains for each option a bitmask which contains the value
282 of each possible flag for this option. Flags may be accumulated by
283 defining multiple flag directives in the suite. Note there is no
284 equal sign between the
285 .B flag
286 directive and its value.
287
288 The following flags are defined.
289 .RS
290 .B
291 .IP multiple
292 This flag instructs the lopsub library to keep track of all given
293 arguments to an option, not just one as for ordinary options. This
294 is only relevant for options which take an (optional or required)
295 argument.
296 .B
297 .IP required
298 Instruct the parser to fail if this option is not given in the
299 argument vector. If an option may be given at the command line or
300 in the config file, this flag should be avoided because the command
301 line argv vector will not be parsed successfully if the option is
302 only given in the config file. The recommended way to deal with
303 this situation is to parse command line and config file separately,
304 then merge the two parse results and check in the application if the
305 option is given in the merged parse result.
306
307 There is another disadvantage of this flag: if the parser fails due
308 to a missing option that was declared required, it is not possible to
309 detect if other options were given. For example, if the suite defines
310 the --help option, and the application is executed with this option
311 only, the parser will still return a parse error.
312 .B
313 .IP ignored
314 This flag indicates that the current option is in fact not a real option.
315 In particular,
316 .B name
317 and
318 .B short_opt
319 are both ignored. The purpose of this flag is to add additional
320 information for the help output and the man page.
321 .RE
322 .TP
323 .B values
324 Create an enumerable option.
325
326 Enumerable options take one out of a fixed set of possible values
327 which are predefined in the suite. Such options are always of type
328 string. It is an error if a different argument type was specified or
329 if the option was defined to not take an argument.
330
331 The syntax for the array of values is
332
333 .EX
334         values = {ID_0 = "string_0", ..., ID_N = "string_N"}
335 .EE
336
337 For each option for which the
338 .B values
339 directive was specified, the lopsubgen command generates a C
340 enumeration which contains the given identifiers. This allows
341 referring to each possible value through a numeric constant.
342 .TP
343 .B default_val
344 This directive makes only sense for options which take an argument. For
345 such options it defines the value the option parser provides
346 automatically if the option is not given in the argument vector.
347
348 If no
349 .B default_val
350 is specified in the suite, and the option is not given in the argument
351 vector either, the implied value depends on the argument type. For
352 numeric options, a value of zero is assumed. For options which take
353 a string argument, a NULL pointer is returned. For enum options,
354 the first possible value (index zero) is taken as the default.
355 .TP
356 .B help
357 The detailed, multi-line help text for the current option. Included
358 in the man page and the long help.
359 .SH EXAMPLE
360 A minimal suite and the corresponding "application".
361
362 The suite file
363 .IR hello.suite :
364
365 .EX
366         [suite hello]
367         [supercommand hello]
368                 purpose = hello world
369                 [option world]
370                         summary = add "world"
371 .EE
372
373 The "application"
374 .IR hello.c :
375
376 .EX
377         #include <stdio.h> /* printf(3) */
378         #include <stdlib.h> /* exit(3) */
379         #include <lopsub.h>
380         #include "hello.lsg.h"
381
382         int main(int argc, char **argv)
383         {
384                 struct lls_parse_result *lpr;
385                 const struct lls_opt_result *result;
386                 const struct lls_command *cmd = lls_cmd(0, hello_suite);
387                 int ret;
388
389                 ret = lls_parse(argc, argv, cmd, &lpr, NULL);
390                 if (ret < 0) {
391                         fprintf(stderr, "%s\\n", lls_strerror(-ret));
392                         exit(1);
393                 }
394                 printf("hello");
395                 result = lls_opt_result(LSG_HELLO_HELLO_OPT_WORLD, lpr);
396                 if (lls_opt_given(result))
397                         printf(" world");
398                 printf("\\n");
399                 exit(0);
400         }
401 .EE
402
403 Generate
404 .I hello.lsg.c
405 and
406 .IR hello.lsg.h :
407
408 .EX
409         $ lopsubgen --gen-c --gen-h < hello.suite
410 .EE
411
412 Compile
413 .I hello.c
414 and
415 .I hello.lsg.c
416 to produce the
417 .I hello
418 executable:
419
420 .EX
421         $ cc hello.c hello.lsg.c -o hello -llopsub
422 .EE
423
424 Run it:
425
426 .EX
427         $ ./hello
428         hello
429         $ ./hello --world
430         hello world
431         $ ./hello -w
432         option not recognized
433         $ ./hello 42
434         invalid number of arguments
435 .EE
436
437 Generate and examine the manual page:
438
439 .EX
440         $ lopsubgen --gen-man < hello.suite
441         $ man -l ./hello.lsg.man
442 .EE
443
444 .SH SEE ALSO
445 .BR lopsubgen (1),
446 .BR lopsub (7),
447 .BR lopsubex (1),
448 .BR gengetopt (1),
449 .BR getopt (1),
450 .BR getopt (3)