File: 02_getopt_processing

package info (click to toggle)
stow 2.3.1-1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 2,040 kB
  • sloc: perl: 3,541; sh: 707; makefile: 165
file content (72 lines) | stat: -rw-r--r-- 2,174 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
From: Kalle Olavi Niemitalo <kon@iki.fi>
Date: Sun, 8 Sep 2019 10:57:26 -0700
Subject: Fix '--' getopt argument processing

This patch makes stow process command-line arguments that appear after
'--' as is customary for programs that use getopt.

Origin: other, http://bugs.debian.org/681752
Bug-Debian: http://bugs.debian.org/681752
Reviewed-By: Chuan-kai Lin <cklin@debian.org>
Last-Update: 2019-09-08
---
 bin/stow.in | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)

diff --git a/bin/stow.in b/bin/stow.in
index 86c0ae1..13ee74f 100755
--- a/bin/stow.in
+++ b/bin/stow.in
@@ -566,11 +566,25 @@ sub parse_options {
     my @pkgs_to_stow   = ();
     my $action = 'stow';
 
+    my @cli_args = @_;
+    my $remember_package_action = sub {
+        if ($action eq 'restow') {
+            push @pkgs_to_unstow, $_[0];
+            push @pkgs_to_stow, $_[0];
+        }
+        elsif ($action eq 'unstow') {
+            push @pkgs_to_unstow, $_[0];
+        }
+        else {
+            push @pkgs_to_stow, $_[0];
+        }
+    };
+
     #$,="\n"; print @_,"\n"; # for debugging rc file
 
     Getopt::Long::config('no_ignore_case', 'bundling', 'permute');
     GetOptionsFromArray(
-        \@_,
+        \@cli_args,
         \%options,
         'verbose|v:+', 'help|h', 'simulate|n|no',
         'version|V', 'compat|p', 'dir|d=s', 'target|t=s',
@@ -603,21 +617,12 @@ sub parse_options {
         'R|restow'  => sub { $action = 'restow' },
 
         # Handler for non-option arguments
-        '<>' =>
-        sub {
-            if ($action eq 'restow') {
-                push @pkgs_to_unstow, $_[0];
-                push @pkgs_to_stow, $_[0];
-            }
-            elsif ($action eq 'unstow') {
-                push @pkgs_to_unstow, $_[0];
-            }
-            else {
-                push @pkgs_to_stow, $_[0];
-            }
-        },
+        '<>' => $remember_package_action,
     ) or usage('');
 
+    # If GetOptions stopped at "--", process any remaining arguments.
+    $remember_package_action->($_) foreach @cli_args;
+
     usage()   if $options{help};
     version() if $options{version};