Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

perl getopt question?

  • 26-09-2006 4: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,570 ✭✭✭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,570 ✭✭✭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