CmdLine(3pm)

πŸ’– Tk::CmdLine β€” the manual, but make it make sense πŸ’–

okay bestie, sit down, grab a boba, because we're about to talk about Tk::CmdLine β€” the perl/Tk module that processes standard X11 command line options and sets your initial resources. it's giving "backend plumbing" but honestly it's so iconic once you get it. ✨


🌸 SYNOPSIS 🌸

Tk::CmdLine::SetArguments([@argument]);

my $value = Tk::CmdLine::cget([$option]);

Tk::CmdLine::SetResources((\@resource | $resource) [, $priority]);

Tk::CmdLine::LoadResources(
    [ -symbol   => $symbol     ]
    [ -file     => $fileSpec   ]
    [ -priority => $priority   ]
    [ -echo     => $fileHandle ] );

πŸ¦‹ DESCRIPTION πŸ¦‹

so this module's whole job is: process standard X11 command line options and set initial resources. that's the vibe. πŸ’…

the X11R5 man page for X11 literally says β€” and I quote β€” "Most X programs attempt to use the same names for command line options and arguments. All applications written with the X Toolkit Intrinsics automatically accept the following options: ...". so yeah, everyone's doing the same thing, and this module handles all those command line options for perl/Tk applications using the SetArguments() function. no cap. 🫢

you can optionally also use this module to load initial resources explicitly via the SetResources() function, or from specified files (default: the standard X11 application-specific resource files) via LoadResources(). lowkey powerful.

πŸŽ€ Command Line Options πŸŽ€

here's the whole roster, no skips, no cuts:

  • -background Color | -bg Color β€” picks the color used for the window background. cute. πŸ’œ
  • -class Class β€” says which class resources for the application should be found under. it's super useful in shell aliases so you can tell different invocations of an app apart without going to the trouble of making links just to change the executable file name. periodt.
  • -display Display | -screen Display β€” names the X server to be used.
  • -font Font | -fn Font β€” the font used for displaying text.
  • -foreground Color | -fg Color β€” the color used for text or graphics. πŸ’…
  • -geometry Geometry β€” the initial size and location of the first MainWindow.
  • -iconic β€” says the user would prefer the application's windows to start out not visible, like they'd been immediately iconified by the user. > heads up: window managers may choose not to honor the application's request. rude but true. 😭
  • -motif β€” says the app should stick as closely as possible to Motif look-and-feel standards. for example, active elements like buttons and scrollbar sliders will not change color when the pointer passes over them. very demure, very mindful.
  • -name Name β€” specifies the name under which resources for the application should be found. again, super handy in shell aliases to distinguish invocations without creating links to alter the executable file name.
  • -synchronous β€” says requests to the X server should be sent synchronously instead of asynchronously. since Xlib normally buffers requests to the server, errors do do not necessarily get reported immediately after they occur. this option turns off the buffering so the application can be debugged. > it should never be used with a working program. literally never. 🚨
  • -title TitleString β€” the title for this window. window managers sometimes use this to give you some sort of header identifying the window.
  • -xrm ResourceString β€” specifies a resource pattern and value to override any defaults. it's also super useful for setting resources that don't have explicit command line arguments.

the ResourceString is of the form <pattern>:<value>, that is (the first) ':' is used to determine which part is pattern and which part is value. the (<pattern>, <value>) pair gets entered into the options database with optionAdd (for each MainWindow configured), with interactive priority. iykyk. ✨

🌈 Initial Resources 🌈

there are several mechanisms for initializing the resource database that an X11 application uses. resources may be defined in a $"HOME"/.Xdefaults file, a system application defaults file (e.g. /usr/lib/X11/app-defaults/<CLASS>), or a user application defaults file (e.g. $"HOME"/<CLASS>). the Tk::CmdLine functionality for setting initial resources concerns itself with the latter two. ok so basically the last two. got it bestie. πŸŽ€

resource files contain data lines of the form <pattern>:<value>. they may also contain blank lines and comment lines (denoted by a ! character as the first non-blank character). refer to option for a description of <pattern>:<value>.

🧚 System Application Defaults Files 🧚

system application defaults files may be specified via environment variable $"XFILESEARCHPATH" which, if set, contains a list of file patterns (joined using the OS-dependent path delimiter, e.g. colon on UNIX).

🌸 User Application Defaults Files 🌸

user application defaults files may be specified via environment variables $"XUSERFILESEARCHPATH", $"XAPPLRESDIR" or $"HOME".


πŸ‘‘ METHODS πŸ‘‘

SetArguments β€” Tk::CmdLine::SetArguments([@argument])

extracts the X11 options contained in a specified array (@ARGV by default).

Tk::CmdLine::SetArguments([@argument])

the X11 options may be specified using a single dash - as per the X11 convention, or using two dashes -- as per the POSIX standard (e.g. -geometry 100x100, -geometry 100x100 or -geometry=100x100). the options may be interspersed with other options or arguments. a -- by itself terminates option processing. so iconic of it. πŸ’…

