How can I allow undefined options when parsing args with Getopt

前端 未结 3 1251
故里飘歌
故里飘歌 2020-12-29 06:26

If I have a command line like:

my_script.pl -foo -WHATEVER

My script knows about --foo, and I want Getopt to set variable

3条回答
  •  太阳男子
    2020-12-29 06:39

    Aren't the remaining (unparsed) values simply left behind in @ARGV? If your extra content starts with dashes, you will need to indicate the end of the options list with a --:

    #!/usr/bin/perl
    use strict;
    use warnings;
    use Getopt::Long;
    use Data::Dumper;
    
    my $foo;
    my $result = GetOptions ("foo"   => \$foo);
    print Dumper([ $foo, \@ARGV ]);
    

    Then calling:

    my_script.pl --foo -- --WHATEVER
    

    gives:

    $VAR1 = [
              1,
              [
                '--WHATEVER'
              ]
            ];
    

    PS. In MooseX::Getopt, the "remaining" options from the command line are put into the extra_argv attribute as an arrayref -- so I'd recommend converting!

提交回复
热议问题