有没有办法阻止无效的命令行参数

时间:2020-05-20 18:45:51

标签: perl

我有一个程序,用户可以像这样在命令行中输入随机字母,然后程序会对其进行数字排序

INPUT:   perl sample.pl a h y s j l
OUTPUT:  a h j l s y

INPUT:  perl sample.pl 1 6 4 8 2 0 4
OUTPUT: 0 1 2 4 4 6 8

,我可以使用-r命令行参数将其从z-> a或9-> 0反转。

但是如何提示用户输入了无效命令。 如果我只允许他们使用-r进行反转,那么如何防止他们输入除反转选项以外的其他内容。

是否将使用qw数组存储-r工作的有效条目?在没有进一步限制用户输入任何其他单词或数字的情况下?

3 个答案:

答案 0 :(得分:2)

处理命令行的标准方法是使用类似Getopt::Long

的模块
use warnings;
use strict;
use Getopt::Long qw(GetOptions);

my $rev = 0;
GetOptions('r' => \$rev) or die;

if ($rev) {
    print "reverse sort\n";
}
else {
    print "plain sort\n";
}

此代码将从-string中剥离所有@ARGV。如果看到-r,它将设置变量$rev = 1。如果看到任何其他-字符串,它将结束脚本(die)。

答案 1 :(得分:2)

使用Getopt::Long

use File::Basename qw( basename );
use Getopt::Long   qw( );

my $opt_reverse;

sub help {
   my $prog = basename($0);
   print
"Usage:
  $prog [options] [--] [character [...]]
  $prog --help

Options:
  --reverse
  -r
      Reverses the order of the output.
";
   exit(0);
}

sub usage {
   if ( my ($msg) = @_ ) {
      chomp($msg);
      warn("$msg\n");
   }

   my $prog = basename($0);
   warn("Try `$prog --help' for more information.\n");
   exit(1);
}

sub parse_args {
   $opt_reverse = 0;

   Getopt::Long::Configure(qw( posix_default ));
   Getopt::Long::GetOptions(
      'help|h|?'  => \&help,
      'r|reverse' => \$opt_reverse,
   )
      or usage();

   # An example of post-parsing validation.
   # Remove this check if it's inappropriate.
   if ( grep { length($_) != 1 } @ARGV ) {
      usage("Each argument must consist of a single character.");
   }

   return @ARGV;
}

sub main {
   my @chars = @_;
   if ($opt_reverse) {
      print(join(" ", sort { $b cmp $a } @chars), "\n");
   } else {
      print(join(" ", sort { $a cmp $b } @chars), "\n");
   }
}

main(parse_args());

答案 2 :(得分:2)

以下模板演示了如何使用Getopt::LongPod::Usage

#!/usr/bin/perl
#
# Description:
#   Describe purpose of the program
#
# Parameters:
#   Describe parameters purpose
#
# Date: Tue Nov 29 1:18:00 UTC 2019
#

use strict;
use warnings;

use Getopt::Long qw(GetOptions);
use Pod::Usage;

my %opt;
my @args = (
            'input|i=s',
            'output|o=s',
            'debug|d',
            'help|?',
            'man|m'
        );

GetOptions( \%opt, @args ) or pod2usage(2);

print Dumper(\%opt) if $opt{debug};

pod2usage(1) if $opt{help};
pod2usage(-exitval => 0, -verbose => 2) if $opt{man};

pod2usage("$0: No files given.")  if ((@ARGV == 0) && (-t STDIN));

__END__

=head1 NAME

program - brief on program's purpose 

=head1 SYNOPSIS

 program.pl [options] file(s)

 Options:
    -i,--input  input filename
    -o,--output output filename
    -d,--debug  output debug information
    -?,--help   brief help message
    -m,--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

=head1 DESCRIPTION

B<This program> accepts B<input> and processes to B<output> with purpose of achiving some goal.

=head1 EXIT STATUS

The section describes B<EXIT STATUS> codes of the program

=head1 ENVIRONMENT

The section describes B<ENVIRONMENT VARIABLES> utilized in the program

=head1 FILES

The section describes B<FILES> which used for program's configuration

=head1 EXAMPLES

The section demonstrates some B<EXAMPLES> of the code

=head1 REPORTING BUGS

The section provides information how to report bugs

=head1 AUTHOR

The section describing author and his contanct information

=head1 ACKNOWLEDGMENT

The section to give credits people in some way related to the code

=head1 SEE ALSO

The section describing related information - reference to other programs, blogs, website, ...

=head1 HISTORY

The section gives historical information related to the code of the program

=head1 COPYRIGHT

Copyright information related to the code

=cut