diff options
author | Jean Delvare <jdelvare@suse.de> | 2011-03-15 13:45:38 +0100 |
---|---|---|
committer | Jean Delvare <jdelvare@suse.de> | 2011-03-15 13:45:38 +0100 |
commit | 1c86e6c9dedc9ada9c785194b6942a7240855077 (patch) | |
tree | 91482bba2e0da181206d4f7eac8073d5fe811d43 /compat | |
parent | eec8aa089709a1711c2ef97cc6c272e7577d88b5 (diff) | |
download | quilt-1c86e6c9dedc9ada9c785194b6942a7240855077.tar.gz |
compat/getopt.in: Fix quoting and spacing
This way the output matches exactly the output of getopt from
util-linux, and spaces and other special characters in filenames are
properly supported.
Diffstat (limited to 'compat')
-rw-r--r-- | compat/getopt.in | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/compat/getopt.in b/compat/getopt.in index 0bcf7b9..dfba72a 100644 --- a/compat/getopt.in +++ b/compat/getopt.in @@ -28,8 +28,8 @@ foreach my $arg (@ARGV) { # there is no reason to parse # the opts if there are no args. -if (! length(@words)) { - print ' -- '; +if (!@words) { + print " --\n"; exit; } @@ -49,22 +49,29 @@ if ($opts =~ /^\s*(?:-q\s+)?-o ([a-zA-Z:]*)?(\s+--long .*)*/) { } } -my @barewords; +my @barewords = ('--'); my @options; # set the previous option name when a param is required my $need_param; +sub quote_word +{ + my $word = shift; + $word =~ s/'/'\\''/; + return "'$word'"; +} + foreach my $word (@words) { # allow '-' to be an option value if (!$need_param && $word !~ /^-./) { - push @barewords, $word; + push @barewords, quote_word($word); next; } if ($need_param) { die "expecting param for $need_param" if $word =~ /^-./; - push @options, '"'.$word.'"'; + push @options, quote_word($word); $need_param = undef; next; } @@ -88,7 +95,7 @@ foreach my $word (@words) { while (scalar(@letters) && ($letters[$#letters] =~ /[0-9]/)) { push @digits, pop @letters; } - push @options, join('',reverse @digits); + push @options, quote_word(join('', reverse @digits)); } } } @@ -106,9 +113,9 @@ foreach my $word (@words) { $need_param = $word if $found =~ /:$/ && $param eq ''; push @options, "--$word"; - push @options, '"'."$param".'"' if $param; + push @options, quote_word($param) if $param; } } -print "@options -- @barewords" +print " @options @barewords\n" |