| 1 | package Getopt::Long::GUI; | ||
| 2 | |||
| 3 | # Copyright 2005, Sparta, Inc. | ||
| 4 | # All rights reserved. | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use Text::Wrap; | ||
| 8 | use Getopt::Long; | ||
| 9 | use QWizard; | ||
| 10 | |||
| 11 | our $VERSION="0.1"; | ||
| 12 | |||
| 13 | require Exporter; | ||
| 14 | |||
| 15 | our @ISA = qw(Exporter Getopt::Long); | ||
| 16 | our @EXPORT = qw(GetOptions); | ||
| 17 | our $GUI_qw; | ||
| 18 | our $verbose; | ||
| 19 | our @ARGVsaved; | ||
| 20 | |||
| 21 | sub GetOptions(@) { | ||
| 22 | |||
| 23 | |||
| 24 | # If the user didn't specify any arguments, we display a GUI for them. | ||
| 25 | if ($#main::ARGV == -1) { | ||
| 26 | |||
| 27 | # they called us with no options, offer them a GUI screen full | ||
| 28 | my @names; | ||
| 29 | my %names; | ||
| 30 | my @qs; | ||
| 31 | my ($st, $cb, @opts) = ((ref($_[0]) eq 'HASH') ? | ||
| 32 | (1, 1, @_) : (0, 2, @_)); | ||
| 33 | my %GUI_info; | ||
| 34 | for (my $i = $st; $i <= $#opts; $i += $cb) { | ||
| 35 | my ($spec, $desc, %rest); | ||
| 36 | if (ref($opts[$i]) eq 'ARRAY') { | ||
| 37 | $spec = $opts[$i][0]; | ||
| 38 | $desc = $opts[$i][1]; | ||
| 39 | %rest = @{$opts[$i]}[2..$#{$opts[$i]}]; | ||
| 40 | } elsif ($opts[$i] eq '') { | ||
| 41 | push @qs,""; | ||
| 42 | next; | ||
| 43 | } else { | ||
| 44 | $spec = $opts[$i]; | ||
| 45 | } | ||
| 46 | |||
| 47 | if ($spec =~ /^GUI:(.*)/) { | ||
| 48 | my $guiname = $1; | ||
| 49 | if ($guiname =~ /guionly/) { | ||
| 50 | push @qs, @{$opts[$i]}[1..$#{$opts[$i]}]; | ||
| 51 | } elsif ($guiname =~ /separator/) { | ||
| 52 | push @qs, { type => 'label', text => $opts[$i][1]}; | ||
| 53 | } else { | ||
| 54 | @{$GUI_info{$guiname}} = @{$opts[$i]}[1..$#{$opts[$i]}]; | ||
| 55 | } | ||
| 56 | next; | ||
| 57 | } | ||
| 58 | |||
| 59 | # regexp stolen (and modified) from Getopt::Long | ||
| 60 | # XXX: copy copyright | ||
| 61 | $spec =~ m;^ | ||
| 62 | # $1 = whole name pattern | ||
| 63 | ( | ||
| 64 | # $2 = Option name | ||
| 65 | (\w+[-\w]* ) | ||
| 66 | # $3 = first name, or "?" | ||
| 67 | (?: \| (\? | \w[-\w]* )? )? | ||
| 68 | # Alias names, or "?" | ||
| 69 | (?: \| (?: \? | \w[-\w]* )? )* | ||
| 70 | )? | ||
| 71 | # $4 = arguments | ||
| 72 | ( | ||
| 73 | # Either modifiers ... | ||
| 74 | [!+] | ||
| 75 | | | ||
| 76 | # ... or a value/dest specification | ||
| 77 | [=:] [ionfs] [@%]? | ||
| 78 | | | ||
| 79 | # ... or an optional-with-default spec | ||
| 80 | : (?: -?\d+ | \+ ) [@%]? | ||
| 81 | )? | ||
| 82 | $;x; | ||
| 83 | |||
| 84 | # map option pieces into variables | ||
| 85 | my ($pat, $optname, $firstname, $args) = ($1, $2, $3, $4); | ||
| 86 | |||
| 87 | |||
| 88 | # calculate the option name to use from the list of names. | ||
| 89 | my $name = (length($optname) == 1 ? | ||
| 90 | ((length($firstname) > 1) ? $firstname : $optname) : | ||
| 91 | $optname); | ||
| 92 | |||
| 93 | # if no description, we copy the name and use that. | ||
| 94 | $desc = $name if (!$desc); | ||
| 95 | |||
| 96 | # remember the name of the option we're working on | ||
| 97 | push @names,$name; | ||
| 98 | $names{$name} = $args; | ||
| 99 | |||
| 100 | # calculate a default value based on the passed in hash | ||
| 101 | # ref or variable reference. | ||
| 102 | my $defval = (($cb == 1) ? | ||
| 103 | (exists($_[0]{$optname}) ? $_[0]{$optname} : undef) : | ||
| 104 | $opts[$i+1]); | ||
| 105 | |||
| 106 | # Now constructure the needed GUI question | ||
| 107 | if ($rest{'question'}) { | ||
| 108 | # if a QWizard question definition was passed to us, use that. | ||
| 109 | if (ref($rest{'question'}) ne 'ARRAY') { | ||
| 110 | $rest{'question'} = [$rest{'question'}]; | ||
| 111 | } | ||
| 112 | map { | ||
| 113 | $_->{'name'} = $name if ($_->{'name'} eq ''); | ||
| 114 | $_->{'text'} = $desc if ($_->{'text'} eq ''); | ||
| 115 | $_->{'default'} = $defval if ($_->{'default'} eq ''); | ||
| 116 | } @{$rest{'question'}}; | ||
| 117 | push @qs, @{$rest{'question'}}; | ||
| 118 | |||
| 119 | } elsif ($desc =~ /^!GUI/) { | ||
| 120 | # if the description was marked as not-for-the-GUI, don't | ||
| 121 | # dispaly aynthing and skip it but remember the parameter name. | ||
| 122 | push @qs, {type => 'hidden', name => $name, default => $defval}; | ||
| 123 | |||
| 124 | } elsif (!$args || $args =~ /[!+]/) { | ||
| 125 | # The option was a boolean flag: use a checkbox | ||
| 126 | push @qs, | ||
| 127 | { | ||
| 128 | 'text' => $desc, | ||
| 129 | 'type' => 'checkbox', | ||
| 130 | values => [1, undef], | ||
| 131 | default => (($defval)?1:undef), | ||
| 132 | 'name' => $name, | ||
| 133 | }; | ||
| 134 | } elsif ($args =~ /i/) { | ||
| 135 | # The option was an integer, use a entry with a forced | ||
| 136 | # integer type check | ||
| 137 | push @qs, | ||
| 138 | { | ||
| 139 | 'text' => $desc, | ||
| 140 | 'type' => 'text', | ||
| 141 | default => $defval, | ||
| 142 | 'name' => $name, | ||
| 143 | check_value => (($rest{'required'}) ? | ||
| 144 | \&qw_integer : \&qw_optional_integer), | ||
| 145 | }; | ||
| 146 | } elsif ($args =~ /[sf]/) { | ||
| 147 | # The option was a string or float, allow anything. | ||
| 148 | # XXX: deal with floats seperately | ||
| 149 | push @qs, | ||
| 150 | { | ||
| 151 | 'text' => $desc, | ||
| 152 | 'type' => 'text', | ||
| 153 | default => $defval, | ||
| 154 | 'name' => $name, | ||
| 155 | }; | ||
| 156 | } else { | ||
| 157 | # In theory we probably shouldn't get here. But fake | ||
| 158 | # it if we do. | ||
| 159 | push @qs, | ||
| 160 | { | ||
| 161 | 'text' => $desc, | ||
| 162 | 'type' => 'text', | ||
| 163 | default => $defval, | ||
| 164 | 'name' => $name, | ||
| 165 | }; | ||
| 166 | } | ||
| 167 | |||
| 168 | # if the required flag was set, force a requirement check. | ||
| 169 | if ($rest{'required'} && !exists($qs[$#qs]{'check_answer'})) { | ||
| 170 | $qs[$#qs]{'check_value'} = \&qw_required_field; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | # Prompt for remaining arguments (or don't if requested to | ||
| 175 | # skip it). Normally this would be stuff handled beyond the | ||
| 176 | # realm of the Getopt processing like file-names, etc. | ||
| 177 | if ($GUI_info{'nootherargs'}) { | ||
| 178 | push @qs, "", { type => 'hidden', name => '__otherargs' }; | ||
| 179 | } else { | ||
| 180 | push @qs, "", { type => 'text', | ||
| 181 | width => 80, | ||
| 182 | name => '__otherargs', | ||
| 183 | text => "Other Arguments" }; | ||
| 184 | } | ||
| 185 | |||
| 186 | # construct the QWizard GUI primaries from the above information | ||
| 187 | my $pris; | ||
| 188 | |||
| 189 | # include other primary information passed to us | ||
| 190 | if (exists($GUI_info{'otherprimaries'})) { | ||
| 191 | %$pris = @{$GUI_info{'otherprimaries'}}; | ||
| 192 | } | ||
| 193 | |||
| 194 | # our master primary | ||
| 195 | $pris->{'getopts'} = | ||
| 196 | { | ||
| 197 | questions => \@qs, | ||
| 198 | title => "Select options for $main::0", | ||
| 199 | actions => [[sub { | ||
| 200 | my $qw = shift; | ||
| 201 | for (my $i = 0; $i <= $#{$_[0]}; $i++) { | ||
| 202 | if (qwparam($_[0][$i])) { | ||
| 203 | if (!$_[1]{$_[0][$i]} || | ||
| 204 | $_[1]{$_[0][$i]} =~ /[+!]/) { | ||
| 205 | push @main::ARGV, | ||
| 206 | "--" . $_[0][$i]; | ||
| 207 | } else { | ||
| 208 | push @main::ARGV, | ||
| 209 | "--" . $_[0][$i], | ||
| 210 | qwparam($_[0][$i]); | ||
| 211 | } | ||
| 212 | } | ||
| 213 | } | ||
| 214 | push @main::ARGV, | ||
| 215 | split(/\s+/,qwparam('__otherargs')); | ||
| 216 | return 'OK'; | ||
| 217 | }, \@names, \%names]], | ||
| 218 | }; | ||
| 219 | |||
| 220 | # add in an post_answers clauses to the master primary | ||
| 221 | if ($GUI_info{'post_answers'}) { | ||
| 222 | @{$pris->{'getopts'}{'post_answers'}} = | ||
| 223 | @{$GUI_info{'post_answers'}}; | ||
| 224 | } | ||
| 225 | |||
| 226 | # add in an actions clauses to the master primary | ||
| 227 | if ($GUI_info{'actions'}) { | ||
| 228 | unshift @{$pris->{'getopts'}{'actions'}}, @{$GUI_info{'actions'}}; | ||
| 229 | } | ||
| 230 | |||
| 231 | # Finally, construct the QWizard GUI class... | ||
| 232 | my $qw = new QWizard(primaries => $pris, | ||
| 233 | no_confirm => 1, | ||
| 234 | title => $main::0); | ||
| 235 | |||
| 236 | |||
| 237 | # ... remember it ... | ||
| 238 | $GUI_qw = $qw; | ||
| 239 | |||
| 240 | # ... and tell it to go | ||
| 241 | $qw->magic('getopts', @{$GUI_info{'submodules'}}); | ||
| 242 | |||
| 243 | |||
| 244 | # ... if we aren't finished processing then exit. This should | ||
| 245 | # only happen if we're in a CGI script where we're not done yet. | ||
| 246 | if (($qw->{'state'} != $QWizard::states{'ACTING'} && | ||
| 247 | $qw->{'state'} != $QWizard::states{'FINISHED'}) || | ||
| 248 | $qw->{'state'} == $QWizard::states{'CANCELED'}) { | ||
| 249 | # we're not done or have been cancelled! exit! | ||
| 250 | exit; | ||
| 251 | } | ||
| 252 | |||
| 253 | |||
| 254 | # if there are any final hook routines to call, do so. | ||
| 255 | if ($GUI_info{'hook_finished'}) { | ||
| 256 | foreach my $h (@{$GUI_info{'hook_finished'}}) { | ||
| 257 | $h->(); | ||
| 258 | } | ||
| 259 | } | ||
| 260 | } | ||
| 261 | |||
| 262 | # map the options passed to us to ones that are compliant with | ||
| 263 | # Getopt::Long | ||
| 264 | my @opts = MapToGetoptLong(@_); | ||
| 265 | |||
| 266 | # save the arguments we contstructed for future use. | ||
| 267 | @ARGVsaved = @main::ARGV; | ||
| 268 | |||
| 269 | # display the results to the user if verbose was requested. | ||
| 270 | if ($verbose) { | ||
| 271 | print STDERR "passing $#main::ARGV args to Getopt::Long: ", | ||
| 272 | join(",",@main::ARGV),"\n"; | ||
| 273 | } | ||
| 274 | |||
| 275 | # finally, pass on to the original Getopt::Long routine. | ||
| 276 | return Getopt::Long::GetOptions(@opts); | ||
| 277 | } | ||
| 278 | |||
| 279 | sub MapToGetoptLong { | ||
| 280 | my ($st, $cb, @opts) = ((ref($_[0]) eq 'HASH') | ||
| 281 | ? (1, 1, $_[0]) : (0, 2)); | ||
| 282 | for (my $i = $st; $i <= $#_; $i += $cb) { | ||
| 283 | if ($_[$i]) { | ||
| 284 | next if (ref($_[$i]) eq 'ARRAY' && $_[$i][0] =~ /^GUI:/); | ||
| 285 | push @opts, ((ref($_[$i]) eq 'ARRAY') ? $_[$i][0] : $_[$i]); | ||
| 286 | push @opts, $_[$i+1] if ($cb == 2); | ||
| 287 | } | ||
| 288 | } | ||
| 289 | return @opts; | ||
| 290 | } | ||
| 291 | |||
| 292 | =pod | ||
| 293 | |||
| 294 | =head1 NAME | ||
| 295 | |||
| 296 | Getopt::Long::GUI | ||
| 297 | |||
| 298 | =head1 SYNOPSIS | ||
| 299 | |||
| 300 | use Getopt::Long::GUI; | ||
| 301 | |||
| 302 | GetOptions(\%opts, | ||
| 303 | ["h|help", "Show help for command line options"], | ||
| 304 | ["some-flag=s", "perform some flag based on a value"]); | ||
| 305 | |||
| 306 | # or | ||
| 307 | |||
| 308 | GetOptions(["h|help", "Show help for command line options"] => \$help, | ||
| 309 | ["some-flag=s", "perform some flag based on a value"] => \$flag); | ||
| 310 | |||
| 311 | |||
| 312 | =head1 DESCRIPTION | ||
| 313 | |||
| 314 | This module is a wrapper around Getopt::Long that extends the value of | ||
| 315 | the originial Getopt::Long module to add a simple graphical user | ||
| 316 | interface option screen if no arguments are passed to the program. | ||
| 317 | Thus, the arguments to actually use are built based on the results of | ||
| 318 | the user interface. If arguments were passed to the program, the user | ||
| 319 | interface is not shown and the program executes as it normally would | ||
| 320 | and acts just as if Getopt::Long::GetOptions had been called instead. | ||
| 321 | |||
| 322 | =head1 USAGE | ||
| 323 | |||
| 324 | The Getopt::Long::GUI module works identically to the Getopt::Long | ||
| 325 | module, but does offer a few extra features. | ||
| 326 | |||
| 327 | =head2 Option format: | ||
| 328 | |||
| 329 | Option strings passed should be formatted in one of the following ways: | ||
| 330 | |||
| 331 | =over | ||
| 332 | |||
| 333 | =item Empty String | ||
| 334 | |||
| 335 | Empty strings are ignored by the non-GUI version of the command, but | ||
| 336 | are treated as vertical separators between questions when displaying | ||
| 337 | the GUI screen. | ||
| 338 | |||
| 339 | =item Standard flag specification string | ||
| 340 | |||
| 341 | EG: "some-flag|optional-flag=s" | ||
| 342 | |||
| 343 | This is the standard method by which Getopt::Long does things and is | ||
| 344 | merely treated the same way here. In this case, the text presented to | ||
| 345 | the user screen will be the first name in the list ("some-flag") in | ||
| 346 | the above option. The type of wdget displayed with the text will | ||
| 347 | depend on the optional =s/i/whatever flag and will be either a | ||
| 348 | checkbox, entry box, ... | ||
| 349 | |||
| 350 | =item Array Reference | ||
| 351 | |||
| 352 | EG: ["some-flag|optional-flag=s", 'Prompt text', OTHER], | ||
| 353 | |||
| 354 | The values passed in this array are as follows: | ||
| 355 | |||
| 356 | =over | ||
| 357 | |||
| 358 | =item 0: Standard flag specification string | ||
| 359 | |||
| 360 | Same as always, and as above. | ||
| 361 | |||
| 362 | =item 1: Prompt text about flag | ||
| 363 | |||
| 364 | The help text that should be shown to the user in the graphical | ||
| 365 | interface. In the example above rather than "some-flag" being shown, | ||
| 366 | "Prompt text" will be shown next to the widget instead. | ||
| 367 | |||
| 368 | If the prompt text is equal to "!GUI" then this option will not be | ||
| 369 | displayed (automatically at least) within the GUI. | ||
| 370 | |||
| 371 | =item 2...: OTHER options | ||
| 372 | |||
| 373 | =over | ||
| 374 | |||
| 375 | =item required => 1 | ||
| 376 | |||
| 377 | Forces a screen option to be filled out by the user. | ||
| 378 | |||
| 379 | =back | ||
| 380 | |||
| 381 | [others TBD] | ||
| 382 | |||
| 383 | =back | ||
| 384 | |||
| 385 | =back | ||
| 386 | |||
| 387 | =head1 Special Flag Names | ||
| 388 | |||
| 389 | Flags that start with GUI: are not passed to the normal Getopt::Long | ||
| 390 | routines and are instead for internal GUI digestion only. If the GUI | ||
| 391 | screen is going to be displayed (remember: the user didn't specify any | ||
| 392 | options), these extra options control how the GUI behaves. | ||
| 393 | |||
| 394 | =over | ||
| 395 | |||
| 396 | =item GUI:guionly | ||
| 397 | |||
| 398 | EG: ['GUI:guionly', { type => 'checkbox', name => 'myguiflag'}] | ||
| 399 | |||
| 400 | Specifies a valid QWizard question(s) to only be shown when the gui is | ||
| 401 | displayed, and the specification is ignored during normal command line | ||
| 402 | usage. | ||
| 403 | |||
| 404 | =item GUI:separator | ||
| 405 | |||
| 406 | EG: ['GUI:separator', 'Task specific options:'] | ||
| 407 | |||
| 408 | Inserts a label above a set of options to identify them as a group. | ||
| 409 | |||
| 410 | =item GUI:nootherargs | ||
| 411 | |||
| 412 | EG: ['GUI:nootherargs', 1] | ||
| 413 | |||
| 414 | Normally the GUI screen shows a "Other Arguments:" option at the | ||
| 415 | bottom of the main GUI screen to allow users to entry additional flags | ||
| 416 | (needed for file names, etc, and other non-option arguments to be | ||
| 417 | passed). If you're going to handle the additional arguments yourself | ||
| 418 | in some way (using either some GUI:guionly or (GUI:otherprimaries and | ||
| 419 | GUI:submodules) flags), then you should specify this so the other | ||
| 420 | arguments field is not shown. You're expected, in your self-handling | ||
| 421 | code, to set the __otherargs QWizard parameter to the final arguments | ||
| 422 | that should be passed on. | ||
| 423 | |||
| 424 | =item GUI:otherprimaries | ||
| 425 | |||
| 426 | EG: ['GUI:otherprimaries', primaryname => | ||
| 427 | { title => '...', questions => [...] }] | ||
| 428 | |||
| 429 | Defines other primaries to be added to the QWizard primary set. | ||
| 430 | |||
| 431 | =item GUI:submodules | ||
| 432 | |||
| 433 | EG: ['GUI:submodules', 'primaryname'] | ||
| 434 | |||
| 435 | Defines a list of other primaries that should be called after the | ||
| 436 | initial one. | ||
| 437 | |||
| 438 | =item GUI:post_answers | ||
| 439 | |||
| 440 | EG: ['GUI:post_actions', sub { do_something(); }] | ||
| 441 | |||
| 442 | Defines an option for QWizard post_answers subroutines to run. | ||
| 443 | |||
| 444 | =item GUI:actions | ||
| 445 | |||
| 446 | EG: ['GUI:post_actions', sub { do_something(); }] | ||
| 447 | |||
| 448 | Defines an option for QWizard actions subroutines to run. | ||
| 449 | |||
| 450 | =item GUI:actions | ||
| 451 | |||
| 452 | EG: ['GUI:hook_finished', sub { do_something(); }] | ||
| 453 | |||
| 454 | Defines subroutine(s) to be called after the GUI has completely finished. | ||
| 455 | |||
| 456 | =item GUI: | ||
| 457 | |||
| 458 | =back | ||
| 459 | |||
| 460 | =head1 PORTABILITY | ||
| 461 | |||
| 462 | If programs desire to not require this module, the following code | ||
| 463 | snippit can be used instead which will not fail even if this module is | ||
| 464 | not available. To be used this way, the LocalGetOptions and | ||
| 465 | LocalOptionsMap functions should be copied to your perl script. | ||
| 466 | |||
| 467 | LocalGetOptions(\%opts, | ||
| 468 | ["h|help", "Show help for command line options"], | ||
| 469 | ["some-flag=s", "perform some flag based on a value"]); | ||
| 470 | |||
| 471 | sub LocalGetOptions { | ||
| 472 | if ($#ARGV == -1 && eval {require Getopt::Long::GUI;}) { | ||
| 473 | import Getopt::Long::GUI; | ||
| 474 | return GetOptions(@_); | ||
| 475 | } else { | ||
| 476 | require Getopt::Long; | ||
| 477 | import Getopt::Long; | ||
| 478 | } | ||
| 479 | GetOptions(LocalOptionsMap(@_)); | ||
| 480 | } | ||
| 481 | |||
| 482 | sub LocalOptionsMap { | ||
| 483 | my ($st, $cb, @opts) = ((ref($_[0]) eq 'HASH') | ||
| 484 | ? (1, 1, $_[0]) : (0, 2)); | ||
| 485 | for (my $i = $st; $i <= $#_; $i += $cb) { | ||
| 486 | if ($_[$i]) { | ||
| 487 | next if (ref($_[$i]) eq 'ARRAY' && $_[$i][0] =~ /^GUI:/); | ||
| 488 | push @opts, ((ref($_[$i]) eq 'ARRAY') ? $_[$i][0] : $_[$i]); | ||
| 489 | push @opts, $_[$i+1] if ($cb == 2); | ||
| 490 | } | ||
| 491 | } | ||
| 492 | return @opts; | ||
| 493 | } | ||
| 494 | |||
| 495 | =cut | ||
| 496 | |||
| 497 | 1; |