#!/usr/bin/env perl use strict; use warnings; use Getopt::Long qw(GetOptionsFromString :config gnu_compat :config pass_through); foreach my $string ( q{--rsync-path = 'blah blah'}, # might be spaces before/after equal sign q{--rsync-path=/usr/bin/rsync}, # no quotes around value (assuming this allowed by rsync) q{--rsync-path="blah blah \"blah"}, # double quotes, with possible escaped quotes q{--rsync-path='blah blah \'blah'}, # single quotes, with possible escaped quotes q{--rsync-path='blah blah' --another-option}, # additional options might follow q{--another-option --rsync-path='blah blah'}, # additional options precede ) { my $tidy_string = $string =~ s/(--rsync-path)\s*=\s*/$1=/r; # This cleans up the whitespace around = in the first example. my $rsync_path; my ($ret, $args) = GetOptionsFromString( $tidy_string, 'rsync-path=s' => \$rsync_path, ); printf "%-48s: rsync_path => %-32s\n", "($string)" => "($rsync_path)"; } #### (--rsync-path = 'blah blah') : rsync_path => (blah blah) (--rsync-path=/usr/bin/rsync) : rsync_path => (/usr/bin/rsync) (--rsync-path="blah blah \"blah") : rsync_path => (blah blah "blah) (--rsync-path='blah blah \'blah') : rsync_path => (blah blah \'blah) (--rsync-path='blah blah' --another-option) : rsync_path => (blah blah) (--another-option --rsync-path='blah blah') : rsync_path => (blah blah)