DS has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks , How can I a pass more than one word for one option : for example if my script take the option -w , then I want to be able to do somthing like this :
perl myScript.pl -w word1 word2
so word1 and word2 are one argument passed with -w , now I wanted to treat each one seprate. my code looks like this :
my $usage = "Usage: $0 -t TARGET -r RELEASE -b BUILDTYPE where: TARGET = {all|esel|esci|sci|btsc|sbc|cn|cis} RELEASE = number + optional buildid for the build BUILDTYPE = {clean|incremental|inc} -- note last two "; my $TARGET=undef; my $RELEASE=undef; my $BUILDTYPE=undef; my $TARGET_FOUND="0"; my $BASEDIR=""; my $DRIVENAME; my $NICE; while(@ARGV){ my $argin = shift; if ($argin =~ /-t/){$TARGET= shift} if ($argin =~ /-r/){$RELEASE=shift} if ($argin =~ /-b/){$BUILDTYPE=shift} } if (( !defined $TARGET ) or ( !defined $RELEASE ) or ( !defined $BUILD +TYPE)) {print $usage;exit;}
so I want to pass more than one RELEASE for the -r option and deal with each one ,, I hope this is clear ,,, thanks for help and hints.

Replies are listed 'Best First'.
Re: Passing Options
by fruiture (Curate) on Aug 12, 2002 at 14:30 UTC

    check Getopt::Long!

    use Getopt::Long; our @realeases; GetOptions( 't=s' => \ our $target, 'b=s' => \ our $buildtype, 'r=s' => \ @releases );
    --
    http://fruiture.de
      check Getopt::Long!
      I just did. I cannot find anywhere how to tell Getopt::Long to parse the command line as wished.
      GetOptions( 't=s' => \ our $target, 'b=s' => \ our $buildtype, 'r=s' => \ @releases );
      That requires multiple -t, -b and -r arguments. From a command line of
      -r foo bar baz -b dog cat -t pony
      it will put foo in @releases, dog in $buildtype, pony in $target, and leave the rest in @ARGV.
      use Getopt::Long; our @releases; GetOptions( 't=s' => \ our $target, 'b=s' => \ our $buildtype, 'r=s' => \ @releases ); print "Target: $target\n"; print "Buildtype: $buildtype\n"; print "Releases: @releases\n"; print "ARGV: @ARGV\n"; __END__ Target: pony Buildtype: dog Releases: foo ARGV: bar baz cat
      Not at all as wished.

      I don't know any module that will parse the command line as was wished, but it shouldn't be too hard to roll your own.

      Abigail

        Probably the "originally wanted" was only abstract: "Pass more than one value for a commandline option". Why should anybody write a new module that parses the options in a way that differs from nearly all other applications, if he could just change the own program's usage message and make it work with standard modules and methods?

        --
        http://fruiture.de
      I would change the \@release into \$release and use : colon or pipe separated values to send multiple arguments without repeating the option. Then simply create your array with a split.
      use Getopt::Long; our $realeases; GetOptions( 't=s' => \ our $target, 'b=s' => \ our $buildtype, 'r=s' => \ $releases ); our @releases = split(/\:/,$releases);
      Program is called with:
      perl script.pl -r 1:2:3 -b blah -t blah
Passing multiple arguments to a flag
by Ferret (Scribe) on Aug 12, 2002 at 17:25 UTC

    The beautiful thing about Getopt::Long is that you can write custom argument handlers. I like them a lot. The below is somewhat slapped together, but should do approximately what you want.

    #!/bin/perl use strict; use Getopt::Long; my @words; my $wordsub = sub { while (@ARGV){ if ( $ARGV[0] !~ /^-/ ){ push @words, shift @ARGV } else { last } } }; GetOptions('words' => $wordsub);

    @words should contain a list of words. You should be able to hack together the same for your release variable.

      thanks a lot to all , thats really help :)
Re: Passing Options
by tommyw (Hermit) on Aug 12, 2002 at 16:12 UTC
    This is untested, but
    while(@ARGV){ my $argin = shift; while ($argin =~ /^-/) { $argtype=$argin; $argin=shift; } if ($argtype =~ /^-t/) {$TARGET.=" $argin"} if ($argtype =~ /^-r/) {$RELEASE.=" $argin"} if ($argtype =~ /^-b/) {$BUILDTYPE.= " $argin"} }
    ought to do the trick. I've anchored the patterns so that 'touch-typing' doesn't end up as being a -t type argument.

    --
    Tommy
    Too stupid to live.
    Too stubborn to die.