How to get the parameters in the order entered by the user using Perl Getopt :: Long?

I have an existing Perl program that uses Getoptpackage and Getopt::Long::Configurewith permuteas one of the parameters. However, now I need to keep the order of the options entered by the user. There is an option $RETURN_IN_ORDERmentioned in Long.pm, however it does not seem to be used anywhere.

When I pass return_in_order, I get the following error.


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


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

Thank.

+3
source share
7 answers

You have two answers pointing to "require_order", but I think that both of these answers do not understand what "require_order" does and what you are looking for.

If "require_order" is not set, you can write (on the command line):

-a file -b

If both the -a and -b options are simple options (without taking an argument). With a require_order setting, the presence of a file completes the parameters, and the -b flag becomes a file name.

, , , '-a' '' -b '. , Getopt:: Long . , Getopt:: *, ( , , Getopt:: JLSS, ). [ , Gmail, .]

+6

require_order, manpage, .: -)

+5

, , .

Getopt:: Long "" :

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

GetOptions, 1 3 @ARGV , . , , .

, ( !):

#!/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" : (: "-p X" , "-p = X" )

1
2
3
4
+2

, , .

mytool -arg1 value 1 -arg2 value2 -arg3 value3

require_order .

, , . -, .

tsee, , .

, , .

+2

, , Getopt:: Mixed. nextOption, , , .

:

myscript /usr --include /usr/local/bin --exclude /usr/local

, , , , script, .

+1

. . , -e <foo> -f <foo> , ( ).

use Data::Dumper; use Getopt::Long; use strict; use warnings;
my @Sources;    # Each element is [was -f, parameter]

my $dr_save_source = sub {      # Called each time we see -e or -f
    my ($which, $text) = @_;
    push @Sources, [$which eq "f", $text];
}; # dr_save_source

GetOptions("e=s@" => $dr_save_source, "f=s@" => $dr_save_source);
print Dumper(\@Sources);

$dr_save_source , (=> $dr_save_source), GetOptions , .

, , ... .

0

Source: https://habr.com/ru/post/1698465/


All Articles