Quiq/CommandLine.pm

filter: Statementstmt 98.6% 73 / 74 Branchbran 68.7% 22 / 32 Conditioncond 52.9% 9 / 17 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 100% 13 / 13 Podpod 100% 8 / 8 totaltotal 82.5% 128 / 155 CC 23 SCAR 13.9
CC
23
Cov
85%
addArgument
5.5
addString
5.3
addOption
5.2
CRAP 4.0
« Quiq/AnsiColor.pm ↑ Summary Quiq/JQuery/Tabs.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::CommandLine - Erstelle eine Unix-Kommandozeile
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 SYNOPSIS
14  
15 use Quiq::CommandLine;
16
17 my $c = Quiq::CommandLine->new('iconv');
18 $c->addOption(
19 -f => 'utf-8',
20 -t => 'latin1',
21 );
22 $c->addString('|','enscript');
23 $c->addBoolOption(
24 '--no-header' => 1,
25 '--landscape' => 1,
26 );
27 $c->addLongOption(
28 '--font' => 'Courier8',
29 );
30 $c->addString('2>/dev/null','|','ps2pdf','-');
31 $c->addArgument('/tmp/test.pdf');
32
33 my $cmd = $c->command;
34 __END__
35 iconv -f utf-8 -t latin1 | enscript --no-header --landscape --font=Courier8 2>/dev/null | ps2pdf - /tmp/test.pdf
36  
37 =head1 DESCRIPTION
38  
39 Die Klasse stellt Methoden zur Konstruktion einer Unix-Kommandozeile
40 zur Verfügung. Die Klasse ist hilfreich, wenn einzelne Bestandteile
41 der Kommandozeile dynamisch sind, also von Bedingungen und
42 variablen Werten abhängen.
43  
44 =cut
45  
46 # -----------------------------------------------------------------------------
47  
48 package Quiq::CommandLine;
49 5 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 5 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 5 our $VERSION = '1.240';
56  
57 # -----------------------------------------------------------------------------
58  
59 =head1 METHODS
60  
61 =head2 Konstruktor
62  
63 =head3 new() - Konstruktor
64  
65 =head4 Synopsis
66  
67 $c = $class->new;
68 $c = $class->new($str);
69  
70 =head4 Arguments
71  
72 =over 4
73  
74 =item $str
75  
76 Anfang der Kommandozeile.
77  
78 =back
79  
80 =head4 Returns
81  
82 Kommandozeilen-Objekt
83  
84 =head4 Description
85  
86 Instantiiere ein Kommandozeilen-Objekt und liefere eine Referenz
87 auf dieses Objekt zurück. Mit $str kann der Anfang der Kommandozeile
88 festgelegt werden.
89  
90 =head4 Example
91  
92 Erzeuge eine Kommandozeile für das Kommando C<enscript>:
93  
94 $c = Quiq::CommandLine->new('enscript');
95  
96 =cut
97  
98 # -----------------------------------------------------------------------------
99  
100 2 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
101 2 my $class = shift;
102 2 ▶ my $cmd = shift // '';
Condition100%2/2
shift() // ""
l!l
11
MC/DC100%1/1
shift() // ""
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
103  
104 # Instantiiere Objekt
105
106 2 return $class->SUPER::new(
107 cmd => $cmd,
108 );
109 }
110
111  
112 # -----------------------------------------------------------------------------
113  
114 =head2 Kommandozeilenbestandteile hinzufügen
115  
116 =head3 addArgument() - Ergänze Argumente
117  
118 =head4 Synopsis
119  
120 $cmd->addArgument(@args);
121  
122 =head4 Arguments
123  
124 =over 4
125  
126 =item @args
127  
128 Liste von Kommandozeilenargumenten.
129  
130 =back
131  
132 =head4 Returns
133  
134 nichts
135  
136 =head4 Description
137  
138 Ergänze die Kommandozeile um 0, 1 oder mehr Argumente. Leerzeichen
139 werden vor jedem Argument hinzugefügt. Enthält ein Argument
140 Leerzeichen oder Shell-Metazeichen, wird es Quotes eingefasst.
141 Ist ein Argument leer (undef oder ''), wird ein Leerstring zur
142 Kommandozeile hinzugefügt.
143  
144 =head4 Example
145  
146 $c->addArgument("/tmp/preview-$$.pdf",'',"Dies ist ein Test");
147  
148 ergänzt die Kommandozeile um die drei Argumente
149  
150 ... /tmp/preview-4711.pdf '' 'Dies ist ein Test'
151  
152 =cut
153  
154 # -----------------------------------------------------------------------------
155  
156 1 ▶sub addArgument {
Subroutine addArgument: called CC=5 SCAR=17.0
Pod documented
157 1 my $self = shift;
158 # @_: @args
159  
160 1 my $ref = $self->getRef('cmd');
161
162 1 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
163 1 my $arg = $self->value(shift);
164  
165 # Leere Argumente sind erlaubt
166  
167 1 ▶ if (!defined($arg) || $arg eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $arg or $arg eq "") 0 1 1
Condition33%1/3
not defined $arg or $arg eq ""
l!l&&r!l&&!r
001
MC/DC0%0/2
not defined $arg or $arg eq ""
not defined $arg $arg eq ""
Truth table33%1/3
not(A) or B
A= not defined $argB= $arg eq ""
ABresult
1X1
011
000
168 0 $arg = "''";
169 }
170  
171 1 ▶ if (length $$ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $$ref) 1 0 1
172 1 $$ref .= ' ';
173 }
174 1 $$ref .= $arg;
175 }
176  
177 1 return;
178 }
179  
180 # -----------------------------------------------------------------------------
181  
182 =head3 addBoolOption() - Ergänze boolsche Optionen
183  
184 =head4 Synopsis
185  
186 $cmd->addBoolOption(@boolOptions);
187  
188 =head4 Arguments
189  
190 =over 4
191  
192 =item @boolOptions
193  
194 Liste von boolschen Optionen, bestehend jeweils aus der Option und
195 zugehörigem Prädikat.
196  
197 =back
198  
199 =head4 Returns
200  
201 nichts
202  
203 =head4 Description
204  
205 Ergänze die Kommandozeile um 0, 1 oder mehr boolsche Optionen.
206 Eine boolsche Option ist eine Option, die keinen Wert hat,
207 sondern vorhanden ist oder nicht, was durch das zugeörige
208 Prädikat bestimmt wird (siehe Beispiel).
209  
210 =head4 Example
211  
212 $c->addBoolOption(
213 '--no-header' => 1,
214 '--landscape' => 0,
215 '--truncate-lines' => 1,
216 );
217  
218 ergänzt die Kommandozeile um die Optionen C<--no-header> und
219 C<--truncate-lines>, aber nicht um die Option C<--landscape>
220  
221 ... --no-header --truncate-lines
222  
223 =cut
224  
225 # -----------------------------------------------------------------------------
226  
227 1 ▶sub addBoolOption {
Subroutine addBoolOption: called CC=4 SCAR=14.0
Pod documented
228 1 my $self = shift;
229 # @_: @optBool
230  
231 1 my $ref = $self->getRef('cmd');
232
233 1 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
234 1 my $opt = shift;
235 1 ▶ if (shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (shift()) 1 0 1
236 1 ▶ if (length $$ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $$ref) 1 0 1
237 1 $$ref .= ' ';
238 }
239 1 $$ref .= $opt;
240 }
241 }
242  
243 1 return;
244 }
245  
246 # -----------------------------------------------------------------------------
247  
248 =head3 addOption() - Ergänze Option und ihre Werte
249  
250 =head4 Synopsis
251  
252 $cmd->addOption(@optVal);
253  
254 =head4 Arguments
255  
256 =over 4
257  
258 =item @optVal
259  
260 Liste von Optionen, bestehend jeweils aus der Option und
261 zugehörigem Wert.
262  
263 =back
264  
265 =head4 Returns
266  
267 nichts
268  
269 =head4 Description
270  
271 Ergänze die Kommandozeile um 0, 1 oder mehr Optionen mit
272 zugehörigem Wert. Option und Wert werden durch ein Leerzeichen
273 getrennt.
274  
275 =cut
276  
277 # -----------------------------------------------------------------------------
278  
279 1 ▶sub addOption {
Subroutine addOption: called CC=5 SCAR=16.6
Pod documented
280 1 my $self = shift;
281 # @_: @optVal
282  
283 1 my $ref = $self->getRef('cmd');
284
285 1 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 1 3
286 2 my $opt = shift;
287 2 my $val = shift;
288  
289 # Wir fügen das Option/Wert-Paar nur hinzu, wenn der Wert
290 # definiert und kein Leerstring ist
291  
292 2 ▶ if (defined($val) && $val ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and $val ne "") 2 0 2
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
002
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
293 2 ▶ if (length $$ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $$ref) 2 0 2
294 2 $$ref .= ' ';
295 }
296 2 $$ref .= $opt.' '.$self->value($val);
297 }
298 }
299  
300 1 return;
301 }
302  
303 # -----------------------------------------------------------------------------
304  
305 =head3 addEqOption() - Ergänze Option und ihre Werte
306  
307 =head4 Synopsis
308  
309 $cmd->addEqOption(@optVal);
310  
311 =head4 Alias
312  
313 addLongOption()
314  
315 =head4 Arguments
316  
317 =over 4
318  
319 =item @optVal
320  
321 Liste von Optionen, bestehend jeweils aus der Option und
322 zugehörigem Wert.
323  
324 =back
325  
326 =head4 Returns
327  
328 nichts
329  
330 =head4 Description
331  
332 Ergänze die Kommandozeile um 0, 1 oder mehr Optionen mit
333 zugehörigem Wert. Option und Wert werden durch ein
334 Gleichheitszeichen (=) getrennt.
335  
336 =head4 Example
337  
338 $c->addEqOption(
339 '--columns' => 2,
340 '--font' => 'Courier10',
341 '--margins' => '0:0:0:0',
342 );
343  
344 ergänzt die Kommandozeile um die Optionen
345  
346 ... --columns=2 --font=Courier10 --margins=0:0:0:0
347  
348 =cut
349  
350 # -----------------------------------------------------------------------------
351  
352 1 ▶sub addEqOption {
Subroutine addEqOption: called CC=5 SCAR=16.6
Pod documented
353 1 my $self = shift;
354 # @_: @optVal
355  
356 1 my $ref = $self->getRef('cmd');
357
358 1 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
359 1 my $opt = shift;
360 1 my $val = $self->value(shift);
361  
362 # Wir fügen das Option/Wert-Paar nur hinzu, wenn der Wert
363 # definiert und kein Leerstring ist
364  
365 1 ▶ if (defined($val) && $val ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and $val ne "") 1 0 1
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
001
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
366 1 ▶ if (length $$ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $$ref) 1 0 1
367 1 $$ref .= ' ';
368 }
369 1 $$ref .= $opt.'='.$val;
370 }
371 }
372  
373 1 return;
374 }
375  
376 {
377 5 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
378 5 *addLongOption = \&addEqOption;
379 }
380  
381 # -----------------------------------------------------------------------------
382  
383 =head3 addString() - Ergänze Zeichenketten
384  
385 =head4 Synopsis
386  
387 $cmd->addString(@strings);
388  
389 =head4 Arguments
390  
391 =over 4
392  
393 =item @strings
394  
395 Liste von Zeichenketten.
396  
397 =back
398  
399 =head4 Returns
400  
401 nichts
402  
403 =head4 Description
404  
405 Ergänze die Kommandozeile um 0, 1 oder mehr Zeichenketten.
406 Diese werden, mit Whitespace getrennt, unverändert zur
407 Kommandozeile hinzugefügt.
408  
409 =head4 Example
410  
411 $c->addString('2>/dev/null','|','ps2pdf','-','-');
412  
413 ergänzt die Kommandozeile um
414  
415 ... 2>/dev/null | ps2pdf - -
416  
417 =cut
418  
419 # -----------------------------------------------------------------------------
420  
421 2 ▶sub addString {
Subroutine addString: called CC=5 SCAR=16.6
Pod documented
422 2 my $self = shift;
423 # @_: @strings
424  
425 2 my $ref = $self->getRef('cmd');
426
427 2 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 2 8
428 6 my $str = shift;
429
430 # Wir fügen den String nur hinzu, wenn der Wert
431 # definiert und kein Leerstring ist
432  
433 6 ▶ if (defined($str) && $str ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str and $str ne "") 6 0 6
Condition33%1/3
defined $str and $str ne ""
!ll&&!rl&&r
006
MC/DC0%0/2
defined $str and $str ne ""
defined $str $str ne ""
Truth table33%1/3
A and B
A= defined $strB= $str ne ""
ABresult
0X0
100
111
434 6 ▶ if (length $$ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $$ref) 6 0 6
435 6 $$ref .= ' ';
436 }
437 6 $$ref .= $str;
438 }
439 }
440  
441 2 return;
442 }
443  
444 # -----------------------------------------------------------------------------
445  
446 =head2 Kommandozeile
447  
448 =head3 command() - Liefere Kommandozeile
449  
450 =head4 Synopsis
451  
452 $cmd = $c->command;
453  
454 =head4 Returns
455  
456 Kommandozeile (String)
457  
458 =head4 Description
459  
460 Liefere die Kommandozeile als Zeichenkette, wie sie z.B. von der
461 Shell ausgeführt werden kann.
462  
463 =cut
464  
465 # -----------------------------------------------------------------------------
466  
467 1 ▶sub command {
Subroutine command: called CC=1 SCAR=0.0
Pod documented
468 1 return shift->{'cmd'};
469 }
470  
471 # -----------------------------------------------------------------------------
472  
473 =head2 Hilfsmethoden
474  
475 =head3 value() - Liefere Options- oder Argumentwert
476  
477 =head4 Synopsis
478  
479 $str2 = $this->value($str1);
480  
481 =head4 Arguments
482  
483 =over 4
484  
485 =item $str1
486  
487 Zeichenkette.
488  
489 =back
490  
491 =head4 Returns
492  
493 Shellverträglichen Wert (String)
494  
495 =head4 Description
496  
497 Analysiere Zeichenkette $str1 auf Whitespace und Shell-Metazeichen
498 hin und liefere einen Wert, der gefahrlos als Optionswert oder
499 Programmargument zur Kommandozeile hinzugefügt werden kann.
500  
501 =head4 Examples
502  
503 Undef:
504  
505 $c->value(undef);
506 =>
507 undef
508  
509 Leerstring:
510  
511 $c->value("");
512 =>
513 ''
514  
515 Zeichenkette ohne Whitespace oder Shell-Metazeichen:
516  
517 $c->value("/tmp/test.pdf");
518 =>
519 /tmp/test.pdf
520  
521 Zeichenkette mit Whitespace und/oder Shell-Metazeichen:
522  
523 $c->value("Dies ist ein Test");
524 =>
525 'Dies ist ein Test'
526  
527 =cut
528  
529 # -----------------------------------------------------------------------------
530  
531 6 ▶sub value {
Subroutine value: called CC=3 SCAR=11.0
Pod documented
532 6 my ($this,$str) = @_;
533  
534 6 ▶ if (!defined($str) || $str =~ m|^([\w/:+-.~]+)$|) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $str or $str =~ m[^([\w/:+-.~]+)$]) 5 1 6
Condition100%3/3
not defined $str or $str =~ m[^([\w/:+-.~]+)$]
l!l&&r!l&&!r
141
MC/DC100%2/2
not defined $str or $str =~ m[^([\w/:+-.~]+)$]
not defined $str $str =~ m[^([\w/:+-.~]+)$]
Truth table100%3/3
not(A) or B
A= not defined $strB= $str =~ m[^([\w/:+-.~]+)$]
ABresult
1X1
011
000
535 5 return $str;
536 }
537
538 1 return "'$str'";
539 }
540  
541 # -----------------------------------------------------------------------------
542  
543 =head1 VERSION
544  
545 1.240
546  
547 =head1 AUTHOR
548  
549 Frank Seitz, L<http://fseitz.de/>
550  
551 =head1 COPYRIGHT
552  
553 Copyright (C) 2026 Frank Seitz
554  
555 =head1 LICENSE
556  
557 This code is free software; you can redistribute it and/or modify
558 it under the same terms as Perl itself.
559  
560 =cut
561  
562 # -----------------------------------------------------------------------------
563  
564 5 1;
565  
566 # eof
« Quiq/AnsiColor.pm ↑ Summary Quiq/JQuery/Tabs.pm »