File Coverage

blib/lib/Getopt/App.pm
Criterion Covered Total %
statement 202 210 96.1
branch 89 112 79.4
condition 33 45 73.3
subroutine 29 29 100.0
pod 10 10 100.0
total 363 406 89.4


line stmt bran cond sub pod time code
1             package Getopt::App;
2 7     7   1570979 use feature qw(:5.16);
  7         19  
  7         1469  
3 7     7   74 use strict;
  7         15  
  7         222  
4 7     7   37 use warnings;
  7         12  
  7         378  
5 7     7   90 use utf8;
  7         25  
  7         40  
6              
7 7     7   285 use Carp qw(croak);
  7         15  
  7         516  
8 7     7   5614 use Getopt::Long ();
  7         105322  
  7         379  
9 7     7   66 use List::Util qw(first);
  7         28  
  7         714  
10              
11 7   50 7   63 use constant DEBUG => $ENV{GETOPT_APP_DEBUG} || 0;
  7         14  
  7         21606  
12              
13             our $VERSION = '1.01';
14              
15             our ($DEPTH, $OPT_COMMENT_RE, $OPTIONS, $SUBCOMMAND, $SUBCOMMANDS, %APPS) = (-1, qr{\s+\#\s+});
16              
17             our $call_maybe = sub {
18             my ($app, $m) = (shift, shift);
19             my $pkg = !DEBUG ? '' : $app->can($m) ? $app : __PACKAGE__->can($m) ? __PACKAGE__ : 'SKIP';
20             warn sprintf "[getopt::app] %s::%s()\n", $pkg, $m if DEBUG;
21             $m = $app->can($m) || __PACKAGE__->can($m);
22             return $m ? $app->$m(@_) : undef;
23             };
24              
25             sub bundle {
26 1     1 1 276720 my ($class, $script, $OUT) = (@_, \*STDOUT);
27 1         3 my ($package, @script);
28              
29 1 50       59 open my $SCRIPT, '<', $script or croak "Can't read $script: $!";
30 1         58 while (my $line = readline $SCRIPT) {
31 2 50       22 if ($line =~ /\bDEBUG\b/) {
    100          
    50          
32 0         0 next;
33             }
34             elsif ($line =~ m!^\s*package\s+\S+\s*;!) { # look for app class name
35 1         4 $package .= $line;
36 1         4 last;
37             }
38             elsif ($. == 1) { # look for hashbang
39 1 50       5 $line =~ m/^#!/ ? print {$OUT} $line : do { print {$OUT} "#!$^X\n"; push @script, $line };
  1         9  
  0         0  
  0         0  
  0         0  
40             }
41             else {
42 0         0 push @script, $line;
43 0 0       0 last if $line =~ m!^[^#]+;!;
44             }
45             }
46              
47 1         16 my $out_line = '';
48 1 50       57 open my $SELF, '<', __FILE__ or croak "Can't read Getopt::App: $!";
49 1         37 while (my $line = readline $SELF) {
50 284 100       2615 next if $line =~ m!(?:\bVERSION\s|^\s*$)!; # TODO: Should version get skipped?
51 232 100       582 next if $line =~ m!^sub bundle\s\{! .. $line =~ m!^}$!; # skip bundle()
52 186 100       401 last if $line =~ m!^1;\s*$!; # do not include POD
53              
54 185         302 chomp $line;
55 185 100       429 if ($line =~ m!^sub\s!) {
    100          
56 15 100       30 print {$OUT} $out_line, "\n" if $out_line;
  1         5  
57 15 100       52 $line =~ m!\}$! ? print {$OUT} $line, "\n" : ($out_line = $line);
  2         8  
58             }
59             elsif ($line =~ m!^}$!) {
60 13         17 print {$OUT} $out_line, $line, "\n";
  13         39  
61 13         37 $out_line = '';
62             }
63             else {
64 157         476 $line =~ s!^[ ]{2,}!!; # remove leading white space
65 157         270 $line =~ s!\#\s.*!!; # remove comments
66 157         476 $out_line .= $line;
67             }
68             }
69              
70 1         4 print {$OUT} qq(BEGIN{\$INC{'Getopt/App.pm'}='BUNDLED'}\n);
  1         3  
71 1   50     2 print {$OUT} +($package || "package main\n");
  1         6  
72 1         3 print {$OUT} @script;
  1         4  
73 1         5 print {$OUT} $_ while readline $SCRIPT;
  2         52  
74             }
75              
76             sub capture {
77 25     25 1 338973 my ($app, $argv) = @_;
78              
79 25         5355 require File::Temp;
80 25         113983 my ($STDOUT_CAPTURE, $STDERR_CAPTURE) = (File::Temp->new, File::Temp->new);
81 25 50       34934 open my $STDOUT_ORIG, '>&STDOUT' or die "Can't remember original STDOUT: $!";
82 25 50       336 open my $STDERR_ORIG, '>&STDERR' or die "Can't remember original STDERR: $!";
83              
84             my $restore = sub {
85 25 50   25   668 open STDERR, '>&', fileno($STDERR_ORIG) or die "Can't restore STDERR: $!";
86 25 50       378 open STDOUT, '>&', fileno($STDOUT_ORIG) or die "Can't restore STDOUT: $!";
87 25 50       79 die $_[0] if $_[0];
88 25         170 };
89              
90 25 50       714 open STDOUT, '>&', fileno($STDOUT_CAPTURE) or $restore->("Can't capture STDOUT: $!");
91 25 50       413 open STDERR, '>&', fileno($STDERR_CAPTURE) or $restore->("Can't capture STDERR: $!");
92              
93 25         58 my $exit_value;
94 25 100 100     53 unless (eval { $exit_value = $app->($argv || [@ARGV]); 1; }) {
  25         153  
  21         259  
95 4         188 print STDERR $@;
96 4         41 $exit_value = int $!;
97             }
98              
99 25         193 STDERR->flush;
100 25         92 STDOUT->flush;
101 25         68 $restore->();
102 25         132 seek $STDERR_CAPTURE, 0, 0;
103 25         97 seek $STDOUT_CAPTURE, 0, 0;
104              
105 25         1532 return [join('', <$STDOUT_CAPTURE>), join('', <$STDERR_CAPTURE>), $exit_value];
106             }
107              
108             sub extract_usage {
109 4     4 1 20 my %pod2usage;
110 4         12 $pod2usage{'-sections'} = shift;
111 4   33     30 $pod2usage{'-input'} = shift || (caller)[1];
112 4 100       16 $pod2usage{'-verbose'} = 99 if $pod2usage{'-sections'};
113              
114 4         1140 require Pod::Usage;
115 4         102541 open my $USAGE, '>', \my $usage;
116 4         34 Pod::Usage::pod2usage(-exitval => 'noexit', -output => $USAGE, %pod2usage);
117 4         20057 close $USAGE;
118              
119 4   100     27 $usage //= '';
120 4 100       20 $usage =~ s!^(.*?)\n!!s if $pod2usage{'-sections'};
121 4         15 $usage =~ s!^Usage:\n\s+([A-Z])!$1!s; # Remove "Usage" header if SYNOPSIS has a description
122 4         13 $usage =~ s!^ !!gm;
123              
124 4   100     31 return join '', $usage, _usage_for_subcommands($SUBCOMMANDS || []), _usage_for_options($OPTIONS || []);
      50        
125             }
126              
127 10     10 1 51 sub getopt_complete_reply { Getopt::App::Complete::complete_reply(@_) }
128              
129 24     24 1 107 sub getopt_configure {qw(bundling no_auto_abbrev no_ignore_case pass_through require_order)}
130              
131             sub getopt_load_subcommand {
132 7     7 1 16 my ($app, $subcommand, $argv) = @_;
133 7 50       23 return $subcommand->[1] if ref $subcommand->[1] eq 'CODE';
134              
135 7   66     41 my $method = $subcommand->[1] =~ /^\w+$/ && $app->can($subcommand->[1]);
136 7         33 my @option_spec = @$OPTIONS;
137 2     2   10 return sub { _run($app, [@option_spec], $_[0], $method) }
138 7 100       20 if $method;
139              
140 6         24 ($@, $!) = ('', 0);
141 6 100       4400 croak "Unable to load subcommand $subcommand->[0]: $@ ($!)" unless my $code = do $subcommand->[1];
142 5         40 return $code;
143             }
144              
145             sub getopt_post_process_argv {
146 23     23 1 53 my ($app, $argv, $state) = @_;
147 23 50       63 return unless $state->{valid};
148 23 100 100     114 return unless $argv->[0] and $argv->[0] =~ m!^-!;
149 1         4 $! = 1;
150 1         16 die "Invalid argument or argument order: @$argv\n";
151             }
152              
153             sub getopt_unknown_subcommand {
154 1     1 1 3 my ($app, $argv) = @_;
155 1         5 $! = 2;
156 1         20 die "Unknown subcommand: $argv->[0]\n";
157             }
158              
159             sub import {
160 20     20   463793 my ($class, @flags) = @_;
161 20         61 my $caller = caller;
162              
163 20         1106 $_->import for qw(strict warnings utf8);
164 20         2913 feature->import(':5.16');
165              
166 20         54 my $skip_default;
167 7     7   89 no strict qw(refs);
  7         14  
  7         19016  
168 20         88 while (my $flag = shift @flags) {
169 10 100       52 if ($flag eq '-capture') {
    100          
    50          
    50          
170 6         15 *{"$caller\::capture"} = \&capture;
  6         42  
171 6         39 $skip_default = 1;
172             }
173             elsif ($flag eq '-complete') {
174 2         459 require Getopt::App::Complete;
175 2         8 *{"$caller\::generate_completion_script"} = \&Getopt::App::Complete::generate_completion_script;
  2         41  
176             }
177             elsif ($flag eq '-signatures') {
178 0         0 require experimental;
179 0         0 experimental->import(qw(signatures));
180             }
181             elsif ($flag !~ /^-/) {
182 2 100       158 croak "package definition required - cannot extend main with $flag!" if $caller eq 'main';
183 1 50       84 croak "require $flag FAIL $@" unless eval "require $flag;1";
184 1         10 push @{"${caller}::ISA"}, $flag;
  1         13  
185             }
186             }
187              
188 19 100       13084 unless ($skip_default) {
189 13 50       213 *{"$caller\::extract_usage"} = \&extract_usage unless $caller->can('extract_usage');
  13         69  
190 13 50       128 *{"$caller\::new"} = \&new unless $caller->can('new');
  13         45  
191 13         48 *{"$caller\::run"} = \&run;
  13         2021  
192             }
193             }
194              
195             sub new {
196 51     51 1 2847 my $class = shift;
197 51 100 33     497 bless @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {}, ref $class || $class;
  1 100       10  
198             }
199              
200             sub run {
201 12     12 1 216108 my ($cb, @option_spec) = (pop, @_);
202 12         51 my $class = caller;
203 12 50       51 exit _run($class->new, [@option_spec], [@ARGV], $cb) unless defined wantarray;
204 12     47   128 return sub { _run($class->new, [@option_spec], $_[0], $cb) };
  47         8069  
205             }
206              
207             sub _exit {
208 33     33   155 my ($app, $exit_value) = @_;
209 33   100     67 $exit_value = $app->$call_maybe(getopt_post_process_exit_value => $exit_value) // $exit_value;
210 33 100 100     219 $exit_value = 0 unless $exit_value and $exit_value =~ m!^\d{1,3}$!;
211 33 100       111 $exit_value = 255 unless $exit_value < 255;
212 33         277 return $exit_value;
213             }
214              
215             sub _run {
216 49     49   147 my ($app, $option_spec, $argv, $cb) = @_;
217 49   50     146 $argv ||= [@ARGV];
218              
219 49         146 local $OPTIONS = [@$option_spec];
220 49         92 local $DEPTH = $DEPTH + 1;
221 49 100 66     194 return $app->$call_maybe('getopt_complete_reply') if defined $ENV{COMP_POINT} and defined $ENV{COMP_LINE};
222              
223 39         110 $app->$call_maybe(getopt_pre_process_argv => $argv);
224 39         119 local $SUBCOMMANDS = $app->$call_maybe('getopt_subcommands');
225 39 100       463 my $exit_value = $SUBCOMMANDS ? _subcommand_run_maybe($app, $SUBCOMMANDS, $argv) : undef;
226 35 100       103 return _exit($app, $exit_value) if defined $exit_value;
227              
228 26         315 s!$OPT_COMMENT_RE.*$!! for @$option_spec;
229              
230 26         63 my @configure = $app->$call_maybe('getopt_configure');
231 26         107 my $prev = Getopt::Long::Configure(@configure);
232 26 100       2034 my $valid = Getopt::Long::GetOptionsFromArray($argv, $app, @$option_spec) ? 1 : 0;
233 26         8277 Getopt::Long::Configure($prev);
234 26         569 $app->$call_maybe(getopt_post_process_argv => $argv, {valid => $valid});
235 24 100       288 return _exit($app, $valid ? $app->$cb(@$argv) : 1);
236             }
237              
238             sub _subcommand_run_maybe {
239 19     19   44 my ($app, $subcommands, $argv) = @_;
240 19 100 100     120 return undef unless $argv->[0] and $argv->[0] =~ m!^\w!;
241              
242 14         25 local $SUBCOMMAND;
243             return $app->$call_maybe(getopt_unknown_subcommand => $argv)
244 14 100   46   103 unless $SUBCOMMAND = first { $_->[0] eq $argv->[0] } @$subcommands;
  46         91  
245              
246 10   66     71 my $cb = $APPS{$SUBCOMMAND->[1]} ||= $app->$call_maybe(getopt_load_subcommand => $SUBCOMMAND, $argv);
247 9 50       29 croak "$SUBCOMMAND->[0] did not return a code ref" unless ref $cb eq 'CODE';
248 9         44 return $cb->([@$argv[1 .. $#$argv]]);
249             }
250              
251             sub _usage_for_options {
252 4     4   9 my ($option_spec) = @_;
253 4 100       95 return '' unless @$option_spec;
254              
255 3         5 my ($len, @options) = (0);
256 3         8 for (@$option_spec) {
257 9         47 my @o = split $OPT_COMMENT_RE, $_, 2;
258 9         25 $o[0] =~ s/(=[si][@%]?|\!|\+)$//;
259 9 100       27 $o[0] = join ', ', map { length($_) == 1 ? "-$_" : "--$_" } sort { length($b) <=> length($a) } split /\|/, $o[0];
  11         32  
  2         5  
260 9   100     21 $o[1] //= '';
261              
262 9         12 my $l = length $o[0];
263 9 100       13 $len = $l if $l > $len;
264 9         17 push @options, \@o;
265             }
266              
267 3         822 return "Options:\n" . join('', map { sprintf " %-${len}s %s\n", @$_ } @options) . "\n";
  9         287  
268             }
269              
270             sub _usage_for_subcommands {
271 4     4   8 my ($subcommands) = @_;
272 4 100       23 return '' unless @$subcommands;
273              
274 1         3 my ($len, @cmds) = (0);
275 1         2 for my $s (@$subcommands) {
276 6         8 my $l = length $s->[0];
277 6 100       10 $len = $l if $l > $len;
278 6   50     16 push @cmds, [$s->[0], $s->[2] // ''];
279             }
280              
281 1         4 return "Subcommands:\n" . join('', map { sprintf " %-${len}s %s\n", @$_ } @cmds) . "\n";
  6         21  
282             }
283              
284             1;
285              
286             =encoding utf8
287              
288             =head1 NAME
289              
290             Getopt::App - Write and test your script with ease
291              
292             =head1 SYNOPSIS
293              
294             =head2 The script file
295              
296             #!/usr/bin/env perl
297             package My::Script;
298             use Getopt::App -complete, -signatures;
299              
300             # See "APPLICATION METHODS"
301             sub getopt_post_process_argv ($app, $argv, $state) { ... }
302             sub getopt_configure ($app) { ... }
303              
304             # run() must be the last statement in the script
305             run(
306              
307             # Specify your Getopt::Long options and optionally a help text
308             'h|help # Output help',
309             'v+ # Verbose output',
310             'name=s # Specify a name',
311             'completion-script # Print autocomplete script',
312              
313             # Here is the main sub that will run the script
314             sub ($app, @extra) {
315             return print generate_completion_script() if $app->{'completion-script'};
316             return print extract_usage() if $app->{h};
317             say $app->{name} // 'no name'; # Access command line options
318             return 42; # Reture value is used as exit code
319             }
320             );
321              
322             =head2 Running the script
323              
324             The example script above can be run like any other script:
325              
326             $ my-script --name superwoman; # prints "superwoman"
327             $ echo $? # 42
328              
329             =head2 Testing
330              
331             use Test::More;
332             use Cwd qw(abs_path);
333             use Getopt::App -capture;
334              
335             # Sourcing the script returns a callback
336             my $app = do(abs_path('./bin/myapp'));
337              
338             # The callback can be called with any @ARGV
339             subtest name => sub {
340             my $got = capture($app, [qw(--name superwoman)]);
341             is $got->[0], "superwoman\n", 'stdout';
342             is $got->[1], '', 'stderr';
343             is $got->[2], 42, 'exit value';
344             };
345              
346             done_testing;
347              
348             =head2 Subcommands
349              
350             #!/usr/bin/env perl
351             # Define a package to avoid mixing methods after loading the subcommand script
352             package My::App::main;
353             use Getopt::App -complete;
354              
355             # getopt_subcommands() is called by Getopt::App
356             sub getopt_subcommands {
357             my $app = shift;
358              
359             return [
360             ['find', '/path/to/subcommand/find.pl', 'Find things'],
361             ['update', '/path/to/subcommand/update.pl', 'Update things'],
362             ];
363             }
364              
365             # run() is only called if there are no matching sub commands
366             run(
367             'h # Print help',
368             'completion-script # Print autocomplete script',
369             sub {
370             my ($app, @args) = @_;
371             return print generate_completion_script() if $app->{'completion-script'};
372             return print extract_usage();
373             }
374             );
375              
376             See L and L
377             for more details.
378              
379             =head1 DESCRIPTION
380              
381             L is a module that helps you structure your scripts and integrates
382             L with a very simple API. In addition it makes it very easy to
383             test your script, since the script file can be sourced without actually being
384             run.
385              
386             L also supports infinite nested L
387             and a method for L this module with your script to prevent
388             depending on a module from CPAN.
389              
390             =head1 VARIABLES
391              
392             =head2 DEPTH
393              
394             C<$Getopt::App::DEPTH> will be increased for each sub command and will be C<0>
395             for the first L.
396              
397             =head2 SUBCOMMAND
398              
399             C<$Getopt::App::SUBCOMMAND> will be set to the active sub command element. See
400             also L.
401              
402             =head1 APPLICATION METHODS
403              
404             These methods are optional, but can be defined in your script to override the
405             default behavior.
406              
407             Order of how the methods are called:
408              
409             run(@option_spec, $cb)
410             -> getopt_pre_process_argv(\@argv)
411             -> getopt_configure()
412             -> getopt_post_process_argv(\@argv, \%state)
413             -> $cb
414              
415             run(@option_spec, $cb)
416             -> getopt_pre_process_argv(\@argv)
417             -> getopt_subcommands()
418             -> getopt_load_subcommand($subcommand, \@argv)
419             -> getopt_configure()
420             -> getopt_post_process_argv(\@argv, \%state)
421             -> $cb
422              
423             =head2 getopt_complete_reply
424              
425             $app->getopt_complete_reply;
426              
427             This method will be called instead of the L callback when the
428             C and C environment variables are set. The default
429             implementation will call L.
430              
431             See also "Completion" under L.
432              
433             =head2 getopt_configure
434              
435             @configure = $app->getopt_configure;
436              
437             This method can be defined if you want L to be set up
438             differently. The default return value is:
439              
440             qw(bundling no_auto_abbrev no_ignore_case pass_through require_order)
441              
442             Note that the default "pass_through" item is to enable the default
443             L to croak on invalid arguments, since
444             L will by default just warn to STDERR about unknown arguments.
445              
446             =head2 getopt_load_subcommand
447              
448             $code = $app->getopt_load_subcommand($subcommand, [@ARGV]);
449              
450             Takes the subcommand found in the L list and the command
451             line arguments and must return a CODE block. The default implementation is
452             simply:
453              
454             $code = do($subcommand->[1]);
455              
456             =head2 getopt_post_process_argv
457              
458             $bool = $app->getopt_post_process_argv([@ARGV], {%state});
459              
460             This method can be used to post process the options. C<%state> contains a key
461             "valid" which is true or false, depending on the return value from
462             L.
463              
464             This method can C and optionally set C<$!> to avoid calling the function
465             passed to L.
466              
467             The default behavior is to check if the first item in C<$argv> starts with a
468             hyphen, and C with an error message if so:
469              
470             Invalid argument or argument order: @$argv\n
471              
472             =head2 getopt_post_process_exit_value
473              
474             $exit_value = $app->getopt_post_process_exit_value($exit_value);
475              
476             A method to be called after the L function has been called.
477             C<$exit_value> holds the return value from L which could be any value,
478             not just 0-255. This value can then be changed to change the exit value from
479             the program.
480              
481             sub getopt_post_process_exit_value ($app, $exit_value) {
482             return int(1 + rand 10);
483             }
484              
485             =head2 getopt_pre_process_argv
486              
487             $app->getopt_pre_process_argv($argv);
488              
489             This method can be defined to pre-process C<$argv> before it is passed on to
490             L. Example:
491              
492             sub getopt_pre_process_argv ($app, $argv) {
493             $app->{first_non_option} = shift @$argv if @$argv and $argv->[0] =~ m!^[a-z]!;
494             }
495              
496             This method can C and optionally set C<$!> to avoid calling the actual
497             L function.
498              
499             =head2 getopt_subcommands
500              
501             $subcommands = $app->getopt_subcommands;
502              
503             This method must be defined in the script to enable sub commands. The return
504             value must be either C to disable subcommands or an array-ref of
505             array-refs like this:
506              
507             [["subname", "/abs/path/to/sub-command-script", "help text"], ...]
508              
509             The first element in each array-ref "subname" will be matched against the first
510             command line option, and when matched, the given subcommand item will be passed
511             on to L which must return a code-ref, preferably from
512             L. The sub command will have C<$Getopt::App::SUBCOMMAND> set to the item
513             found in the list.
514              
515             =head2 getopt_unknown_subcommand
516              
517             $exit_value = $app->getopt_unknown_subcommand($argv);
518              
519             Will be called when L is defined but C<$argv> does not
520             match an item in the list. Default behavior is to C with an error message:
521              
522             Unknown subcommand: $argv->[0]\n
523              
524             Returning C instead of dying or a number (0-255) will cause the L
525             callback to be called.
526              
527             =head1 EXPORTED FUNCTIONS
528              
529             =head2 capture
530              
531             use Getopt::App -capture;
532             my $app = do '/path/to/bin/myapp';
533             my $array_ref = capture($app, [@ARGV]); # [$stdout, $stderr, $exit_value]
534              
535             Used to run an C<$app> and capture STDOUT, STDERR and the exit value in that
536             order in C<$array_ref>. This function will also capture C. C<$@> will be
537             set and captured in the second C<$array_ref> element, and C<$exit_value> will
538             be set to C<$!>.
539              
540             This function is a very slimmed down alternative to L.
541             The main reason why L exists in this package is that if something
542             inside the C<$app> throws an exception, then it will be part of the captured
543             C<$stderr> instead of making C throw an exception.
544              
545             L is however more robust than this function, so please
546             try L out in case you find an edge case.
547              
548             =head2 extract_usage
549              
550             # Default to "SYNOPSIS" from current file
551             my $str = extract_usage($section, $file);
552             my $str = extract_usage($section);
553             my $str = extract_usage();
554              
555             Will extract a C<$section> from POD C<$file> and append command line option
556             descriptions when called from inside of L. Command line options can
557             optionally have a description with "spaces-hash-spaces-description", like this:
558              
559             run(
560             'o|option # Some description',
561             'v|verbose # Enable verbose output',
562             sub {
563             ...
564             },
565             );
566              
567             This function will I be exported if a function with the same name already
568             exists in the script.
569              
570             =head2 new
571              
572             my $app = new($class, %args);
573             my $app = new($class, \%args);
574              
575             This function is exported into the caller package so we can construct a new
576             object:
577              
578             my $app = Application::Class->new(\%args);
579              
580             This function will I be exported if a function with the same name already
581             exists in the script.
582              
583             =head2 run
584              
585             # Run a code block on valid @ARGV
586             run(@option_spec, sub ($app, @extra) { ... });
587              
588             # For testing
589             my $cb = run(@option_spec, sub ($app, @extra) { ... });
590             my $exit_value = $cb->([@ARGV]);
591              
592             L can be used to call a callback when valid command line options are
593             provided. On invalid arguments, warnings will be issued and the program will
594             exit with C<$?> set to 1.
595              
596             C<$app> inside the callback is a hash blessed to the caller package. The keys
597             in the hash are the parsed command line options, while C<@extra> is the extra
598             unparsed command line options.
599              
600             C<@option_spec> are the same options as L can take. Example:
601              
602             # app.pl -vv --name superwoman -o OptX cool beans
603             run(qw(h|help v+ name=s o=s@), sub ($app, @extra) {
604             die "No help here" if $app->{h};
605             warn $app->{v}; # 2
606             warn $app->{name}; # "superwoman"
607             warn @{$app->{o}}; # "OptX"
608             warn @extra; # "cool beans"
609             return 0; # Used as exit code
610             });
611              
612             In the example above, C<@extra> gets populated, since there is a non-flag value
613             "cool" after a list of valid command line options.
614              
615             =head1 METHODS
616              
617             =head2 bundle
618              
619             Getopt::App->bundle($path_to_script);
620             Getopt::App->bundle($path_to_script, $fh);
621              
622             This method can be used to combine L and C<$path_to_script> into a
623             a single script that does not need to have L installed from CPAN.
624             This is for example useful for sysadmin scripts that otherwise only depends on
625             core Perl modules.
626              
627             The script will be printed to C<$fh>, which defaults to C.
628              
629             Example usage:
630              
631             perl -MGetopt::App -e'Getopt::App->bundle(shift)' ./src/my-script.pl > ./bin/my-script;
632              
633             =head2 import
634              
635             use Getopt::App;
636             use Getopt::App 'My::Script::Base', -signatures;
637             use Getopt::App -capture;
638              
639             =over 2
640              
641             =item * Default
642              
643             use Getopt::App;
644              
645             Passing in no flags will export the default functions L,
646             L and L. In addition it will save you from a lot of typing, since
647             it will also import the following:
648              
649             use strict;
650             use warnings;
651             use utf8;
652             use feature ':5.16';
653              
654             =item * Completion
655              
656             use Getopt::App -complete;
657              
658             Same as L, but will also load L and import
659             L.
660              
661             =item * Signatures
662              
663             use Getopt::App -signatures;
664              
665             Same as L, but will also import L. This
666             requires Perl 5.20+.
667              
668             =item * Class name
669              
670             package My::Script::Foo;
671             use Getopt::App 'My::Script';
672              
673             Same as L but will also make C inherit from
674             L. Note that a package definition is required.
675              
676             =item * Capture
677              
678             use Getopt::App -capture;
679              
680             This will only export L.
681              
682             =back
683              
684             =head1 COPYRIGHT AND LICENSE
685              
686             This library is free software. You can redistribute it and/or modify it under
687             the same terms as Perl itself.
688              
689             =head1 AUTHOR
690              
691             Jan Henning Thorsen - C
692              
693             =cut