using Getopt::Long with option value having spaces

S

Sunil

All,
I am trying to use GetOptions to get the values corresponding to options
in a string like in

####################################################
my $eval_string = '@ARGV = qw (' . "$scriptOptions" . ' )' ;
eval $eval_string;
GetOptions("fwk=s" => \$framework,
"mode=s" => \$errMode,
"args=s" => \@arguments,
"comments=s" => \$comments );
####################################################

The problem I am having is that if $scriptOptions contains something
like -comments="this is a comment", I get only "this as the comment as
GetOptions does not take kindly to option values having spaces. Is there any
workaround for this?


Thanks,
Sunil.
 
T

Tony Curtis

All, I am trying to use GetOptions to get the values
corresponding to options in a string like in
#################################################### my
$eval_string = '@ARGV = qw (' . "$scriptOptions" . ' )'
; eval $eval_string; GetOptions("fwk=s" => \$framework,
"mode=s" => \$errMode, "args=s" => \@arguments,
"comments=s" => \$comments );
####################################################
The problem I am having is that if $scriptOptions
contains something like -comments="this is a comment", I
get only "this as the comment as GetOptions does not
take kindly to option values having spaces. Is there any
workaround for this?

The " around the value of $scriptOptions are part of the
perl syntax, not part of the string.

I'm not sure why you would want to fiddle with @ARGV
though? IMHO the point of something like Getopt::Long is
to take unstructured text from the command-line and turn
it into a usable data structure. If you've already got
the data in the program why try to push it back out and
pull(*) it in again?

hth
t

(*) REXX pun.
 
S

Sunil

Tony Curtis said:
The " around the value of $scriptOptions are part of the
perl syntax, not part of the string.

I'm not sure why you would want to fiddle with @ARGV
though? IMHO the point of something like Getopt::Long is
to take unstructured text from the command-line and turn
it into a usable data structure. If you've already got
the data in the program why try to push it back out and
pull(*) it in again?

Sorry for not being clear the first time,
Let me try to make amends.

I have a file which has some metadata, like
----
t1.pl -frwk=perl -mode=1 -args=a,b,c -comments="This is comment 1"
t2.sql -frwk=sql -mode=2 -args=x -comments="This is comment 2"
----

I need to read this file line by line and parse it to get the values of
frwk, -mode and comments, so that I can create another string depending on
the different values for this string and pass it on to the corresponding
perl api I have which will execute it for me.

I am stuck because I am not able to pass spaces as part of comments. Is
there a workaround?

Thanks,
Sunil.
 
B

Bart Lateur

Sunil said:
I have a file which has some metadata, like
----
t1.pl -frwk=perl -mode=1 -args=a,b,c -comments="This is comment 1"
t2.sql -frwk=sql -mode=2 -args=x -comments="This is comment 2"
----

I need to read this file line by line and parse it to get the values of
frwk, -mode and comments, so that I can create another string depending on
the different values for this string and pass it on to the corresponding
perl api I have which will execute it for me.

I am stuck because I am not able to pass spaces as part of comments. Is
there a workaround?

Perhaps look at this FAQ entry:

"How can I split a [character] delimited string except when
inside [character]? (Comma-separated files)"

<http://perldoc.com/perl5.8.0/pod/pe...n-inside [character]--(Comma-separated-files)>

(The person responsible for these long URLs should be shot, IMO.)

In your case, you want to split on spaces except when in quotes. So
replace every occurrence of "," in that regex with "\ ". Spaces without
preceding backslash would be stripped from the regex thanks to the /x
modifier.
 
B

Bart Lateur

Alan said:
I'm sure you realise that they're generated by software.

It would presumably need a bit more craft to build a short form
of the fragment name, and to ensure that it's unique within the
document. I'm sure it's do-able, but who's going to volunteer to
implement it?

Just a counter will do, perhaps with a letter as prefix, as these are
the only anchors in the entire document.
 

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,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top