parsing string, as if it were a command line

N

nickelstat

Hi, I'm looking for codes/modules that parse exactly like the command
line parser
(otherwise, I'll do the work myself)

Here is the story

When this command below is executed

perlScript -k b='this has blanks' -x "escape \" the quote"


I get $ARGV[0] = -k
[1] = b='this has blanks'
[2] = -x
[3] = escape " the quote

Note: this is one example. Hairy situations may be a combination of
lots of blanks and quotes and # variations.

Now I want to achieve the same thing, but the arguments are in a string

$string=qq# -k b='this has blanks' -x "escape \" the quote"#;

If not for the quotes and the blanks, I could do @ARGV = split /\s+/
but those blanks create problems

(I put double quotes in the value to show empty arguments
ARGV[0]=""
ARGV[1]="-k"
ARGV[2]="b='this"
ARGV[3]="has"
ARGV[4]="blanks'"
ARGV[5]="-x"
ARGV[6]=""escape"
ARGV[7]="""
ARGV[8]="the"
ARGV[9]="quote""

Thanks in advance
 
N

nickelstat

Understand Getopt assumes you use the command line.

Here I have a string, inside a script, that I want parsed to get @ARGV

Paul, I'll look into your suggestion.

And thanks guys for the very speedy answers. Just as if you were
sitting next to me.
 
G

Gunnar Hjalmarsson

Hi, I'm looking for codes/modules that parse exactly like the command
line parser

See Paul's suggestion.
$string=qq# -k b='this has blanks' -x "escape \" the quote"#;
----------^^

You didn't mean to use qq// there, you meant to use the q// operator.
 
N

nickelstat

Thanks

I used &shellwords() and it does what I want.

Saves me lots of time.

Except that it didn't like leading blanks -> it gives me a blank as the
first token.

Really appreciate your inputs.
 
J

Jürgen Exner

Hi, I'm looking for codes/modules that parse exactly like the command
line parser

That will probably be _very_ challenging because each of the many, many
"command line parsers" has its own way of parsing the command line.
Ever noticed the differences between e.g. sh, bash, tcsh, cmd.exe, ....

jue
 
E

Eric J. Roode

(e-mail address removed) wrote in @g44g2000cwa.googlegroups.com:
Hi, I'm looking for codes/modules that parse exactly like the command
line parser
(otherwise, I'll do the work myself)

Which command-line parser? ksh? sh? ash? zsh? bash? DOS 5.x? 4DOS? Windows
CMD? DR-DOS? csh? VMS? CP/M? AIX? TSO?

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
N

nickelstat

Sorry,
should have been more precise

The one that is common to ksh,csh,bash.
But I'm only interested in a small subset of the parsing, not the whole
thing.

Text::parseWords seems to do what I need.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top