Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

perl getopt question?

  • 26-09-2006 05:18PM
    #1
    Closed Accounts Posts: 247 ✭✭


    Hi,
    i want to use getopt to input a list of tests to run in my script
    Something along the lines of this:
    GetOptions(
                "i"          => \$interactive,
                "l0=s"       => \$lex_0,
                "l1=s"       => \$lex_1,
                "h"          => \$display_help,
               );
    

    The only thing is i want -l0 and -l1 options to be more than 1 string eg
    ./script.pl -l0 bla0 bla1 bla2 -l1 bla3 bla4 -i
    

    How can i do this?
    At the moment im calling the script by this:
    ./script.pl -l0 bla0:bla1:bla2 -l1 bla3:bla4 -i
    
    and then using an internal split function to separate the tests but i dont like this as its messy.
    ./script.pl -l0 bla0 bla1 bla2 -l1 bla3 bla4 -i
    
    Using the way just above:
    $lex_0 = bla0
    $lex_1 = bla3
    @ARGV[0] = bla1
    @ARGV[1] = bla2
    @ARGV[2] = bla4

    But now i have no idea if bla1, bla2 or bla4 are associated with $lex_1 or $lex_2??


    Any help please?
    Thanks!!
    John


Comments

  • Registered Users, Registered Users 2 Posts: 6,652 ✭✭✭daymobrew


    First thing to do is to change the $lex_0 and $lex_1 variables to be arrays.
    GetOptions(
                "i"          => \$interactive,
                "l0=s"       => \@lex_0,
                "l1=s"       => \@lex_1,
                "h"          => \$display_help,
               );
    
    this allows you to specify their switches multiple times.
    # Yes, this is very verbose.
    ./script.pl -l0 bla0 -l0 bla1 -l0 bla2 -l1 bla3 -l1 bla4 -i
    
    which will be the equivalent to
    @lex_0 = qw/ bla0 bla1 bla2 /;
    @lex_1 = qw / blah3 blah4 /;
    
    Having said that, I call GetOptions like above (with arrays), but invoke it like your colon separated method and then use an internal split/join function
    # Note two -l0 switches.
    ./script.pl -l0 bla0:bla1 -l0 bla2 -l1 bla3:bla4 -i
    
    # This changes lex_0 from having two elements qw/ bla0:bla1 blah2 /
    # to having three elements qw/ bla0 bla1 blah2 /.
    @lex_0 = split( /:/ ,join( ':', @lex_0 ) );
    


  • Closed Accounts Posts: 23 atholl45


    No no no thats rubbish lads, ye have'nt a clue. There's much more simpler and far more obvious way of doing that.


  • Closed Accounts Posts: 247 ✭✭because_I_can


    yes there is atholl45 - its just a matter of using the @ARGV and parsing that array and not using getoptions at all.
    Very simple really when i just thought about it for a bit...

    in future though can you maybe help out a bit instead of saying we dont have a clue??


    if anyone wants i can post the code but its very simple.
    thanks!!


  • Registered Users, Registered Users 2 Posts: 6,652 ✭✭✭daymobrew


    if anyone wants i can post the code but its very simple.
    Please post the code. Will it handle everything that GetOptions::Long does or is it specific to your needs?


Advertisement