blib/lib/Getopt/Auto/Long/Usage.pm

filter: Statementstmt 23.3% 14 / 60 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 57.1% 4 / 7 Podpod 100% 1 / 1 totaltotal 17.9% 19 / 106 CC 19 SCAR 53.4
CC
19
Cov
15%
getoptlong2usage
306.0
__ANON__
6.0
__ANON__
6.0
CRAP 209.2
↑ Summary Build »
1 #!/usr/bin/env -S perl -CSDA
2  
3 # Copyright 2021 Alin Mr. <almr.oss@outlook.com>. Licensed under the MIT license (https://opensource.org/licenses/MIT).
4  
5 package Getopt::Auto::Long::Usage;
6 1 ▶use v5.28.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 C<Getopt::Auto::Long::Usage> - generate usage strings from Getopt::Long specs
12  
13 =head1 VERSION
14  
15 Version 0.01
16  
17 =cut
18  
19 1 our $VERSION = '0.01';
20  
21 =head1 SYNOPSIS
22  
23 This is a pure perl module that generates simple usage / help messages by parsing L<Getopt::Long> argument specs (and optionally using provided descriptions).
24  
25 print getoptlong2usage( Getopt_Long => \@conf [, ...] )
26  
27 =head1 DESCRIPTION
28  
29 C<Getopt::Auto::Long::Usage> strives to be compatible with L<Getopt::LongUsage>. In particular, it does not require supplementing existing arglist specs with additional data (e.g. descriptions are optional). However, the goal is to provide maximum maintainability with the least amount of code, not to achieve complete L<Getopt::Long> coverage. So, there are some differences:
30  
31 =over 4
32  
33 =item * the generated usage clearly distinguishes boolean flags from arguments requiring an option, and prints type information for the latter. For negatable boolean options (C<longopt|s!>), it will print the corresponding C<--no-longopt> flag (but not C<--no-s>).
34  
35 =item * there are no dependencies; the main function can be copied directly into your source code, if necessary
36  
37 =item * it does not attempt to parse C<GetOptions()> abbreviated / case-insensitive options, and in fact recommends that you disable those when using C<Getopt::Long> for maintainability and predictability. One shortopt + one (or several) longopts, explicitly specified, will avoid nasty surprises (plus, suppose you decide to rewrite the code in some other language...)
38  
39 =back
40  
41 The following example should print the generated help message either to stdout, if requested (C<--help>) or to stderr, if argument parsing fails.
42  
43 use Getopt::Auto::Long::Usage;
44 use Getopt::Long;
45 my @getoptargs = qw{ help
46 delim:s
47 eval|e!
48 };
49 my %_O_; my @getoptconf = (\%_O_, @getoptargs);
50  
51 sub usage {
52 my ($rc) = @_;
53 my @dsc = ( delim => 'instead of newline' );
54 print getoptlong2usage(
55 Getopt_Long => \@getoptconf, # all others optional
56 cli_use => "Arguments: [OPTION]...\nOptions:";
57 footer => "No other arguments may be supplied"
58 descriptions => \@dsc
59 );
60 exit $rc if defined( $rc );
61 }
62  
63 Getopt::Long::Configure( qw(
64 no_ignore_case no_auto_abbrev no_getopt_compat
65 gnu_compat bundling
66 ));
67 unless( GetOptions( @getoptconf ) ) {
68 local *STDOUT = *STDERR; usage 1;
69 }
70 usage 0 if( $_O_{ help } );
71  
72 =head1 EXPORT
73  
74 =over 4
75  
76 =item * C<getoptlong2usage>
77  
78 =back
79  
80 =cut
81  
82 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
83 1 our @EXPORT = qw(getoptlong2usage);
84  
85 =head1 FUNCTIONS
86  
87 =head2 getoptlong2usage
88  
89 $usage = getoptlong2usage( Getopt_Long => \@getoptconf [,
90 descriptions => \@dsc,
91 cli_use => $cli_use,
92 indent => $n,
93 pfx => $prefix ] )
94  
95 C<@getoptconf> is an arrayref containing all the arguments you would supply to C<GetOptions()>, including the initial hashref in which C<GetOptions()> stores results (and which is ignored). It's easiest to define C<@getoptconf> separately and reuse it for both calls. See the synopsis for an example.
96  
97 =cut
98  
99 sub getoptlong2usage {
100 0 ▶ my %_O_ = @_; $_O_{ descriptions } //= [];
Subroutine getoptlong2usage: not called CC=17 SCAR=57.2
Pod documented
Condition0%0/2
$_O_{"descriptions"} //= []
l!l
00
MC/DC0%0/1
$_O_{"descriptions"} //= []
$_O_{"descriptions"}
Truth table0%0/2
A
A= $_O_{"descriptions"}
Aresult
11
00
101 0 ▶ $_O_{ $_ } //= '' for qw{ pfx cli_use footer };
Condition0%0/2
$_O_{$_} //= ""
l!l
00
MC/DC0%0/1
$_O_{$_} //= ""
$_O_{$_}
Truth table0%0/2
A
A= $_O_{$_}
Aresult
11
00
102 0 my ($conf, $pfx ) = @_O_{ qw{ Getopt_Long pfx }};
103 0 my %dsc = @{ $_O_{ descriptions } };
104  
105 0 ▶ my $opt2dash = sub { length $_ == 1 ? "-$_" : "--$_"; };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
length $_ == 1 ? : 0 0 0
106 0 ▶ my $finalnl = sub { my ($s) = @_; $s .= "\n" unless $s =~ /(^|\n)$/; $s; };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $s =~ /(^|\n)$/u 0 0 0
107 # my $uniq = sub { my %cn; return grep { ! $cn{ $_ }++ } @_; }; # for auto_abbrev
108  
109 0 my $out = &$finalnl( $_O_{ cli_use } );
110 0 ▶ $pfx = (' ' x $_O_{ indent }) if defined( $_O_{ indent } );
Branch: 1 branch
BranchTrueFalseTotal
if defined $_O_{"indent"} 0 0 0
111  
112 0 my %t2p = (s => 'STR', i => 'INT', f => 'REAL', o => '[0[x|b]]INT', '+' => 'repeated...'); # TODO: :number, = t [desttype] [repeat]
113  
114 0 for( @{ $conf }[ 1..$#$conf ] ) {
115 0 my $a = $_; my $isneg = 0; my $t = ''; my $isopt = 0;
116 0 ▶ if (/(.*)!$/) { $a = $1; $isneg = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (/(.*)!$/u) 0 0 0
117 0 ▶ if (/(.*)\+$/) { $a = $1; $t = '+'; $isopt = 1; }
Branch: 3 branches
BranchTrueFalseTotal
if (/(.*)\+$/u) { } 0 0 0
elsif (/(.*):(.*)/u) { } 0 0 0
elsif (/(.*)=(.*)/u) { } 0 0 0
118 0 elsif (/(.*):(.*)/) { $a = $1; $t = $2; $isopt = 1; }
119 0 elsif (/(.*)=(.*)/) { $a = $1; $t = $2; }
120  
121 0 $a =~ qr{ (^|\|) ( (?<long> [^|]{2,}) ($|\|) ) }x; my $along = $+{long};
122 0 my @aa = split( /\|/, $a );
123 # @aa = &$uniq( @aa, substr( $along, 0, 1 ) ) if length $along; # handle auto_abbrev
124 0 $out .= $pfx . join( ' | ', map( &$opt2dash, @aa ) );
125 0 ▶ if( length $t ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $t) { } 0 0 0
126 0 ▶ $t = $t2p{ $t } // "ARG:$t"; $t = "[$t]" if $isopt;
Branch: 1 branch
BranchTrueFalseTotal
if $isopt 0 0 0
Condition0%0/2
$t2p{$t} // "ARG:$t"
l!l
00
MC/DC0%0/1
$t2p{$t} // "ARG:$t"
$t2p{$t}
Truth table0%0/2
A
A= $t2p{$t}
Aresult
11
00
127 0 $out .= " $t";
128 } else {
129 0 ▶ if( $isneg ) { $out .= "\n$pfx--no-" . $along if length $along; }
Branch: 2 branches
BranchTrueFalseTotal
if ($isneg) 0 0 0
if length $along 0 0 0
130 }
131 0 ▶ $out .= ": $dsc{ $along }" if length $along and $dsc{ $along };
Branch: 1 branch
BranchTrueFalseTotal
if length $along and $dsc{$along} 0 0 0
Condition0%0/3
length $along and $dsc{$along}
!ll&&!rl&&r
000
MC/DC0%0/2
length $along and $dsc{$along}
length $along $dsc{$along}
Truth table0%0/3
A and B
A= length $alongB= $dsc{$along}
ABresult
0X0
100
111
132 0 $out .= "\n";
133 }
134 0 return $out . &$finalnl( $_O_{ footer } );
135 }
136  
137 1 1;
138  
139 =head1 SOURCE
140  
141 The source code repository can be found at L<https://gitlab.com/kstr0k/perl-getopt-auto-long-usage.git>.
142  
143 =head1 SUPPORT
144  
145 You can find documentation for this module with the perldoc command.
146  
147 perldoc Getopt::Auto::Long::Usage
148  
149 =head1 AUTHOR
150  
151 Alin Mr., C<< <almr.oss at outlook.com> >>
152  
153 =head1 LICENSE AND COPYRIGHT
154  
155 This software is Copyright (c) 2021 by Alin Mr.
156  
157 This is free software, licensed under:
158  
159 The MIT (X11) License
160  
161 =cut
162  
163 # vi: set ft=perl:
↑ Summary Build »