【问题标题】:perl - use command-line argument multiple timesperl - 多次使用命令行参数
【发布时间】:2019-12-21 02:59:33
【问题描述】:

我正在修改一个 perl 脚本,其中命令行参数的解析如下:

if ($arg eq "-var1") {
    $main::variable1 = shift(@arguments)
} elsif ($arg eq "-var2") {
    $main::variable2 = shift(@arguments)
} elsif ($arg eq "var3") {
    $main::variable3 = shift(@arguments)
} ... 

所以有一大堆 elsif 语句来涵盖所有命令行参数。

我现在处于想要多次使用参数“-var2”的情况。

所以我的 main::variable2 应该是一个数组,其中包含使用“-var2”传递的所有值。

我发现使用 Perl::getopt 可以轻松实现 (Perl Getopt Using Same Option Multiple Times)。

但是,我的脚本解析其命令行参数的方式不同。所以我想知道是否可以实现它,而不必更改解析参数的方式。

【问题讨论】:

  • Re "无需更改解析参数的方式。",我不确定您的意思。如果不进行更改,您的脚本将继续执行相同的操作。

标签: perl


【解决方案1】:

这不是您的实际代码,是吗?它甚至不会编译。

如果Getopt::Long 不能解决您的问题,我会感到非常惊讶,使用库而不是编写自己的代码确实是一个更好的主意。

但是更改代码以将 -var2 选项存储在数组中是很简单的。

my ($variable1, @variable2, $variable3);

if ($arg eq "-var1") {
    $variable1 = shift(@arguments)
} elsif ($arg eq "-var2") {
    push @variable2, shift(@arguments)
} elsif ($arg eq "-var3") {
    $variable3 = shift(@arguments)
}

(我还从您的变量中删除了main::,并添加了可能缺少的$s。您不太可能希望使用包变量而不是词法变量。)

【讨论】:

    【解决方案2】:

    这个特殊的轮子already exists。请不要试图重新发明它。这只会让尝试使用您的脚本的人感到痛苦。没有理由强迫人们学习一套全新的规则来执行您的程序。

    use File::Basename qw( basename );
    use Getopt::Long   qw( );
    
    my $foo;
    my @bars;
    my $baz;
    
    sub help {
       my $prog = basename($0);
       print
    "Usage:
      $prog [options]
      $prog --help
    
    Options:
      --foo foo
          ...
    
      --bar bar
          May be used multiple times.
          ...
    
      --baz baz
          ...
    ";
       exit(0);
    }
    
    
    sub usage {
       if (@_) {
          my ($msg) = @_;
          chomp($msg);
          say STDERR $msg;
       }
    
       my $prog = basename($0);
       say STDERR "Try '$prog --help' for more information.";
       exit(1);
    }
    
    
    sub parse_args {
       Getopt::Long::Configure(qw( posix_default ));
       Getopt::Long::GetOptions(
          "help"  => \&help,
          "foo=s" => \$foo,
          "bar=s" => \@bars,
          "baz=s" => \$baz,
       )
          or usage();
    
       !@ARGV
          or usage("Too many arguments");
    
       return @ARGV;
    }
    
    
    main(parse_args());
    

    【讨论】:

      【解决方案3】:

      嗯,记录你的核心是一种很好的做法——你会在你回来进行更改时立即感谢它

      注意:在 Linux 中,它需要安装 perl-doc 软件包才能完全使用 --man 选项

      #!/usr/bin/perl
      #
      # Description:
      #   Describe purpose of the program
      #
      # Parameters:
      #   Describe parameters purpose
      #
      # Date: Tue Nov 29 1:18:00 UTC 2019
      #
      
      use warnings;
      use strict;
      
      use Getopt::Long qw(GetOptions);
      use Pod::Usage;
      
      my %opt;
      
      GetOptions(
              'input|i=s'     => \$opt{input},
              'output|o=s'    => \$opt{output},
              'debug|d'       => \$opt{debug},
              'help|?'        => \$opt{help},
              'man'           => \$opt{man}
      ) or pod2usage(2);
      
      pod2usage(1) if $opt{help};
      pod2usage(-exitval => 0, -verbose => 2) if $opt{man};
      
      print Dumper(\%opt) if $opt{debug};
      
      
      __END__
      
      =head1 NAME
      
      program - describe program's functionality 
      
      =head1 SYNOPSIS
      
      program.pl [options]
      
       Options:
          -i,--input  input filename
          -o,--output output filename
          -d,--debug  output debug information
          -?,--help   brief help message
             --man    full documentation
      
      =head1 OPTIONS
      
      =over 4
      
      =item B<-i,--input>
      
      Input filename
      
      =item B<-o,--output>
      
      Output filename
      
      =item B<-d,--debug>
      
      Print debug information.
      
      =item B<-?,--help>
      
      Print a brief help message and exits.
      
      =item B<--man>
      
      Prints the manual page and exits.
      
      =back
      
      B<This program> accepts B<several parameters> and operates with B<them> to produce some B<result>
      
      =cut
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-23
        • 1970-01-01
        • 2018-04-07
        • 2011-08-15
        • 1970-01-01
        • 1970-01-01
        • 2017-07-23
        • 2020-09-21
        相关资源
        最近更新 更多