|
NAMEGetopt::GUI::Long SYNOPSIS use Getopt::GUI::Long;
# pass useful config options to Configure
Getopt::GUI::Long::Configure(qw(display_help no_ignore_case capture_output));
GetOptions(\%opts,
["GUI:separator", "Important Flags:"],
["f|some-flag=s", "A flag based on a string"],
["o|other-flag", "A boloean"],
);
# or use references instead of a hash (less tested, however):
GetOptions(["some-flag=s", "perform some flag based on a value"] => \$flag,
["other-flag=s", "perform some flag based on a value"] => \$other);
# displays auto-help given the input above:
% opttest -h
Usage: opttest [OPTIONS] Other Arguments
OPTIONS:
Important Flags:
-f STRING A flag based on a string
-o A boloean
Help Options:
-h Display help options -- short flags preferred
--help Display help options -- long flags preferred
--help-full Display all help options -- short and long
# or long help:
% opttest --help
Usage: opttest [OPTIONS] Other Arguments
OPTIONS:
Important Flags:
--some-flag=STRING A flag based on a string
--other-flag A boloean
Help Options:
-h Display help options -- short flags preferred
--help Display help options -- long flags preferred
--help-full Display all help options -- short and long
# or a GUI screen:
(see http://www.dnssec-tools.org/images/getopt_example.png )
DESCRIPTION This module is a wrapper around Getopt::Long that extends the value of
the original Getopt::Long module to:
1) add a simple graphical user interface option screen if no
arguments are passed to the program. Thus, the arguments to
actually use are built based on the results of the user
interface. If arguments were passed to the program, the user
interface is not shown and the program executes as it normally
would and acts just as if Getopt::Long::GetOptions had been
called instead.
2) provide an auto-help mechanism such that -h and --help are
handled automatically. In fact, calling your program with -h
will default to showing the user a list of short-style arguments
when one exists for the option. Similarly --help will show the
user a list of long-style when possible. --help-full will list
all potential arguments for an option (short and long both).
It's designed to make the creation of graphical shells trivial
without the programmer having to think about it much as well as
providing automatic good-looking usage output without the
programmer needing to write usage() functions.
This also can turn normal command line programs into web CGI scripts
as well (automatically). If the Getopt::GUI::Long program is
installed as a CGI script then it will automatically prompt the user
for the same variables.
USAGEThe Getopt::GUI::Long module can work identically to the Getopt::Long module but really benefits from some slightly different usage conventions described below. Option format:Option strings passed should be formatted in one of the following ways:
[others TBD] Special Flag NamesFlags that start with GUI: are not passed to the normal Getopt::Long routines and are instead for internal GUI digestion only. If the GUI screen is going to be displayed (remember: only if the user didn't specify any options), these extra options control how the GUI behaves. Some of these options requires some knowledge of the QWizard programming system. Knowledge of QWizard should only be required if you want to make use of those particular extra features.
CONFIGURATIONIf you call Getopt::GUI::Long's Configure routine, it will accept a number of configure tokens and will pass the remaining ones to the Getopt::Long Configure routine. The tokens that it recognizes itself are described below:
Using the QWizard object for other purposesThe Getopt::GUI::Long qwizard object is stored at $Getopt::GUI::Long::GUI_qw, which is usable for other GUI screens you may need to create after the options screens have been processed. You can also use it during the script to optionally display a progress meter by making use of the QWizard::set_progress function. However, you should test to see if the GUI screen mode was actually used before operating with the object though. As an example: $Getopt::GUI::Long::GUI_qw->set_progress(3/5)
if ($Getopt::GUI::Long::GUI_qw);
PORTABILITYIf programs desire to not require this module, the following code snippit can be used instead which will not fail even if this module is not available. To be used this way, the LocalGetOptions and LocalOptionsMap functions should be copied to your perl script. LocalGetOptions(\%opts,
["h|help", "Show help for command line options"],
["some-flag=s", "perform some flag based on a value"]);
sub LocalGetOptions {
if (eval {require Getopt::GUI::Long;}) {
import Getopt::GUI::Long;
# optional configure call
Getopt::GUI::Long::Configure(qw(display_help no_ignore_case capture_output));
return GetOptions(@_);
}
require Getopt::Long;
import Getopt::Long;
# optional configure call
Getopt::Long::Configure(qw(auto_help no_ignore_case));
GetOptions(LocalOptionsMap(@_));
}
sub LocalOptionsMap {
my ($st, $cb, @opts) = ((ref($_[0]) eq 'HASH')
? (1, 1, $_[0]) : (0, 2));
for (my $i = $st; $i <= $#_; $i += $cb) {
if ($_[$i]) {
next if (ref($_[$i]) eq 'ARRAY' && $_[$i][0] =~ /^GUI:/);
push @opts, ((ref($_[$i]) eq 'ARRAY') ? $_[$i][0] : $_[$i]);
push @opts, $_[$i+1] if ($cb == 2);
}
}
return @opts;
}
Forcing a Particular GeneratorThere a times where you may want to force a particular generator to be used. This can be accomplished by setting the QWIZARD_GENERATOR environmental variable. this can actually be done in code within something like LocalGetOptions function as well: sub LocalGetOptions {
# force the library by using $ENV{'QWIZARD_GENERATOR'}
# if Gtk2, Tk, HTML or Readline generators are required, set the
# QWIZARD_GENERATOR ENV variable either externally or in this
# script Example: Force QWizard to use Tk instead of the
# preferred default of Gtk2 like this:
$ENV{'QWIZARD_GENERATOR'} = 'Tk' if (not exists($ENV{'QWIZARD_GENERATOR'}));
# ... conitune with the rest of the LocalGetOptions shown above
Usage as a CGI ScriptIf a Getopt::GUI::Long script is installed as a CGI script, then the Getopt::GUI::Long system will automatically create a web front end for the perl script. It will present the user with all the normal arguments that it would normally to a Gtk2 or other windowing system. It will not present the box for generic additional arguments since this is not safe to do. If you trust your users (ie, you have an authentication system in place) then you can set the allowcgiargs GUI variable to make this box appear. Example invocation (not generally recommended): ['GUI:allowcgiargs',1] It also allows you to not present certain options to web users that you will to command line users (some options may not be safe for CGI use). You can do this by setting the nocgi variable in option definitions you wish to disallow via CGI. E.G., if you had an option to specify a location where to load configuration a file from, this would likely be unsafe to publish in a CGI script. So remove it: ["c|config-file","Load a specific configuration file", nocgi => 1] EXAMPLESSee the getopttest program in the examples directory for an exmaple script that uses a lot of these features. AUTHORWes Hardaker, hardaker@users.sourceforge.net COPYRIGHT and LICENSECopyright (c) 2006-2009, SPARTA, Inc. All rights reserved Copyright (c) 2006-2007, Wes Hardaker. All rights reserved Getopt::GUI::Long is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SEE ALSOperl(1) modules: QWizard HISTORYThis module was originally named Getopt::Long::GUI but the Getopt::Long author wanted to reserve the Getopt::Long namespace entirely for himself and thus it's been recently renamed to Getopt::GUI::Long instead. The class tree isn't as clean this way, as this module still inherits from Getopt::Long but it everything still works of course.
|