using Getopt::Long with option value having spaces

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

  1. Sunil

    Sunil Guest

    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.
     
    Sunil, Aug 13, 2003
    #1
    1. Advertising

  2. Sunil

    Tony Curtis Guest

    >> On Wed, 13 Aug 2003 14:21:36 +0530,
    >> "Sunil" <> said:


    > 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.
     
    Tony Curtis, Aug 13, 2003
    #2
    1. Advertising

  3. Sunil

    Sunil Guest

    "Tony Curtis" <> wrote in message
    news:...
    > >> On Wed, 13 Aug 2003 14:21:36 +0530,
    > >> "Sunil" <> said:

    >
    > > 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?
    >


    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.
     
    Sunil, Aug 14, 2003
    #3
  4. Sunil

    Bart Lateur Guest

    Sunil wrote:

    >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/perlfaq4.html#How-can-I-split-a-%5bcharacter%5d-delimited-string-except-when-inside%0a%5bcharacter%5d--(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.


    --
    Bart.
     
    Bart Lateur, Aug 14, 2003
    #4
  5. On Thu, Aug 14, Bart Lateur inscribed on the eternal scroll:

    > <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 person responsible for these long URLs should be shot, IMO.)


    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?

    cheers
     
    Alan J. Flavell, Aug 14, 2003
    #5
  6. Sunil

    Bart Lateur Guest

    Alan J. Flavell wrote:

    >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.

    --
    Bart.
     
    Bart Lateur, Aug 14, 2003
    #6
    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:
    798
    Jorgen Grahn
    Jun 10, 2009
  2. Sunil
    Replies:
    8
    Views:
    140
    Benjamin Goldberg
    Sep 2, 2003
  3. mothra
    Replies:
    3
    Views:
    343
    Shawn Corey
    Dec 2, 2004
  4. Perl Learner
    Replies:
    1
    Views:
    199
    Jim Keenan
    Jun 13, 2005
  5. nahum_barnea
    Replies:
    3
    Views:
    127
    Peter Makholm
    Sep 21, 2010
Loading...

Share This Page