16.20. Parsing Command-Line Options

In the Unix world, it is standard practice to specify command-line options when you are invoking an application, especially a graphical program. Starting your program as myscript -geometry "80x40" would not be unusual. To have Perl/Tk automatically parse and apply these command-line options for you, just call CmdLine immediately after you create your MainWindow.

$mw->CmdLine();

In Tk4, if you want to have CmdLine stop processing command-line arguments and leave some for you to deal with, add a double dash (--) before the arguments you want it to leave for you; for instance, myscript -geometry "80x40" -- -myopt.

In Tk8, the processing of options will stop when the first unknown option is found.

Another way to deal with command-line options is to use the Perl Getopts modules. Take a look in Programming Perl (O'Reilly, 1997) to find out how to use the methods available in Getopts. The methods inside Getopts don't handle the options for you; it just puts them in a structure that's easier to deal with.

Get Learning Perl/Tk now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.