Debian Codegraph @

pod/CmdLine.pod

raw
1 =head1 NAME
2
3 Tk::CmdLine - Process standard X11 command line options and set initial resources
4
5 =for pm Tk/CmdLine.pm
6
7 =for category Creating and Configuring Widgets
8
9 =head1 SYNOPSIS
10
11 Tk::CmdLine::SetArguments([@argument]);
12
13 my $value = Tk::CmdLine::cget([$option]);
14
15 Tk::CmdLine::SetResources((\@resource | $resource) [, $priority]);
16
17 Tk::CmdLine::LoadResources(
18 [ -symbol => $symbol ]
19 [ -file => $fileSpec ]
20 [ -priority => $priority ]
21 [ -echo => $fileHandle ] );
22
23 =head1 DESCRIPTION
24
25 Process standard X11 command line options and set initial resources.
26
27 The X11R5 man page for X11 says: "Most X programs attempt to use the same names
28 for command line options and arguments. All applications written with the
29 X Toolkit Intrinsics automatically accept the following options: ...".
30 This module processes these command line options for perl/Tk applications
31 using the C<SetArguments>() function.
32
33 This module can optionally be used to load initial resources explicitly via
34 function C<SetResources>(), or from specified files (default: the standard X11
35 application-specific resource files) via function C<LoadResources>().
36
37 =head2 Command Line Options
38
39 =over 4
40
41 =item B<-background> I<Color> | B<-bg> I<Color>
42
43 Specifies the color to be used for the window background.
44
45 =item B<-class> I<Class>
46
47 Specifies the class under which resources for the application should be found.
48 This option is useful in shell aliases to distinguish between invocations
49 of an application, without resorting to creating links to alter the executable
50 file name.
51
52 =item B<-display> I<Display> | B<-screen> I<Display>
53
54 Specifies the name of the X server to be used.
55
56 =item B<-font> I<Font> | B<-fn> I<Font>
57
58 Specifies the font to be used for displaying text.
59
60 =item B<-foreground> I<Color> | B<-fg> I<Color>
61
62 Specifies the color to be used for text or graphics.
63
64 =item B<-geometry> I<Geometry>
65
66 Specifies the initial size and location of the I<first>
67 L<MainWindow|Tk::MainWindow>.
68
69 =item B<-iconic>
70
71 Indicates that the user would prefer that the application's windows initially
72 not be visible as if the windows had been immediately iconified by the user.
73 Window managers may choose not to honor the application's request.
74
75 =item B<-motif>
76
77 Specifies that the application should adhere as closely as possible to Motif
78 look-and-feel standards. For example, active elements such as buttons and
79 scrollbar sliders will not change color when the pointer passes over them.
80
81 =item B<-name> I<Name>
82
83 Specifies the name under which resources for the application should be found.
84 This option is useful in shell aliases to distinguish between invocations
85 of an application, without resorting to creating links to alter the executable
86 file name.
87
88 =item B<-synchronous>
89
90 Indicates that requests to the X server should be sent synchronously, instead of
91 asynchronously. Since Xlib normally buffers requests to the server, errors do
92 do not necessarily get reported immediately after they occur. This option turns
93 off the buffering so that the application can be debugged. It should never
94 be used with a working program.
95
96 =item B<-title> I<TitleString>
97
98 This option specifies the title to be used for this window. This information is
99 sometimes used by a window manager to provide some sort of header identifying
100 the window.
101
102 =item B<-xrm> I<ResourceString>
103
104 Specifies a resource pattern and value to override any defaults. It is also
105 very useful for setting resources that do not have explicit command line
106 arguments.
107
108 The I<ResourceString> is of the form E<lt>I<pattern>E<gt>:E<lt>I<value>E<gt>,
109 that is (the first) ':' is used to determine which part is pattern and which
110 part is value. The (E<lt>I<pattern>E<gt>, E<lt>I<value>E<gt>) pair is entered
111 into the options database with B<optionAdd> (for each
112 L<MainWindow|Tk::MainWindow> configured), with I<interactive> priority.
113
114 =back
115
116 =head2 Initial Resources
117
118 There are several mechanism for initializing the resource database to be used
119 by an X11 application. Resources may be defined in a $C<HOME>/.Xdefaults file,
120 a system application defaults file (e.g.
121 /usr/lib/X11/app-defaults/E<lt>B<CLASS>E<gt>),
122 or a user application defaults file (e.g. $C<HOME>/E<lt>B<CLASS>E<gt>).
123 The Tk::CmdLine functionality for setting initial resources concerns itself
124 with the latter two.
125
126 Resource files contain data lines of the form
127 E<lt>I<pattern>E<gt>:E<lt>I<value>E<gt>.
128 They may also contain blank lines and comment lines (denoted
129 by a ! character as the first non-blank character). Refer to L<option|Tk::option>
130 for a description of E<lt>I<pattern>E<gt>:E<lt>I<value>E<gt>.
131
132 =over 4
133
134 =item System Application Defaults Files
135
136 System application defaults files may be specified via environment variable
137 $C<XFILESEARCHPATH> which, if set, contains a list of file patterns
138 (joined using the OS-dependent path delimiter, e.g. colon on B<UNIX>).
139
140 =item User Application Defaults Files
141
142 User application defaults files may be specified via environment variables
143 $C<XUSERFILESEARCHPATH>, $C<XAPPLRESDIR> or $C<HOME>.
144
145 =back
146
147 =head1 METHODS
148
149 =over 4
150
151 =item B<SetArguments> - Tk::CmdLine::SetArguments([@argument])
152
153 Extract the X11 options contained in a specified array (@ARGV by default).
154
155 Tk::CmdLine::SetArguments([@argument])
156
157 The X11 options may be specified using a single dash I<-> as per the X11
158 convention, or using two dashes I<--> as per the POSIX standard (e.g.
159 B<-geometry> I<100x100>, B<-geometry> I<100x100> or B<-geometry=>I<100x100>).
160 The options may be interspersed with other options or arguments.
161 A I<--> by itself terminates option processing.
162
163 By default, command line options are extracted from @ARGV the first time
164 a MainWindow is created. The Tk::MainWindow constructor indirectly invokes
165 C<SetArguments>() to do this.
166
167 =item B<GetArguments> - Tk::CmdLine::GetArguments()
168
169 Get a list of the X11 options that have been processed by C<SetArguments>().
170 (C<GetArguments>() first invokes C<SetArguments>() if it has not already been invoked.)
171
172 =item B<cget> - Tk::CmdLine::cget([$option])
173
174 Get the value of a configuration option specified via C<SetArguments>().
175 (C<cget>() first invokes C<SetArguments>() if it has not already been invoked.)
176
177 Tk::CmdLine::cget([$option])
178
179 The valid options are: B<-class>, B<-name>, B<-screen> and B<-title>.
180 If no option is specified, B<-class> is implied.
181
182 A typical use of C<cget>() might be to obtain the application class in order
183 to define the name of a resource file to be loaded in via C<LoadResources>().
184
185 my $class = Tk::CmdLine::cget(); # process command line and return class
186
187 =item B<SetResources> - Tk::CmdLine::SetResources((\@resource | $resource) [, $priority])
188
189 Set the initial resources.
190
191 Tk::CmdLine::SetResources((\@resource | $resource) [, $priority])
192
193 A single resource may be specified using a string of the form
194 'E<lt>I<pattern>E<gt>:E<lt>I<value>E<gt>'. Multiple resources may be specified
195 by passing an array reference whose elements are either strings of the above
196 form, and/or anonymous arrays of the form [ E<lt>I<pattern>E<gt>,
197 E<lt>I<value>E<gt> ]. The optional second argument specifies the priority,
198 as defined in L<option|Tk::option>, to be associated with the resources
199 (default: I<userDefault>).
200
201 Note that C<SetResources>() first invokes C<SetArguments>() if it has not already
202 been invoked.
203
204 =item B<LoadResources> - Tk::CmdLine::LoadResources([%options])
205
206 Load initial resources from one or more files.
207
208 Tk::CmdLine::LoadResources(
209 [ -symbol => $symbol ]
210 [ -file => $fileSpec ]
211 [ -priority => $priority ]
212 [ -echo => $fileHandle ] );
213
214 [ B<-symbol> =E<gt> $symbol ] specifies the name of an environment variable
215 that, if set, defines a list of one or more directories and/or file patterns
216 (joined using the OS-dependent path delimiter, e.g. colon on B<UNIX>).
217 $C<XUSERFILESEARCHPATH> is a special case.
218 If $C<XUSERFILESEARCHPATH> is not set, $C<XAPPLRESDIR> is checked instead.
219 If $C<XAPPLRESDIR> is not set, $C<HOME> is checked instead.
220
221 An item is identified as a file pattern if it contains one or more /%[A-Za-z]/
222 patterns. Only patterns B<%L>, B<%T> and B<%N> are currently recognized. All
223 others are replaced with the null string. Pattern B<%L> is translated into
224 $C<LANG>. Pattern B<%T> is translated into I<app-defaults>. Pattern B<%N> is
225 translated into the application class name.
226
227 Each file pattern, after substitutions are applied, is assumed to define a
228 FileSpec to be examined.
229
230 When a directory is specified, FileSpecs
231 E<lt>B<DIRECTORY>E<gt>/E<lt>B<LANG>E<gt>/E<lt>B<CLASS>E<gt>
232 and E<lt>B<DIRECTORY>E<gt>/E<lt>B<CLASS>E<gt> are defined, in that order.
233
234 [ B<-file> =E<gt> $fileSpec ] specifies a resource file to be loaded in.
235 The file is silently skipped if if does not exist, or if it is not readable.
236
237 [ B<-priority> =E<gt> $priority ] specifies the priority, as defined in
238 L<option|Tk::option>, to be associated with the resources
239 (default: I<userDefault>).
240
241 [ B<-echo> =E<gt> $fileHandle ] may be used to specify that a line should be
242 printed to the corresponding FileHandle (default: \*STDOUT) everytime a file
243 is examined / loaded.
244
245 If no B<-symbol> or B<-file> options are specified, C<LoadResources>()
246 processes symbol $C<XFILESEARCHPATH> with priority I<startupFile> and
247 $C<XUSERFILESEARCHPATH> with priority I<userDefault>.
248 (Note that $C<XFILESEARCHPATH> and $C<XUSERFILESEARCHPATH> are supposed to
249 contain only patterns. $C<XAPPLRESDIR> and $C<HOME> are supposed to be a single
250 directory. C<LoadResources>() does not check/care whether this is the case.)
251
252 For each set of FileSpecs, C<LoadResources>() examines each FileSpec to
253 determine if the file exists and is readable. The first file that meets this
254 criteria is read in and C<SetResources>() is invoked.
255
256 Note that C<LoadResources>() first invokes C<SetArguments>() if it has not already
257 been invoked.
258
259 =back
260
261 =head1 NOTES
262
263 This module is an object-oriented module whose methods can be invoked as object
264 methods, class methods or regular functions. This is accomplished via an
265 internally-maintained object reference which is created as necessary, and which
266 always points to the last object used. C<SetArguments>(), C<SetResources>() and
267 C<LoadResources>() return the object reference.
268
269 =head1 EXAMPLES
270
271 =over
272
273 =item 1
274
275 @ARGV is processed by Tk::CmdLine at MainWindow creation.
276
277 use Tk;
278
279 # <Process @ARGV - ignoring all X11-specific options>
280
281 my $mw = MainWindow->new();
282
283 MainLoop();
284
285 =item 2
286
287 @ARGV is processed by Tk::CmdLine before MainWindow creation.
288 An @ARGV of (--geometry=100x100 -opt1 a b c -bg red)
289 is equal to (-opt1 a b c) after C<SetArguments>() is invoked.
290
291 use Tk;
292
293 Tk::CmdLine::SetArguments(); # Tk::CmdLine->SetArguments() works too
294
295 # <Process @ARGV - not worrying about X11-specific options>
296
297 my $mw = MainWindow->new();
298
299 MainLoop();
300
301 =item 3
302
303 Just like 2) except that default arguments are loaded first.
304
305 use Tk;
306
307 Tk::CmdLine::SetArguments(qw(-name test -iconic));
308 Tk::CmdLine::SetArguments();
309
310 # <Process @ARGV - not worrying about X11-specific options>
311
312 my $mw = MainWindow->new();
313
314 MainLoop();
315
316 =item 4
317
318 @ARGV is processed by Tk::CmdLine before MainWindow creation.
319 Standard resource files are loaded in before MainWindow creation.
320
321 use Tk;
322
323 Tk::CmdLine::SetArguments();
324
325 # <Process @ARGV - not worrying about X11-specific options>
326
327 Tk::CmdLine::LoadResources();
328
329 my $mw = MainWindow->new();
330
331 MainLoop();
332
333 =item 5
334
335 @ARGV is processed by Tk::CmdLine before MainWindow creation.
336 Standard resource files are loaded in before MainWindow creation
337 using non-default priorities.
338
339 use Tk;
340
341 Tk::CmdLine::SetArguments();
342
343 # <Process @ARGV - not worrying about X11-specific options>
344
345 Tk::CmdLine::LoadResources(-echo => \*STDOUT,
346 -priority => 65, -symbol => 'XFILESEARCHPATH' );
347 Tk::CmdLine::LoadResources(-echo => \*STDOUT,
348 -priority => 75, -symbol => 'XUSERFILESEARCHPATH' );
349
350 my $mw = MainWindow->new();
351
352 MainLoop();
353
354 =item 6
355
356 @ARGV is processed by Tk::CmdLine before MainWindow creation.
357 Standard resource files are loaded in before MainWindow creation.
358 Individual resources are also loaded in before MainWindow creation.
359
360 use Tk;
361
362 Tk::CmdLine::SetArguments();
363
364 # <Process @ARGV - not worrying about X11-specific options>
365
366 Tk::CmdLine::LoadResources();
367
368 Tk::CmdLine::SetResources( # set a single resource
369 '*Button*background: red',
370 'widgetDefault' );
371
372 Tk::CmdLine::SetResources( # set multiple resources
373 [ '*Button*background: red', '*Button*foreground: blue' ],
374 'widgetDefault' );
375
376 my $mw = MainWindow->new();
377
378 MainLoop();
379
380 =back
381
382 =head1 ENVIRONMENT
383
384 =over 4
385
386 =item B<HOME> (optional)
387
388 Home directory which may contain user application defaults files as
389 $C<HOME>/$C<LANG>/E<lt>B<CLASS>E<gt> or $C<HOME>/E<lt>B<CLASS>E<gt>.
390
391 =item B<LANG> (optional)
392
393 The current language (default: I<C>).
394
395 =item B<XFILESEARCHPATH> (optional)
396
397 List of FileSpec patterns
398 (joined using the OS-dependent path delimiter, e.g. colon on B<UNIX>)
399 used in defining system application defaults files.
400
401 =item B<XUSERFILESEARCHPATH> (optional)
402
403 List of FileSpec patterns
404 (joined using the OS-dependent path delimiter, e.g. colon on B<UNIX>)
405 used in defining user application defaults files.
406
407 =item B<XAPPLRESDIR> (optional)
408
409 Directory containing user application defaults files as
410 $C<XAPPLRESDIR>/$C<LANG>/E<lt>B<CLASS>E<gt> or
411 $C<XAPPLRESDIR>/E<lt>B<CLASS>E<gt>.
412
413 =back
414
415 =head1 SEE ALSO
416
417 L<MainWindow|Tk::MainWindow>
418 L<option|Tk::option>
419
420 =head1 HISTORY
421
422 =over 4
423
424 =item *
425
426 1999.03.04 Ben Pavon E<lt>[email protected]<gt>
427
428 Rewritten as an object-oriented module.
429
430 Allow one to process command line options in a specified array (@ARGV by default).
431 Eliminate restrictions on the format and location of the options within the array
432 (previously the X11 options could not be specified in POSIX format and had to be
433 at the beginning of the array).
434
435 Added the C<SetResources>() and C<LoadResources>() functions to allow the definition
436 of resources prior to MainWindow creation.
437
438 =item *
439
440 2000.08.31 Ben Pavon E<lt>[email protected]<gt>
441
442 Added the C<GetArguments>() method which returns the list of arguments that
443 have been processed by C<SetArguments>().
444
445 Modified C<LoadResources>() to split the symbols using the OS-dependent
446 path delimiter defined in the B<Config> module.
447
448 Modified C<LoadResources>() to eliminate a warning message when processing
449 patterns B<%l>, B<%C>, B<%S>.
450
451 =back
452
453 =cut
454