Resend: using Getopt::Long with option value having spaces

Discussion in 'Perl Misc' started by Sunil, Aug 22, 2003.

  1. Sunil

    Sunil Guest

    All,

    I have a text 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?


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

    I am using GetOptions as in future the metadata file may have new or
    changed options.



    Thanks,
    Sunil.
     
    Sunil, Aug 22, 2003
    #1
    1. Advertising

  2. On Fri, 22 Aug 2003 20:07:56 +0530
    "Sunil" <> wrote:
    > 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 am stuck because I am not able to pass spaces as part of comments.
    > Is there a workaround?


    Have you tried single quotes on the command-line?
    ie
    -comments='This is a comment'

    HTH

    Jim
     
    James Willmore, Aug 22, 2003
    #2
    1. Advertising

  3. Sunil wrote:
    >
    > I have a text 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?
    >
    > I am doing something like the following.
    > ####################################################
    > my $eval_string = '@ARGV = qw (' . "$scriptOptions" . ' )' ;
    > eval $eval_string;


    That is the same as:

    @ARGV = split ' ', $scriptOptions;

    But without the eval(). This appears to work for your data:

    $ perl -le'
    $x = q/t1.pl -frwk=perl -mode=1 -args=a,b,c -comments="This is
    comment 1"/;
    print for $x =~ /(\S.*?\S)(?=\s+-|$)/g;
    '
    t1.pl
    -frwk=perl
    -mode=1
    -args=a,b,c
    -comments="This is comment 1"



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Aug 22, 2003
    #3
  4. Sunil

    Sunil Guest

    "John W. Krahn" <> wrote in message
    news:...
    > Sunil wrote:
    > >
    > > I have a text 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?
    > >
    > > I am doing something like the following.
    > > ####################################################
    > > my $eval_string = '@ARGV = qw (' . "$scriptOptions" . ' )' ;
    > > eval $eval_string;

    >
    > That is the same as:
    >
    > @ARGV = split ' ', $scriptOptions;
    >
    > But without the eval(). This appears to work for your data:
    >
    > $ perl -le'
    > $x = q/t1.pl -frwk=perl -mode=1 -args=a,b,c -comments="This is
    > comment 1"/;
    > print for $x =~ /(\S.*?\S)(?=\s+-|$)/g;
    > '
    > t1.pl
    > -frwk=perl
    > -mode=1
    > -args=a,b,c
    > -comments="This is comment 1"
    >



    can some one please simpilify
    print for $x =~ /(\S.*?\S)(?=\s+-|$)/g;
    for this newbie.
     
    Sunil, Aug 26, 2003
    #4
  5. Sunil

    Bart Lateur Guest

    Sunil wrote:

    >I have a text 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?


    Even though my first thought was to use Getopt::Long, too, I don't think
    it's appropriate, as the data needs already to have been parsed into an
    array, @ARGV, by the shell. It's that job you have to do yourself.

    Your question seems related to 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/perlfaq4.html#How-can-I-split-a-%5bcharacter%5d-delimited-string-except-when-inside%0a%5bcharacter%5d--(Comma-separated-files)>

    The various quoting styles for the shell in order to embed quotes inside
    quoted fields, is an extra complication. It must be possible to embed
    them into the regex offered in that answer.

    You might still be able to use Getopt::Long to further process the array
    you get this way, though the hardwired connection with @ARGV may
    complicated the call. Perhaps it's overkill to try and use it, depending
    on how complex your lines actually are.

    --
    Bart.
     
    Bart Lateur, Aug 26, 2003
    #5
  6. Sunil

    Sunil Guest

    > > I have a text 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?
    > >
    > > I am doing something like the following.
    > > ####################################################
    > > my $eval_string = '@ARGV = qw (' . "$scriptOptions" . ' )' ;
    > > eval $eval_string;

    >
    > That is the same as:
    >
    > @ARGV = split ' ', $scriptOptions;
    >
    > But without the eval(). This appears to work for your data:
    >
    > $ perl -le'
    > $x = q/t1.pl -frwk=perl -mode=1 -args=a,b,c -comments="This is
    > comment 1"/;
    > print for $x =~ /(\S.*?\S)(?=\s+-|$)/g;
    > '
    > t1.pl
    > -frwk=perl
    > -mode=1
    > -args=a,b,c
    > -comments="This is comment 1"


    can some one please simpilify
    print for $x =~ /(\S.*?\S)(?=\s+-|$)/g;
    for this newbie.
     
    Sunil, Aug 27, 2003
    #6
  7. Sunil

    Bart Lateur Guest

    John W. Krahn wrote:

    >$x = q/t1.pl -frwk=perl -mode=1 -args=a,b,c -comments="This is
    >comment 1"/;
    >print for $x =~ /(\S.*?\S)(?=\s+-|$)/g;


    Nah... what about

    -comments="This includes -one, -two odditites"

    IMO you really need to properly parse the quoted strings.

    print for $x =~ /((?>[^"\s]+|"[^"]*")+)/g;

    Works well with

    $x = q/t1.pl -frwk=perl -mode=1 -args=a,b,c -comments="This
    is comment 1" -comments="This contains -one, -two oddities"/;

    (unwrapped)

    --
    Bart.
     
    Bart Lateur, Aug 27, 2003
    #7
  8. Sunil

    Soren A. Guest

    Re: using Getopt::Long with option value having spaces

    Sunil (Sunil <>) posted to [comp.lang.perl.misc]:
    "John W. Krahn" <> wrote:

    >> That is the same as:


    >> @ARGV = split ' ', $scriptOptions;


    >> But without the eval(). This appears to work for your data:


    >> $ perl -le'
    >> $x = q/t1.pl -frwk=perl -mode=1 -args=a,b,c -comments="This is
    >> comment 1"/;
    >> print for $x =~ /(\S.*?\S)(?=\s+-|$)/g;
    >> '
    >> t1.pl
    >> -frwk=perl
    >> -mode=1
    >> -args=a,b,c
    >> -comments="This is comment 1"


    and Sunil came back with:
    > can some one please simpilify
    > print for $x =~ /(\S.*?\S)(?=\s+-|$)/g;
    > for this newbie.


    This is a classic example of making it harder for other people to help
    you. Very understandable though, because when people are frustrated they
    don't think clearly. The OP, Sunil, needed to specify more precisely
    what he didn't understand about the suggested code; a later follow-up
    guessed that he didn't understand the regex construct, but I believe
    it's just as possible that he didn't understand the outer perl idiom,
    that is, the structure. So I am going to comment on that in hopes it
    will help.

    To a Perl newbie, something like "print for $x =~
    /(\S.*?\S)(?=\s+-|$)/g;" is puzzling. That's because they haven't even
    begun to deal with the underlying principles of Perl's notions of truth,
    or the flow control elements of perl syntax (and it's OK, happens to
    many of us, it takes a while).

    Sunil, it's not a question of simplifying it. It's a question of
    complexifying it, that is, this was a statement made in a nice, brief,
    concise way -- like the way humans normally talk in daily life (when
    they are not making political speeches, sermons or delivering seminars).
    So, OK, to complexify it enough for you to understand it:

    while( my $truth = $x =~ /(\S.*?\S)(?=\s+-|$)/g ) { print $_ if $truth; }

    Is that a little better? "for" is often a more idiomatic way to say
    something in perl than "while" is. And then there's the use of the
    implicit "$_" var which you don't see after "print". Together, that's
    Idiomatic Perl. You don't have to write Perl that way (TMTOWTDI,
    presumably you are aware of that expression): but if you ask for help
    from perl experts in fora like this, you will be perplexed at the advice
    until you grapple with these issues I've pointed out.

    It helped me to read the Camel and other perl mainstays over and over
    and over again.

    HTH,
    Soren A
    --
    Sooooo, tell me, my little one-eyed one: on what poor, pitiful, defenseless
    planet has my MONSTROSITY been ... unleashed?
    -- "Dr. Jumba" (Disney's "Lilo and Stitch")
     
    Soren A., Aug 29, 2003
    #8
  9. Sunil wrote:
    >
    > All,
    >
    > I have a text 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?
    >
    > I am doing something like the following.
    > ####################################################
    > my $eval_string = '@ARGV = qw (' . "$scriptOptions" . ' )' ;
    > eval $eval_string;


    Eww. What happens if $scriptOptions happens to contain a ")" in it?

    Anyway, the proper solution would be to do:

    use Text::parseWords qw(shellwords);
    @ARGV = shellwords($scriptOptions);

    > GetOptions("fwk=s" => \$framework,
    > "mode=s" => \$errMode,
    > "args=s" => \@arguments,
    > "comments=s" => \$comments );


    This part is ok, though.

    > ####################################################
    >
    > I am using GetOptions as in future the metadata file may have new or
    > changed options.


    --
    $a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
    );{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
    ]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
     
    Benjamin Goldberg, Sep 2, 2003
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Shapiro

    python and getopt and spaces in option

    David Shapiro, Jun 9, 2009, in forum: Python
    Replies:
    1
    Views:
    821
    Jorgen Grahn
    Jun 10, 2009
  2. Sunil
    Replies:
    5
    Views:
    361
    Bart Lateur
    Aug 14, 2003
  3. mothra
    Replies:
    3
    Views:
    379
    Shawn Corey
    Dec 2, 2004
  4. Perl Learner
    Replies:
    1
    Views:
    213
    Jim Keenan
    Jun 13, 2005
  5. nahum_barnea
    Replies:
    3
    Views:
    136
    Peter Makholm
    Sep 21, 2010
Loading...

Share This Page