Quiq/Option.pm

filter: Statementstmt 85.7% 108 / 126 Branchbran 77.6% 59 / 76 Conditioncond 66.6% 12 / 18 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 80.8% 198 / 245 CC 45 SCAR 31.7
CC
45
Cov
81%
extract
33.4
extractMulti
20.3
extractAll
4.0
CRAP 23.8
« Quiq/Html/Widget/SelectMenu.pm ↑ Summary Quiq/Section/Object.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Option - Verarbeitung von Programm- und Methoden-Optionen
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =cut
14  
15 # -----------------------------------------------------------------------------
16  
17 package Quiq::Option;
18 270 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 270 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 270 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 270 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 270 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 270 our $VERSION = '1.240';
26  
27 270 ▶use Quiq::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 270 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # -----------------------------------------------------------------------------
31  
32 =head1 METHODS
33  
34 =head2 Klassenmethoden
35  
36 =head3 extract() - Extrahiere Optionen aus Argumentliste
37  
38 =head4 Synopsis
39  
40 $opt = $class->extract(@opt,\@args,@keyVal); # Options-Objekt
41 $class->extract(@opt,\@args,@keyVal); # Options-Variablen
42  
43 =head4 Options
44  
45 =over 4
46  
47 =item -dontExtract => $bool (Default: 0)
48  
49 Entferne die Optionen I<nicht> aus der Argumentliste.
50  
51 =item -mode => 'strict'|'sloppy' (Default: 'strict')
52  
53 Im Falle von C<< -mode=>'strict' >> (dem Default), wird eine Exception
54 ausgelöst, wenn eine unbekannte Option vorkommt. Im Falle von
55 C<< -mode=>'sloppy' >> wird das Argument stillschweigend übergangen.
56  
57 =item -properties => $bool (Default: 0)
58  
59 Argumentliste aus Schlüssel/Wert-Paaren, bei denen die Schlüssel
60 nicht mit einem Bindestrich (-) beginnen. Eine Unterscheidung
61 zwischen Optionen und Argumenten gibt es nicht.
62  
63 =item -simpleMessage => $bool (Default: 0)
64  
65 Wirf im Falle eines Fehlers eine einzeilige Fehlermeldung als Exception.
66  
67 =back
68  
69 =head4 Description
70  
71 Extrahiere die Optionen @keyVal aus der Argumentliste @args und
72 weise sie im Void-Kontext Variablen zu oder im Skalar-Kontext
73 einem Optionsobjekt.
74  
75 B<Schreibweisen für eine Option>
76  
77 Eine Option kann auf verschiedene Weisen angegeben werden.
78  
79 Als Programm-Optionen:
80  
81 --log-level=5 (ein Argument)
82 --logLevel=5 (mixed case)
83  
84 Als Methoden-Optionen:
85  
86 -log-level 5 (zwei Argumente)
87 -logLevel 5 (mixed case)
88  
89 Die Schreibweise mit zwei Bindestrichen wird typischerweise bei
90 Programmaufrufen angegeben. Die Option besteht aus I<einem> Argument,
91 bei dem der Wert durch ein Gleichheitszeichen vom Optionsnamen
92 getrennt angegeben ist.
93  
94 Die Schreibweise mit einem Bindestrich wird typischerweise bei
95 Methodenaufrufen angegeben. In Perl ist bei einem Bindestrich
96 kein Quoting nötig. Die Option besteht aus I<zwei> Argumenten.
97  
98 Beide Schreibweisen sind gleichberechtigt, so dass derselbe Code
99 sowohl Programm- als auch Methodenoptionen verarbeiten kann.
100  
101 Ist C<< -properties=>1 >> gesetzt, ist die Argumentliste eine
102 Abfolge Schlüssel/Wert-Paaren ohne Bindestrich als
103 Options-Kennzeichen:
104  
105 a 1 b 2 c 3
106  
107 =head4 Examples
108  
109 =over 2
110  
111 =item *
112  
113 Instantiierung eines Options-Objekts:
114  
115 sub meth {
116 my $self = shift;
117 # @_: @args
118
119 my $opt = Quiq::Option->extract(\@_,
120 -logLevel => 1,
121 -verbose => 0,
122 );
123 ...
124
125 if ($opt->verbose) {
126 ...
127 }
128 }
129  
130 Das gelieferte Options-Objekt $opt ist eine Quiq::Hash-Instanz.
131 Die Schlüssel haben keine führenden Bindestriche. Eine Abfrage
132 des Optionswerts ist per Methode möglich:
133  
134 $verbose = $opt->verbose;
135  
136 =item *
137  
138 Setzen von Options-Variablen:
139  
140 sub meth {
141 my $self = shift;
142 # @_: @args
143
144 my $logLevel = 1;
145 my $verbose = 0;
146
147 Quiq::Option->extract(\@_,
148 -logLevel => \$logLevel,
149 -verbose => \$verbose,
150 );
151 ...
152
153 if ($verbose) {
154 ...
155 }
156 }
157  
158 =item *
159  
160 Optionen bei Programmaufruf:
161  
162 $ prog --log-level=2 --verbose file.txt
163  
164 =item *
165  
166 Optionen bei Methodenaufruf:
167  
168 $prog->begin(-logLevel=>2,-verbose=>1,'file.txt');
169  
170 =item *
171  
172 Abfrage einer Option:
173  
174 $logLevel = $opt->logLevel;
175  
176 =item *
177  
178 Abfrage mehrerer Optionen:
179  
180 ($verbose,$logLevel) = $opt->get('verbose','logLevel');
181  
182 =back
183  
184 =cut
185  
186 # -----------------------------------------------------------------------------
187  
188 741 ▶sub extract {
Subroutine extract: called CC=30 SCAR=35.1
Pod documented
189 741 my $class = shift;
190 # @_: @opt,$argA,@optVal
191  
192 # Verarbeitungs-Optionen
193  
194 741 my $dontExtract = 0;
195 741 my $mode = 'strict';
196 741 my $properties = 0;
197 741 my $simpleMessage = 0;
198  
199 741 ▶ while (!ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[0]) 194 741 935
200 194 my $key = shift;
201  
202 194 ▶ if ($key eq '-mode') {
Branch: 4 branches
BranchTrueFalseTotal
if ($key eq '-mode') { } 154 40 194
elsif ($key eq '-properties') { } 1 39 40
elsif ($key eq '-simpleMessage') { } 0 39 39
elsif ($key eq '-dontExtract') { } 39 0 39
203 154 $mode = shift;
204 }
205 elsif ($key eq '-properties') {
206 1 $properties = shift;
207 }
208 elsif ($key eq '-simpleMessage') {
209 0 $simpleMessage = shift;
210 }
211 elsif ($key eq '-dontExtract') {
212 39 $dontExtract = shift;
213 }
214 else {
215 0 ▶ if ($simpleMessage) {
Branch: 1 branch
BranchTrueFalseTotal
if ($simpleMessage) 0 0 0
216 0 die "Ungültige Methodenoption: $key\n";
217 }
218 $class->throw(
219 0 'OPT-00002: Ungültige Methodenoption',
220 Option => $key,
221 );
222 }
223 }
224 741 my $argA = shift;
225  
226 # Wir sind im VarMode, wenn die Methode im Void-Kontext gerufen wird.
227 # Im VarMode enthält der Options-Hash Referenzen auf Programm-Variablen,
228 # auf die wir die Optionswerte schreiben, nicht die Options-Defaultwerte.
229 # Im VarMode können wir sofort zurückkehren, wenn die Argumentliste
230 # leer ist.
231  
232 741 ▶ my $varMode = defined wantarray? 0: 1;
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 8 733 741
233 741 ▶ if ($varMode && !@$argA) {
Branch: 1 branch
BranchTrueFalseTotal
if ($varMode and not @$argA) 173 568 741
Condition100%3/3
$varMode and not @$argA
!ll&&!rl&&r
8560173
MC/DC100%2/2
$varMode and not @$argA
$varMode not @$argA
Truth table100%3/3
A and not(B)
A= $varModeB= not @$argA
ABresult
0X0
100
111
234 173 return;
235 }
236  
237 # Options-Hash initialisieren
238  
239 568 my %opt;
240 568 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3177 568 3745
241 3177 my $key = shift;
242 3177 $key =~ s/^-+//; # Optionsnamen ohne führende Bindestriche
243 3177 $opt{$key} = shift;
244 }
245  
246 # Argumente auswerten und auf Options-Hash schreiben
247  
248 568 my $i = 0;
249 568 ▶ while ($i < @$argA) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$argA) 1322 568 1890
250 1322 my ($key,$dashPrefix,$val,$skip);
251  
252 1322 $key = $argA->[$i];
253 1322 ▶ $dashPrefix = defined($key) && $key =~ s/^(-+)//? $1: '';
Branch: 1 branch
BranchTrueFalseTotal
defined $key && $key =~ s/^(-+)// ? : 796 526 1322
Condition66%2/3
defined $key and $key =~ s/^(-+)//
!ll&&!rl&&r
0526796
MC/DC50%1/2
defined $key and $key =~ s/^(-+)//
defined $key $key =~ s/^(-+)//
Truth table66%2/3
A and B
A= defined $keyB= $key =~ s/^(-+)//
ABresult
0X0
100
111
254  
255 1322 ▶ if ($properties) {
Branch: 3 branches
BranchTrueFalseTotal
if ($properties) { } 3 1319 1322
elsif ($dashPrefix eq '--') { } 14 1305 1319
elsif ($dashPrefix eq '-' and $key ne '') { } 782 523 1305
256 # Property-Liste: KEY,VAL
257  
258 3 $val = $argA->[$i+1];
259 3 $skip = 2;
260 }
261 elsif ($dashPrefix eq '--') {
262 # Programm-Option: --KEY=VAL
263  
264 14 ▶ if ($key eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "") 0 14 14
265 # Ende der Optionsliste, Option '--'
266 0 splice @$argA,$i,1;
267 0 last;
268 }
269 14 ($key,$val) = split /=/,$key,2;
270 14 ▶ if (!defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 7 7 14
271 7 $val = 1;
272 }
273 14 $skip = 1;
274 }
275 ▶ elsif ($dashPrefix eq '-' && $key ne '') { # '-' ist normaler Wert
Condition66%2/3
$dashPrefix eq "-" and $key ne ""
!ll&&!rl&&r
5230782
MC/DC50%1/2
$dashPrefix eq "-" and $key ne ""
$dashPrefix eq "-" $key ne ""
Truth table66%2/3
A and B
A= $dashPrefix eq "-"B= $key ne ""
ABresult
0X0
100
111
276 # Methoden-Option: -KEY,VAL
277  
278 782 ▶ if ($key eq 'h' && exists $opt{'help'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'h' and exists $opt{'help'}) { } 1 781 782
Condition66%2/3
$key eq "h" and exists $opt{"help"}
!ll&&!rl&&r
78101
MC/DC50%1/2
$key eq "h" and exists $opt{"help"}
$key eq "h" exists $opt{"help"}
Truth table66%2/3
A and B
A= $key eq "h"B= exists $opt{"help"}
ABresult
0X0
100
111
279 1 $key = 'help';
280 1 $val = 1;
281 1 $skip = 1;
282 }
283 else {
284 781 $val = $argA->[$i+1];
285 781 $skip = 2;
286 }
287 }
288 else {
289 # Keine Option, weitergehen
290 523 $i++;
291 523 next;
292 }
293  
294 799 ▶ if (!$properties) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($properties) 796 3 799
295 # eingebettete Bindestriche in CamelCase-Schreibweise wandeln
296 796 $key =~ s/(.)-(.)/$1\U$2/g;
297 }
298  
299 # warn "KEY=$key VAL=$val\n";
300  
301 # Existenz des Key prüfen
302  
303 799 ▶ if (!exists $opt{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opt{$key}) 116 683 799
304 116 ▶ if ($mode eq 'sloppy') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "sloppy") 116 0 116
305 116 $i += $skip;
306 116 next;
307 }
308 0 ▶ if ($simpleMessage) {
Branch: 1 branch
BranchTrueFalseTotal
if ($simpleMessage) 0 0 0
309 0 die "Ungültige Option: $dashPrefix$key\n";
310 }
311 $class->throw(
312 0 'OPT-00001: Unknown option',
313 Option => "$dashPrefix$key",
314 );
315 };
316  
317 # Optionswert setzen
318  
319 683 ▶ if (defined $val) { # Defaultwert bleibt. NEU! 27.7.2014
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 521 162 683
320 521 ▶ if ($varMode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($varMode) { } 505 16 521
321 505 ${$opt{$key}} = $val;
322 }
323 else {
324 16 $opt{$key} = $val;
325 }
326 }
327  
328 683 ▶ if ($dontExtract) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dontExtract) { } 163 520 683
329 # Optionen bleiben in Argumentliste, Index weiterschalten
330 163 $i += $skip;
331 }
332 else {
333 # Option und Wert aus Argumentliste entfernen
334 520 splice @$argA,$i,$skip;
335 }
336 }
337  
338 568 ▶ if ($varMode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($varMode) { } 560 8 568
339 560 return;
340 }
341 else {
342 8 return Quiq::Hash->new(\%opt);
343 }
344 }
345  
346 # -----------------------------------------------------------------------------
347  
348 =head3 extractAll() - Extrahiere alle Optionen als Liste
349  
350 =head4 Synopsis
351  
352 @opts|$optA = $class->extractAll(\@arr);
353  
354 =head4 Description
355  
356 Extrahiere alle Option/Wert-Paare aus @arr und liefere diese als Liste
357 zurück. Im Skalarkontext liefere eine Referenz auf die Liste.
358  
359 =cut
360  
361 # -----------------------------------------------------------------------------
362  
363 1 ▶sub extractAll {
Subroutine extractAll: called CC=4 SCAR=13.9
Pod documented
364 1 my ($this,$argA) = @_;
365  
366 1 my @arr;
367 1 ▶ for (my $i = 0; $i < @$argA; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$argA) 7 1 8
368 7 ▶ if (substr($argA->[$i],0,1) eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($argA->[$i], 0, 1) eq "-") 2 5 7
369 2 push @arr,splice(@$argA,$i--,2);
370 }
371 }
372  
373 1 ▶ return wantarray? @arr: \@arr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
374 }
375  
376 # -----------------------------------------------------------------------------
377  
378 =head3 extractMulti() - Extrahiere Mehrwert-Optionen
379  
380 =head4 Synopsis
381  
382 $class->extractMulti(@opt,\@arr,$key=>$ref,...);
383  
384 =head4 Options
385  
386 =over 4
387  
388 =item -sloppy => $bool (Default: 0)
389  
390 Übergehe unbekannte Optionen.
391  
392 =back
393  
394 =head4 Description
395  
396 Extrahiere aus Array @arr die Werte zu den angegebenen Schlüsseln
397 $key und weise diese an die Variablen-Referenzen $ref zu.
398 Die Methode liefert keinen Wert zurück.
399  
400 Eine Referenz kann eine Skalar- oder eine Arrayreferenz sein.
401 Im Fall einer Skalarreferenz wird der Wert zugewiesen.
402 Im Falle einer Array-Referenz werden mehrere aufeinanderfolgende
403 Werte in dem Array gesammelt.
404  
405 Für das Hinzufügen einer Default-Option, siehe Beispiel.
406  
407 =head4 Example
408  
409 # Optionen
410
411 my @select;
412 my @from;
413 my @where;
414 my $limit;
415
416 unshift @_,'-select'; # Default-Option
417
418 Quiq::Option->extractMulti(\@_,
419 -select => \@select,
420 -from => \@from,
421 -where => \@where,
422 -limit => \$limit,
423 );
424
425 unless (@from) {
426 die "FROM-Klausel fehlt\n";
427 }
428 unless (@select) {
429 @select = ('*');
430 }
431  
432 =cut
433  
434 # -----------------------------------------------------------------------------
435  
436 97 ▶sub extractMulti {
Subroutine extractMulti: called CC=13 SCAR=30.1
Pod documented
437 97 my $this = shift;
438 # @_: @opt,\@arr,@keyRef
439  
440 # Optionen
441  
442 97 my $sloppy = 0;
443  
444 97 ▶ while (!ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[0]) 0 97 97
445 0 my $key = shift;
446 0 ▶ if ($key eq '-sloppy') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq '-sloppy') { } 0 0 0
447 0 $sloppy = shift;
448 }
449 else {
450 0 $this->throw(
451 'OPT-00004: Ungültige Option',
452 Option => $key,
453 );
454 }
455 }
456  
457 97 my $arr = shift;
458 97 my %keyVar = @_;
459  
460 97 my ($ref,$refType);
461 97 ▶ for (my $i = 0; $i < @$arr; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$arr) 487 97 584
462 487 my $arg = $arr->[$i];
463  
464 487 ▶ if (defined($arg) && exists $keyVar{$arg}) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $arg and exists $keyVar{$arg}) { } 220 267 487
elsif (defined $arg and $arg =~ /^-\D/) { } 0 267 267
elsif ($ref) { } 252 15 267
Condition66%2/3
defined $arg and exists $keyVar{$arg}
!ll&&!rl&&r
0267220
MC/DC50%1/2
defined $arg and exists $keyVar{$arg}
defined $arg exists $keyVar{$arg}
Truth table66%2/3
A and B
A= defined $argB= exists $keyVar{$arg}
ABresult
0X0
100
111
465 # Argument ist Option
466  
467 220 $ref = $keyVar{$arg};
468 220 $refType = Scalar::Util::reftype($ref);
469 220 ▶ if (!defined $refType) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $refType) 0 220 220
470 0 $this->throw(
471 'OPT-00002: Ungültige Variablen-Referenz',
472 Option => $arg,
473 );
474 }
475 220 splice @$arr,$i--,1;
476 220 next;
477 }
478 # Zahlen beginnen können mit '-' beginnen
479 ▶ elsif (defined($arg) && $arg =~ /^-\D/) {
Condition33%1/3
defined $arg and $arg =~ /^-\D/
!ll&&!rl&&r
02670
MC/DC0%0/2
defined $arg and $arg =~ /^-\D/
defined $arg $arg =~ /^-\D/
Truth table33%1/3
A and B
A= defined $argB= $arg =~ /^-\D/
ABresult
0X0
100
111
480 # Unbekannte Option
481  
482 0 ▶ if (!$sloppy) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sloppy) 0 0 0
483 0 $this->throw(
484 'OPT-00001: Unknown option',
485 Option => $arg);
486 }
487 0 $ref = undef;
488 0 next;
489 }
490 elsif ($ref) {
491 # Argument ist Wert zu Option
492  
493 252 ▶ if ($refType eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($refType eq 'ARRAY') { } 205 47 252
elsif ($refType eq 'SCALAR') { } 47 0 47
494 205 push @$ref,$arg;
495 }
496 elsif ($refType eq 'SCALAR') {
497 47 $$ref = $arg;
498 47 $ref = undef;
499 }
500 252 splice @$arr,$i--,1;
501 252 next;
502 }
503 else {
504 # Unbekanntes Argument -> übergehen
505 15 next;
506 }
507 }
508  
509 97 return;
510 }
511  
512 # -----------------------------------------------------------------------------
513  
514 =head1 VERSION
515  
516 1.240
517  
518 =head1 AUTHOR
519  
520 Frank Seitz, L<http://fseitz.de/>
521  
522 =head1 COPYRIGHT
523  
524 Copyright (C) 2026 Frank Seitz
525  
526 =head1 LICENSE
527  
528 This code is free software; you can redistribute it and/or modify
529 it under the same terms as Perl itself.
530  
531 =cut
532  
533 # -----------------------------------------------------------------------------
534  
535 270 1;
536  
537 # eof
« Quiq/Html/Widget/SelectMenu.pm ↑ Summary Quiq/Section/Object.pm »