by default, command line options are extracted from @ARGV the first time a MainWindow is created. the Tk::MainWindow constructor indirectly invokes SetArguments() to do this.

GetArguments β€” Tk::CmdLine::GetArguments()

gets a list of the X11 options that have been processed by SetArguments(). (GetArguments() first invokes SetArguments() if it has not already been invoked.) no duplicating work, slay. ✨

cget β€” Tk::CmdLine::cget([$option])

gets the value of a configuration option specified via SetArguments(). (cget() first invokes SetArguments() if it has not already been invoked.)

Tk::CmdLine::cget([$option])

the valid options are: -class, -name, -screen and -title. if no option is specified, -class is implied. πŸ’–

a typical use of cget() might be to obtain the application class in order to define the name of a resource file to be loaded in via LoadResources().

my $class = Tk::CmdLine::cget(); # process command line and return class

SetResources β€” Tk::CmdLine::SetResources((\@resource | $resource) [, $priority])

sets the initial resources.

Tk::CmdLine::SetResources((\@resource | $resource) [, $priority])

a single resource may be specified using a string of the form '<pattern>:<value>'. multiple resources may be specified by passing an array reference whose elements are either strings of the above form, and/or anonymous arrays of the form [ <pattern>, <value> ]. the optional second argument specifies the priority, as defined in option, to be associated with the resources (default: userDefault).

note that SetResources() first invokes SetArguments() if it has not already been invoked. it's very "I got you bestie" of it. 🫢

LoadResources β€” Tk::CmdLine::LoadResources([%options])

loads initial resources from one or more files.

Tk::CmdLine::LoadResources(
    [ -symbol   => $symbol     ]
    [ -file     => $fileSpec   ]
    [ -priority => $priority   ]
    [ -echo     => $fileHandle ] );
  • [ -symbol => $symbol ] β€” specifies the name of an environment variable that, if set, defines a list of one or more directories and/or file patterns (joined using the OS-dependent path delimiter, e.g. colon on UNIX). $"XUSERFILESEARCHPATH" is a special case. if $"XUSERFILESEARCHPATH" is not set, $"XAPPLRESDIR" is checked instead. if $"XAPPLRESDIR" is not set, $"HOME" is checked instead. it's giving backup plan energy. 😌

an item is identified as a file pattern if it contains one or more /%[A-Za-z]/ patterns. only patterns %L, %T and %N are currently recognized. all others are replaced with the null string. pattern %L is translated into $"LANG". pattern %T is translated into app-defaults. pattern %N is translated into the application class name. 🌈

each file pattern, after substitutions are applied, is assumed to define a FileSpec to be examined.

when a directory is specified, FileSpecs <DIRECTORY>/<LANG>/<CLASS> and <DIRECTORY>/<CLASS> are defined, in that order.

  • [ -file => $fileSpec ] β€” specifies a resource file to be loaded in. the file is silently skipped if if does not exist, or if it is not readable. (yes, "if if". we keep it real here. πŸ’€)
  • [ -priority => $priority ] β€” specifies the priority, as defined in option, to be associated with the resources (default: userDefault).
  • [ -echo => $fileHandle ] β€” may be used to specify that a line should be printed to the corresponding FileHandle (default: \*STDOUT) everytime a file is examined / loaded. lil narrator moment. πŸ‘‘

if no -symbol or -file options are specified, LoadResources() processes symbol $"XFILESEARCHPATH" with priority startupFile and $"XUSERFILESEARCHPATH" with priority userDefault. (note that $"XFILESEARCHPATH" and $"XUSERFILESEARCHPATH" are supposed to contain only patterns. $"XAPPLRESDIR" and $"HOME" are supposed to be a single directory. LoadResources() does not check/care whether this is the case. it's unbothered, moisturized, in its own lane. πŸ’…)

for each set of FileSpecs, LoadResources() examines each FileSpec to determine if the file exists and is readable. the first file that meets this criteria is read in and SetResources() is invoked. first come, first served, no cap. πŸ”₯

note that LoadResources() first invokes SetArguments() if it has not already been invoked.

⭐ NOTES ⭐

this module is an object-oriented module whose methods can be invoked as object methods, class methods or regular functions. this is accomplished via an internally-maintained object reference which is created as necessary, and which always points to the last object used. main character reference energy, honestly. πŸ‘‘

SetArguments(), SetResources() and LoadResources() return the object reference. πŸŽ€

✨ EXAMPLES ✨

Bestie, these are the receipts. Every single one is a real, working setup, so pick the vibe that matches what you're building. πŸ’…


1. @ARGV</p> gets processed by Tk::CmdLine right when MainWindow is created. Set it and forget it, honestly.

use Tk;

# <Process @ARGV - ignoring all X11-specific options>

my $mw = MainWindow->new();

MainLoop();

2. @ARGV</p> gets processed by Tk::CmdLine before MainWindow is created. So if your @ARGV</p> is (--geometry=100x100 -opt1 a b c -bg red), then after SetArguments() runs it's equal to (-opt1 a b c). The X11 stuff got whisked away, no cap. 🧚

use Tk;

Tk::CmdLine::SetArguments(); # Tk::CmdLine->SetArguments() works too

# <Process @ARGV - not worrying about X11-specific options>

my $mw = MainWindow->new();

MainLoop();

3. Same energy as 2), except the default arguments get loaded in first. Iconic little pre-game. πŸ‘‘

