views:

535

answers:

5

I have an existing Perl program that uses Getopt package and Getopt::Long::Configure with permute as one of the options. However, now I need to keep the order of the options entered by the user. There is an option $RETURN_IN_ORDER mentioned in the Long.pm, however doesn't seem to be used anywhere at all.

When I pass return_in_order, I am getting the following error.


Getopt::Long: unknown config parameter "return_in_order" at C:/Program Files/IBM/RationalSDLC/common/lib/perl5/5.8.6/Getopt/Long.pm line 1199.


Can someone please tell me if this is supported at all and if so, the right way to use? If not, I would like to know the other alternatives I have.

Thanks.

+5  A: 

It's called require_order, according to the manpage I have here. :-)

Chris Jester-Young
+5  A: 

I think you want "require_order"

http://perldoc.perl.org/Getopt/Long.html#Configuring-Getopt%3a%3aLong

SquareCog
+4  A: 

You have two answers pointing at 'require_order', but I think both those answers are misunderstanding both what 'require_order' does and what you seek.

If 'require_order' is unset, then you can write (on the command line):

-a file -b

Where both -a and -b are simple options (not taking an argument). With 'require_order' set, the presence of 'file' terminates the options and the '-b' flag becomes a 'file name'.

What I think you are seeking is a mechanism that allows you to tell that '-a' appeared before both 'file' and '-b'. I do not think that Getopt::Long supports that. In fact, I'm not aware of any Getopt::* module that does that (other than my own, unpublished, Getopt::JLSS which I sometimes use). [If you are interested in the code, send me an email at Gmail, using a dot between first and last name.]

Jonathan Leffler
+2  A: 

Maybe it would have helped if you provided an example call to your program.

Getopt::Long supports mixing options with ordinary "positional" args:

./foo --option1 --option2 file1 file2 file3

If you only parse the options with GetOptions, file1 to 3 will be left in @ARGV in the order they appeared. Maybe you can use that behaviour to achieve what you want.

Furthermore, you can specify the same option multiple times and have the results put in an array (in order!):

#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
my @permute;
GetOptions(
  'p=s' => \@permute,
);

print "$_\n" for @permute;

"perl foo.pl -p=1 -p=2 -p=3 -p=4" results in this output: (Note: "-p X" works the same as "-p=X")

1
2
3
4
tsee
+1  A: 

The requirement in my case is very different as the command line parameters are going to be a combination of options and arguments, such as the following.

mytool -arg1 value 1 -arg2 value2 -arg3 value3

And this is why require_order is not particularly helpful.

And as I just figured out, the problem is not really with the parsing component. It's with the hash object that is used to store them.

As tsee right pointed out, the order will be same if we use the default configurations.

Thanks folks, anyways.

Jay