| 1 | # Copyright © 2014 Guillem Jover <guillem@debian.org> | ||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||
| 3 | # This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||
| 4 | # it under the terms of the GNU General Public License as published by | ||||||||||||||||||||||||||||||||
| 5 | # the Free Software Foundation; either version 2 of the License, or | ||||||||||||||||||||||||||||||||
| 6 | # (at your option) any later version. | ||||||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||||||
| 8 | # This program is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||||||
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||||||
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||||||||||||||
| 11 | # GNU General Public License for more details. | ||||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||||
| 13 | # You should have received a copy of the GNU General Public License | ||||||||||||||||||||||||||||||||
| 14 | # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | =encoding utf8 | ||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | Dpkg::Getopt - option parsing handling | ||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | This module provides helper functions for option parsing, and complements | ||||||||||||||||||||||||||||||||
| 25 | the system Getopt::Long module. | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | B<Note>: This is a private module, its API can change at any time. | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | =cut | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | package Dpkg::Getopt 0.02; | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 1 | ▶ | use v5.36; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | 1 | our @EXPORT = qw( | |||||||||||||||||||||||||||||||
| 36 | normalize_options | ||||||||||||||||||||||||||||||||
| 37 | parse_option_dir | ||||||||||||||||||||||||||||||||
| 38 | print_option_sep | ||||||||||||||||||||||||||||||||
| 39 | print_option_def | ||||||||||||||||||||||||||||||||
| 40 | print_option_env | ||||||||||||||||||||||||||||||||
| 41 | format_option_spec | ||||||||||||||||||||||||||||||||
| 42 | format_option_parts | ||||||||||||||||||||||||||||||||
| 43 | print_option | ||||||||||||||||||||||||||||||||
| 44 | print_version | ||||||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | 1 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 1 | ▶ | use Dpkg; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 50 | 1 | ▶ | use Dpkg::Color; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 51 | 1 | ▶ | use Dpkg::Gettext; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 52 | 1 | ▶ | use Dpkg::ErrorHandling; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | sub normalize_options | ||||||||||||||||||||||||||||||||
| 55 | 3 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
normalize_options: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 56 | 3 | my (%opts) = @_; | |||||||||||||||||||||||||||||||
| 57 | 3 | my $norm = 1; | |||||||||||||||||||||||||||||||
| 58 | 3 | my @args; | |||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | @args = map { | ||||||||||||||||||||||||||||||||
| 61 | 14 | ▶ | if ($norm and m/^(-[A-Za-z])(.+)$/) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$norm and /^(-[A-Za-z])(.+)$/u
MC/DC100%2/2
$norm and /^(-[A-Za-z])(.+)$/u
$norm
/^(-[A-Za-z])(.+)$/u
Truth table100%3/3
A and B
A= $normB= /^(-[A-Za-z])(.+)$/u
| |||||||||||||||||||||||||||||||||
| 62 | 3 | ($1, $2) | |||||||||||||||||||||||||||||||
| 63 | ▶ | } elsif ($norm and m/^(--[A-Za-z-]+)=(.*)$/) { | |||||||||||||||||||||||||||||||
|
Condition100%3/3
$norm and /^(--[A-Za-z-]+)=(.*)$/u
MC/DC100%2/2
$norm and /^(--[A-Za-z-]+)=(.*)$/u
$norm
/^(--[A-Za-z-]+)=(.*)$/u
Truth table100%3/3
A and B
A= $normB= /^(--[A-Za-z-]+)=(.*)$/u
| |||||||||||||||||||||||||||||||||
| 64 | 2 | ($1, $2) | |||||||||||||||||||||||||||||||
| 65 | } else { | ||||||||||||||||||||||||||||||||
| 66 | 9 | ▶ | $norm = 0 if defined $opts{delim} and $_ eq $opts{delim}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $opts{"delim"} and $_ eq $opts{"delim"}
MC/DC100%2/2
defined $opts{"delim"} and $_ eq $opts{"delim"}
defined $opts{"delim"}
$_ eq $opts{"delim"}
Truth table100%3/3
A and B
A= defined $opts{"delim"}B= $_ eq $opts{"delim"}
| |||||||||||||||||||||||||||||||||
| 67 | 9 | $_; | |||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | 3 | } @{$opts{args}}; | |||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 3 | return @args; | |||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub parse_option_dir($opt, $dir) | ||||||||||||||||||||||||||||||
|
Subroutine
parse_option_dir: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 75 | { | ||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if (! length $dir) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 77 | 0 | usageerr(g_('missing directory for option %s'), $opt); | |||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if (! -e $dir) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 81 | # TODO: Switch this warning into an error, after checking its impact. | ||||||||||||||||||||||||||||||||
| 82 | 0 | warning(g_('directory %s for %s does not exist'), $dir, $opt); | |||||||||||||||||||||||||||||||
| 83 | 0 | return; | |||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | if (! -d $dir) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 87 | 0 | usageerr(g_('argument %s for %s is not a directory'), $dir, $opt); | |||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | 0 | $dir =~ s{/+$}{}; | |||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | 0 | return $dir; | |||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | sub print_option_sep() | ||||||||||||||||||||||||||||||
|
Subroutine
print_option_sep: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 96 | { | ||||||||||||||||||||||||||||||||
| 97 | 0 | print "\n"; | |||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | # Indent the entries with 10 spaces, to cover 2 for the short option | ||||||||||||||||||||||||||||||||
| 102 | # indentation, 4 for the short option itself, and 4 for the long option. | ||||||||||||||||||||||||||||||||
| 103 | # | ||||||||||||||||||||||||||||||||
| 104 | # " -s, --short" | ||||||||||||||||||||||||||||||||
| 105 | # " Description for short.\n" | ||||||||||||||||||||||||||||||||
| 106 | 1 | ▶ | use constant OPTION_DESC_INDENT => 10; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub print_option_def($def) | ||||||||||||||||||||||||||||||
|
Subroutine
print_option_def: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 109 | { | ||||||||||||||||||||||||||||||||
| 110 | 0 | printf "%s[%s: %s]\n", ' ' x OPTION_DESC_INDENT, | |||||||||||||||||||||||||||||||
| 111 | C_('cli-options', 'default'), $def; | ||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | sub print_option_env($env) | ||||||||||||||||||||||||||||||
|
Subroutine
print_option_env: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 115 | { | ||||||||||||||||||||||||||||||||
| 116 | 0 | printf "%s[%s: %s=]\n", ' ' x OPTION_DESC_INDENT, | |||||||||||||||||||||||||||||||
| 117 | C_('cli-options', 'env'), $env; | ||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | sub format_option_spec($spec) | ||||||||||||||||||||||||||||||
|
Subroutine
format_option_spec: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 121 | { | ||||||||||||||||||||||||||||||||
| 122 | 0 | my $color_reset = color_get('reset'); | |||||||||||||||||||||||||||||||
| 123 | 0 | my $color_opt = color_get('bold'); | |||||||||||||||||||||||||||||||
| 124 | 0 | my $color_arg = color_get('italic'); | |||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | 0 | $spec =~ s{([][().|])}{$color_reset$1$color_opt}g; | |||||||||||||||||||||||||||||||
| 127 | 0 | $spec =~ s{<}{$color_reset<$color_arg}g; | |||||||||||||||||||||||||||||||
| 128 | 0 | $spec =~ s{>}{$color_reset>$color_opt}g; | |||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 0 | return "$color_opt$spec$color_reset"; | |||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | sub format_option_parts($spec, $help) | ||||||||||||||||||||||||||||||
|
Subroutine
format_option_parts: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 134 | { | ||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | my $indent_type = $spec =~ m{^--} ? 6 : 2; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 136 | 0 | my $indent_spec = ' ' x $indent_type; | |||||||||||||||||||||||||||||||
| 137 | 0 | my $indent_help = ' ' x 10; | |||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | 0 | $spec = format_option_spec($spec); | |||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | 0 | return sprintf "%s%s\n%s%s\n", $indent_spec, $spec, $indent_help, $help; | |||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | sub print_option($desc_fmt, @args) | ||||||||||||||||||||||||||||||
|
Subroutine
print_option: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 145 | { | ||||||||||||||||||||||||||||||||
| 146 | 0 | my $desc = sprintf $desc_fmt, @args; | |||||||||||||||||||||||||||||||
| 147 | 0 | my ($spec, $help) = split /\n/, $desc, 2; | |||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | 0 | printf format_option_spec($spec) . "\n$help"; | |||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | sub print_version() | ||||||||||||||||||||||||||||||
|
Subroutine
print_version: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 153 | { | ||||||||||||||||||||||||||||||||
| 154 | 0 | printf g_("%s version %s\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION; | |||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | =head1 CHANGES | ||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | =head2 Version 0.xx | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | This is a private module. | ||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | =cut | ||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | 1 | 1; | |||||||||||||||||||||||||||||||