| 1 | package Getopt::Lazier; | ||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use File::Basename; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 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 | 1 | 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<new> 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<use> pragma will make the import method | ||||||||||||||||||||||||||||||||||||
| 108 | run C<new> automatically on C<@ARGV> and import the string as variable names in package C<main>. | ||||||||||||||||||||||||||||||||||||
| 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<gt>{help}> | ||||||||||||||||||||||||||||||||||||
| 116 | would be set to C<1>. | ||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||
| 118 | =cut | ||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||
| 120 | 1 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=9.6
| |||||||||||||||||||||||||||||||||||||
| 121 | 1 | my ($exporter, $fuckery) = @_; | |||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||
| 123 | 1 | ▶ | if ($fuckery) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 124 | 0 | my $opt = new(); | |||||||||||||||||||||||||||||||||||
| 125 | 1 | ▶ | no strict 'refs'; # so naughty! | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 126 | # Create hash in main. | ||||||||||||||||||||||||||||||||||||
| 127 | 0 | *{"main::$fuckery"} = \%$opt; | |||||||||||||||||||||||||||||||||||
| 128 | # Create hashref in main (for backwards compatability) | ||||||||||||||||||||||||||||||||||||
| 129 | 0 | *{"main::$fuckery"} = \\%{"main::$fuckery"}; | |||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | sub new { # DNM: I <3 this function. | ||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 134 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | my @ARGA = scalar(@_) ? @_ : @main::ARGV; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 136 | 0 | my $opt = {}; | |||||||||||||||||||||||||||||||||||
| 137 | 0 | my @DARG; | |||||||||||||||||||||||||||||||||||
| 138 | 0 | my $var = uc(basename($0)); | |||||||||||||||||||||||||||||||||||
| 139 | 0 | my $cont = 1; | |||||||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | unshift(@ARGA, split(/\s+/, $ENV{$var})) if ($ENV{$var}); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 141 | 0 | foreach my $ar (@ARGA) { | |||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | if ($cont && $ar eq '--') { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition0%0/3
$cont and $ar eq "--"
MC/DC0%0/2
$cont and $ar eq "--"
$cont
$ar eq "--"
Truth table0%0/3
A and B
A= $contB= $ar eq "--"
| |||||||||||||||||||||||||||||||||||||
| 143 | 0 | $cont = 0; | |||||||||||||||||||||||||||||||||||
| 144 | ▶ | } elsif ($cont && $ar =~ m/^--?(.*?)[=|:](.*)/) { | |||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$cont and $ar =~ /^--?(.*?)[=|:](.*)/
MC/DC0%0/2
$cont and $ar =~ /^--?(.*?)[=|:](.*)/
$cont
$ar =~ /^--?(.*?)[=|:](.*)/
Truth table0%0/3
A and B
A= $contB= $ar =~ /^--?(.*?)[=|:](.*)/
| |||||||||||||||||||||||||||||||||||||
| 145 | 0 | ${$opt}{$1} = $2; | |||||||||||||||||||||||||||||||||||
| 146 | ▶ | } elsif ($cont && $ar =~ m/^--?(.*)$/) { | |||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$cont and $ar =~ /^--?(.*)$/
MC/DC0%0/2
$cont and $ar =~ /^--?(.*)$/
$cont
$ar =~ /^--?(.*)$/
Truth table0%0/3
A and B
A= $contB= $ar =~ /^--?(.*)$/
| |||||||||||||||||||||||||||||||||||||
| 147 | 0 | ${$opt}{$1} = 1; | |||||||||||||||||||||||||||||||||||
| 148 | } else { | ||||||||||||||||||||||||||||||||||||
| 149 | 0 | push @DARG, $ar; | |||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | return ($opt, @DARG) if wantarray; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 153 | 0 | @main::ARGV = @DARG; | |||||||||||||||||||||||||||||||||||
| 154 | 0 | return($opt); | |||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||
| 157 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||
| 159 | Jojess Fournier, C<< <jojessf@cpan.org> >>, Dave Maez | ||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||
| 161 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||
| 163 | Please report any bugs or feature requests to C<bug-getopt-lazier at rt.cpan.org>, or through | ||||||||||||||||||||||||||||||||||||
| 164 | the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Getopt-Lazier>. 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<https://github.com/jojessf/GetOptLazier> | ||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||
| 181 | =over 4 | ||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||
| 183 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||
| 185 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Getopt-Lazier> | ||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||
| 187 | =item * CPAN Ratings | ||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||
| 189 | L<https://cpanratings.perl.org/d/Getopt-Lazier> | ||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||
| 191 | =item * Search CPAN | ||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||
| 193 | L<https://metacpan.org/release/Getopt-Lazier> | ||||||||||||||||||||||||||||||||||||
| 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 | 1; # End of Getopt::Lazier | |||||||||||||||||||||||||||||||||||