File Coverage

blib/lib/URI/VersionRange/App.pm
Criterion Covered Total %
statement 47 71 66.2
branch 10 28 35.7
condition n/a
subroutine 11 12 91.6
pod 2 2 100.0
total 70 113 61.9


line stmt bran cond sub pod time code
1             package URI::VersionRange::App;
2              
3 2     2   2192 use feature ':5.10';
  2         3  
  2         351  
4 2     2   14 use strict;
  2         5  
  2         61  
5 2     2   13 use warnings;
  2         4  
  2         138  
6 2     2   12 use utf8;
  2         4  
  2         18  
7              
8 2     2   112 use Getopt::Long qw(GetOptionsFromArray :config gnu_compat);
  2         26  
  2         20  
9 2     2   496 use Pod::Usage qw(pod2usage);
  2         4  
  2         95  
10 2     2   38 use Carp ();
  2         6  
  2         40  
11 2     2   9 use JSON::PP ();
  2         3  
  2         32  
12 2     2   7 use Data::Dumper ();
  2         4  
  2         31  
13              
14 2     2   611 use URI::VersionRange ();
  2         5  
  2         1727  
15              
16             our $VERSION = '2.25';
17              
18             sub cli_error {
19 0     0 1 0 my ($error) = @_;
20 0         0 $error =~ s/ at .* line \d+.*//;
21 0         0 print STDERR "ERROR: $error\n";
22             }
23              
24             sub run {
25              
26 1     1 1 7954 my ($class, @args) = @_;
27              
28 1         4 my %options = (format => 'json');
29              
30 1 50       7 GetOptionsFromArray(
31             \@args, \%options, qw(
32             help
33             man
34             v
35              
36             contains=s
37              
38             null|0
39             format=s
40              
41             json
42             human-readable|h
43             )
44             ) or pod2usage(-verbose => 0);
45              
46 1 50       535 pod2usage(-exitstatus => 0, -verbose => 2) if defined $options{man};
47 1 50       3 pod2usage(-exitstatus => 0, -verbose => 0) if defined $options{help};
48              
49 1 50       3 if (defined $options{v}) {
50              
51 0         0 (my $progname = $0) =~ s/.*\///;
52              
53 0         0 say <<"VERSION";
54             $progname version $URI::VersionRange::VERSION
55              
56             Copyright 2022-2026, Giuseppe Di Terlizzi
57              
58             This program is part of the "URI-PackageURL" distribution and is free software;
59             you can redistribute it and/or modify it under the same terms as Perl itself.
60              
61             Complete documentation for $progname can be found using 'man $progname'
62             or on the internet at .
63             VERSION
64              
65 0         0 return 0;
66              
67             }
68              
69 1         2 my ($vers_string) = @args;
70              
71 1 50       2 pod2usage(-verbose => 1) if !$vers_string;
72              
73 1 50       3 $options{format} = 'json' if defined $options{json};
74 1 50       3 $options{format} = 'human-readable' if defined $options{'human-readable'};
75              
76 1         1 my $vers = eval { URI::VersionRange->from_string($vers_string) };
  1         7  
77              
78 1 50       3 if ($@) {
79 0         0 cli_error($@);
80 0         0 return 1;
81             }
82              
83 1 50       3 if (defined $options{contains}) {
84              
85 0         0 my $vers_comparator_class = join '::', 'URI::VersionRange::Version', $vers->scheme;
86              
87 0 0       0 if (!$vers_comparator_class->can('new')) {
88 0         0 say STDERR 'WARNING: Loaded the fallback scheme class comparator.';
89 0         0 say STDERR ' The comparison may not work correctly!';
90             }
91              
92 0         0 my $res = eval { $vers->contains($options{contains}) };
  0         0  
93              
94 0 0       0 if ($@) {
95 0         0 cli_error($@);
96 0         0 return 1;
97             }
98              
99 0 0       0 say STDERR $res ? 'true' : 'false';
100 0         0 return !$res;
101              
102             }
103              
104 1 50       2 if ($options{format} eq 'json') {
105 1         8 print JSON::PP->new->canonical->pretty(1)->convert_blessed(1)->encode($vers);
106 1         133 return 0;
107             }
108              
109 0 0         if ($options{format} eq 'human-readable') {
110 0           say $vers->scheme;
111 0           say "- " . $_->to_human_string for (@{$vers->constraints});
  0            
112 0           return 0;
113             }
114              
115             }
116              
117             1;
118              
119             __END__