use Tk;

Tk::CmdLine::SetArguments(qw(-name test -iconic));
Tk::CmdLine::SetArguments();

# <Process @ARGV - not worrying about X11-specific options>

my $mw = MainWindow->new();

MainLoop();

4. @ARGV</p> gets processed by Tk::CmdLine before MainWindow creation, and the standard resource files get loaded in before MainWindow creation too. Two birds, one slay. πŸ¦‹

use Tk;

Tk::CmdLine::SetArguments();

# <Process @ARGV - not worrying about X11-specific options>

Tk::CmdLine::LoadResources();

my $mw = MainWindow->new();

MainLoop();

5. Same as 4, but now the standard resource files load in with non-default priorities. We're customizing, we're picky, we're thriving. ✨

use Tk;

Tk::CmdLine::SetArguments();

# <Process @ARGV - not worrying about X11-specific options>

Tk::CmdLine::LoadResources(-echo => \*STDOUT,
    -priority => 65, -symbol => 'XFILESEARCHPATH' );
Tk::CmdLine::LoadResources(-echo => \*STDOUT,
    -priority => 75, -symbol => 'XUSERFILESEARCHPATH' );

my $mw = MainWindow->new();

MainLoop();

6. @ARGV</p> gets processed before MainWindow creation, standard resource files load in before MainWindow creation, and individual resources load in before MainWindow creation. Full stack of prep, and honestly? We love a thorough queen. πŸ’œ

use Tk;

Tk::CmdLine::SetArguments();

# <Process @ARGV - not worrying about X11-specific options>

Tk::CmdLine::LoadResources();

Tk::CmdLine::SetResources( # set a single resource
    '*Button*background: red',
    'widgetDefault' );

Tk::CmdLine::SetResources( # set multiple resources
    [ '*Button*background: red', '*Button*foreground: blue' ],
    'widgetDefault' );

my $mw = MainWindow->new();

MainLoop();

🌈 ENVIRONMENT 🌈

These are the little environment variables that change the whole mood of how your defaults files get found. All of them are optional, so like, no pressure bestie. 🫢

HOME (optional)

The home directory, which may contain user application defaults files as $"HOME"/$"LANG"/<CLASS> or $"HOME"/<CLASS>.

LANG (optional)

The current language. Default is C. (Yes, just C. It's giving minimal. πŸ’€)

XFILESEARCHPATH (optional)

A list of FileSpec patterns β€” joined using the OS-dependent path delimiter, e.g. colon on UNIX β€” used in defining system application defaults files.

XUSERFILESEARCHPATH (optional)

A list of FileSpec patterns β€” joined using the OS-dependent path delimiter, e.g. colon on UNIX β€” used in defining user application defaults files.

XAPPLRESDIR (optional)

A directory containing user application defaults files as $"XAPPLRESDIR"/$"LANG"/<CLASS> or $"XAPPLRESDIR"/<CLASS>.


πŸ‘‘ SEE ALSO πŸ‘‘

  • MainWindow option β€” go read that one next, it's the sister page fr fr. πŸ”₯

πŸ’– HISTORY πŸ’–

The lore. The origin story. Sit down, we're doing a deep dive. πŸ“

  • 1999.03.04 β€” Ben Pavon <ben.pavon@hsc.hac.com>
    • Rewritten as an object-oriented module.
    • Allow one to process command line options in a specified array (@ARGV by default).
    • Eliminate restrictions on the format and location of the options within the array β€” previously the X11 options could not be specified in POSIX format and had to be at the beginning of the array. Not anymore bestie, we freed her. πŸ•ŠοΈ
    • Added the SetResources() and LoadResources() functions to allow the definition of resources prior to MainWindow creation.
  • 2000.08.31 β€” Ben Pavon <ben.pavon@hsc.hac.com>
    • Added the GetArguments() method, which returns the list of arguments that have been processed by SetArguments().
    • Modified LoadResources() to split the symbols using the OS-dependent path delimiter defined in the Config module.
    • Modified LoadResources() to eliminate a warning message when processing patterns %l, %C, %S.

And that's the whole page, bestie!! 🌸 This was CmdLine(3pm) from perl v5.40.1, dated 2025-04-13 β€” yeah, she's a vintage icon and she still eats. πŸ’…βœ¨ Go forth and process those arguments, you absolute main character. πŸŽ€πŸ¦‹πŸ’–

πŸ“¦ where this page comes from