File Coverage

blib/lib/Getopt/Lazier.pm
Criterion Covered Total %
statement 16 40 40.0
branch 1 14 7.1
condition 0 9 0.0
subroutine 6 7 85.7
pod 1 1 100.0
total 24 71 33.8


line stmt bran cond sub pod time code
1             package Getopt::Lazier;
2              
3 1     1   94735 use 5.006;
  1         3  
4 1     1   8 use strict;
  1         2  
  1         24  
5 1     1   3 use warnings;
  1         1  
  1         39  
6 1     1   4 use File::Basename;
  1         1  
  1         127  
7              
8             =head1 NAME
9              
10             Getopt::Lazier - Lazy Getopt-like command-line options and argument parser
11              
12             =head1 VERSION
13              
14             Version 0.06
15              
16             =cut
17              
18             our $VERSION = '0.06';
19              
20              
21             =head1 SYNOPSIS
22              
23             my ($opt, @DARG) = Getopt::Lazier->new(@ARGV);
24              
25             =head2 EXAMPLE USAGE
26              
27             Lazy:
28              
29             use Getopt::Lazier;
30              
31             my ($opt, @DARG) = Getopt::Lazier->new(@ARGV);
32              
33             use Data::Dumper; print Dumper([$opt, \@DARG])."\n";
34              
35             # perl lazyscript.pl -help a b c d --meow=5345923 -awoo="doggo vibes" -- --this-aint-no-option
36              
37             $VAR1 = [
38             {
39             'awoo' => 'doggo vibes',
40             'meow' => '5345923',
41             'help' => 1
42             },
43             [
44             'a',
45             'b',
46             'c',
47             'd',
48             '--this-aint-no-option'
49             ]
50             ];
51              
52             Lazier:
53              
54             use Getopt::Lazier;
55              
56             my $opt = Getopt::Lazier->new();
57              
58             use Data::Dumper; print Dumper([$opt, \@ARGV])."\n";
59              
60             # perl lazierscript.pl -o -p ok
61              
62             $VAR1 = [
63             {
64             'o' => 1,
65             'p' => 1
66             },
67             [
68             'ok'
69             ]
70             ];
71              
72             More Lazier:
73              
74             use Getopt::Lazier "ovar";
75              
76             use Data::Dumper; print Dumper([{%ovar}, $ovar, \@ARGV])."\n";
77              
78             # perl t.pl --opt1=val arg --opt2 arg2
79              
80             $VAR1 = [
81             {
82             'opt1' => 'val',
83             'opt2' => 1
84             },
85             {
86             'opt1' => 'val',
87             'opt2' => 1
88             },
89             [
90             'arg',
91             'arg2'
92             ]
93             ];
94              
95             =cut
96              
97             =head1 SUBROUTINES/METHODS
98              
99             =head2 new
100              
101             The laziest way to parse arguments tho.
102             Returns a hashref of parsed options, and (if called in list context) an array of remaining arguments.
103             C takes a list/array as an argument, and if unspecified will use @ARGV by default.
104              
105             =head2 import
106              
107             Now with namespace fuckery! Passing a string to the C pragma will make the import method
108             run C automatically on C<@ARGV> and import the string as variable names in package C
.
109              
110             For example:
111              
112             use Getopt::Lazier "options";
113              
114             Will import both C<%options> (a hash of the parsed options), and (for backwards compatability) C<$options> (a
115             reference to the hash). If the script was passed C<--help> on the command line, both C<$options{help}> and C<$options-E{help}>
116             would be set to C<1>.
117              
118             =cut
119              
120             sub import {
121 1     1   9 my ($exporter, $fuckery) = @_;
122              
123 1 50       11 if ($fuckery) {
124 0           my $opt = new();
125 1     1   5 no strict 'refs'; # so naughty!
  1         6  
  1         334  
126             # Create hash in main.
127 0           *{"main::$fuckery"} = \%$opt;
  0            
128             # Create hashref in main (for backwards compatability)
129 0           *{"main::$fuckery"} = \\%{"main::$fuckery"};
  0            
  0            
130             }
131             }
132              
133             sub new { # DNM: I <3 this function.
134 0     0 1   my $self = shift;
135 0 0         my @ARGA = scalar(@_) ? @_ : @main::ARGV;
136 0           my $opt = {};
137 0           my @DARG;
138 0           my $var = uc(basename($0));
139 0           my $cont = 1;
140 0 0         unshift(@ARGA, split(/\s+/, $ENV{$var})) if ($ENV{$var});
141 0           foreach my $ar (@ARGA) {
142 0 0 0       if ($cont && $ar eq '--') {
    0 0        
    0 0        
143 0           $cont = 0;
144             } elsif ($cont && $ar =~ m/^--?(.*?)[=|:](.*)/) {
145 0           ${$opt}{$1} = $2;
  0            
146             } elsif ($cont && $ar =~ m/^--?(.*)$/) {
147 0           ${$opt}{$1} = 1;
  0            
148             } else {
149 0           push @DARG, $ar;
150             }
151             }
152 0 0         return ($opt, @DARG) if wantarray;
153 0           @main::ARGV = @DARG;
154 0           return($opt);
155             }
156              
157             =head1 AUTHOR
158              
159             Jojess Fournier, C<< >>, Dave Maez
160              
161             =head1 BUGS
162              
163             Please report any bugs or feature requests to C, or through
164             the web interface at L. I will be notified, and then you'll
165             automatically be notified of progress on your bug as I make changes.
166              
167              
168              
169              
170             =head1 SUPPORT
171              
172             You can find documentation for this module with the perldoc command.
173              
174             perldoc Getopt::Lazier
175              
176              
177             You can also look for information at:
178              
179             L
180              
181             =over 4
182              
183             =item * RT: CPAN's request tracker (report bugs here)
184              
185             L
186              
187             =item * CPAN Ratings
188              
189             L
190              
191             =item * Search CPAN
192              
193             L
194              
195             =back
196              
197              
198             =head1 ACKNOWLEDGEMENTS
199              
200             Thanks to Dave for the ENV addition. Also for being awesome. :3
201              
202             =cut
203             =head1 LICENSE AND COPYRIGHT
204              
205             This software is Copyright (c) 2024 by Jojess Fournier.
206              
207             This is free software, licensed under:
208              
209             GNU GENERAL PUBLIC LICENSE 3.0
210              
211              
212             =cut
213              
214             1; # End of Getopt::Lazier