in reply to Parsing Windows CommandLine from Perl

Fired up ye olde VM, and found the solution. You see, you can write a batch script that does the parsing for you... it looks like this:

@ECHO OFF SetLocal EnableDelayedExpansion set ARGS= :parse_args if not %1.==. ( set ARG=%1 set ARGS=!ARGS! ^"!ARG:"=\"!^" shift goto :parse_args ) call perl getargs.pl %ARGS%

I rewrote the perscript to fit the output of your C code, but it is basically the same:

unshift(@ARGV,$0); for my $n (0..$#ARGV) { print "$n:$ARGV[$n]:\n" }

The resulting output is then:

C:\>parseargs.bat "abc "" def" bob 0:getargs.pl: 1:"abc "" def": 2:bob:

Which is not exactly the same what you wanted, but it is actually exactly what you typed in as arguments. Now, it is time to s/""/"/g and you are ready to go. What I mean is:

unshift(@ARGV,$0); @ARGV = map {s/^"(.*)"$/$1/ ? s/""/"/g && $_ : $_ } @ARGV; for my $n (0..$#ARGV) { print "$n:$ARGV[$n]:\n" }

Dont ask why I used map... I am not sober at the moment...

0:getargs.pl: 1:abc " def: 2:bob:

note to self: do not forget bob

Replies are listed 'Best First'.
Re^2: Parsing Windows CommandLine from Perl
by fishmonger (Chaplain) on Apr 17, 2015 at 20:30 UTC

    That's a long way around the bush just to avoid using a backslash in the command.

    c:\dev>type arg.pl #!/usr/bin/perl use 5.010; use warnings; use strict; my $i = 0; say $i++ . ":$_" for ($0, @ARGV);

    The resulting output:

    c:\dev>perl arg.pl "abc \" xyz" bob 0:arg.pl 1:abc " xyz 2:bob
Re^2: Parsing Windows CommandLine from Perl
by Anonymous Monk on Apr 17, 2015 at 20:53 UTC