#!/usr/bin/perl -w use strict; use Test::More qw(no_plan); while(my $args= ) { chomp $args; my @expected= map { chomp; $_ } `perl -e'print join "\n", \@ARGV' -- $args`; my @parsed= parse_args( $args); ok( eq_array( \@parsed, \@expected), $args) or diag( " expected: ", display_array( @expected), "\n", " got : ", display_array( @parsed), "\n" ); } sub parse_args { my $args= shift; my @args; while( $args=~ s{^\s* (?: '((?:\\.|[^'])+)' # ' quoted args |"((?:\\.|[^"])+)" # " quoted args |((?:\\.|\S)+) # bareword args )} {}x ) { # only one of $1, $2, $3 is defined, get it my $arg=defined $1 ? $1 : defined $2 ? $2 : defined $3 ? $3 : undef; $arg=~ s{\\}{}g; # unescape \ push @args, $arg; } return @args; } sub display_array { return '[empty]' unless @_; return "/" . join( '/ - /', @_), "/"; } __DATA__ -f --flag -f -l -fl -f val --file val -- file\ with\ spaces and more\ file\ with\ spaces 'an other "file name" here ' "and here " foo\ foo\ bar 'foo bar' 'foo bar' foo\ bar "foo bar" "foo \" bar" foo\ bar 'foo bar' 'foo bar' foo\ bar "foo bar" "foo \" bar" foobar - f val